Hello all,
I am working on taking command line switches.
I would like to have an option take two parameters on the command line. i.e.:
./program.pl -a param1 param2 -b param3 param4
Initially I wrote the script to take one parameter per switch.
#!/usr/bin/perl use strict; use warnings; use Getopt::Long; my %options = (); GetOptions ( "a=s" => \$options{'a'}, "b=s" => \$options{'b'}, "z!" => \$options{'z'}, ); my %Subs = ( 'a' => \&do_a, 'b' => \&do_b, 'z' => \&do_z, ); while (my ($sub_name, $function) = each %Subs){ $function->($options{$sub_name}); } sub do_a { my $active = shift; return unless $active; my $param1 = $active; # my $param2 = shift @ARGV; # print "$param1, $param2, \n"; } sub do_b { my $active = shift; return unless $active; my $param1 = $active; # my $param2 = shift @ARGV; # print "$param1, $param2, \n"; } sub do_z { my $active = shift; return unless $active; #do stuff without parameters }
To take two parameters I added the line:
my $param2 = shift @ARGV; #commented out in the code above
this works great if the parameters are passed in order:
$perl program.pl -a param1_a param2_a -b param1_b param2_b param1_a, param2_a, param1_b, param2_b,
but if I switch the order that I call the options the in the reverse order the second parameters are passed to the wrong sub:
$perl program.pl -b param1_b param2_b -a param1_a param2_a param1_a, param2_b, param1_b, param2_a,
Can anyone suggest a method for taking two parameters from the command line?
In reply to GetOpts::long Multiple Parameters per option by PyrexKidd
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |