in reply to Re: Wrap multiple programs
in thread Wrap multiple programs
To the OP: Although below I'll show a way to put multiple commands on one command line, I actually don't necessarily recommend this, nor would I recommend building a monolithic "one script to do it all", instead follow stevieb's and Athanasius's advice to modularize. One element of the Unix philosophy is to make each tool "Do One Thing and Do It Well". If your scripts need to share common functions, then do that through a module, rather than shoving all the code into one script.
You can not get command line options twice within the same program.
That statement is just a little too absolute for my tastes, it's totally possible to get too clever with @ARGV :-)
use warnings; use strict; use Data::Dump; use Getopt::Std; use Getopt::Long; # fake command line local @ARGV = qw/ one -abc -- two -d -e -- three -abcdef -- four --foo --bar --bar --bar -- five --foo --quz baz /; while (@ARGV) { my $cmd = shift @ARGV; getopts('abcdef', \my %o); dd $cmd, \%o; last if $o{f}; } while (@ARGV) { my $cmd = shift @ARGV; GetOptions(\my %o, 'foo!', 'bar+', 'quz!'); dd $cmd, \%o; last if $o{quz}; } dd @ARGV; __END__ ("one", { a => 1, b => 1, c => 1 }) ("two", { d => 1, e => 1 }) ("three", { a => 1, b => 1, c => 1, d => 1, e => 1, f => 1 }) ("four", { bar => 3, foo => 1 }) ("five", { foo => 1, quz => 1 }) "baz"
Also, it's possible to read options from strings, such as the lines of STDIN. Getopt::Long has GetOptionsFromString, and with Getopt::Std it's possible with a little trick.
|
|---|