http://qs1969.pair.com?node_id=1049620

kcarpenter@soleo.com has asked for the wisdom of the Perl Monks concerning the following question:

I need to read multiple word command line arguemnts that are not quoted.

The number of words after the flag can vary with each execution of the script
Something like.
 my_script.pl -a one -b two words -c three words here
and on the next itteration
 my_script.pl -a two words -b oneword -c four words here now
Is there an easy way to do this ?
Thanks

Replies are listed 'Best First'.
Re: easiest way to read multiple word command arguments
by toolic (Bishop) on Aug 15, 2013 at 16:15 UTC
    Options with multiple values
    Warning: What follows is an experimental feature.
    use warnings; use strict; use Getopt::Long; use Data::Dumper; my @aopt; my @bopt; GetOptions( 'a=s{1,}' => \@aopt, 'b=s{1,}' => \@bopt, ) or die; print Dumper(\@aopt); print Dumper(\@bopt); __END__ my_script.pl -a sf -b asdad dfsdf $VAR1 = [ 'sf' ]; $VAR1 = [ 'asdad', 'dfsdf' ];
Re: easiest way to read multiple word command arguments
by choroba (Cardinal) on Aug 15, 2013 at 16:06 UTC
    Process the options one by one. If the option does not start with a dash, add it to the previous one:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %options; my $current_option; my $space; for (@ARGV) { if (/^-/) { $current_option = $_; $space = q(); # No space before the + first word. } else { $options{$current_option} .= $space . $_; $space = ' '; } } print Dumper \%options;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: easiest way to read multiple word command arguments
by kennethk (Abbot) on Aug 15, 2013 at 19:12 UTC
    Is there a good reason you don't just use Getopt::Std or Getopt::Long, and then require that multiword input get quoted? e.g.
    my_script.pl -a one -b 'two words' -c 'three words here' my_script.pl -a 'two words' -b oneword -c 'four words here now'

    This would seem to be more in line with *NIX tradition.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: easiest way to read multiple word command arguments
by ramlight (Friar) on Aug 15, 2013 at 18:37 UTC
    Here's another variation on the same theme. In this case a new array is created from @ARGV with multiple words collapsed into one array element. The advantage is that this rebuilt version of @ARGV can be fed into Getopt::Long's GetOptionsFromArray method:
    use strict; use warnings; use Getopt::Long qw(GetOptionsFromArray); my @arglist = (); for my $arg (@ARGV) { if ($arg =~ /^-/) { push(@arglist, $arg); } else { if ($arglist[$#arglist] =~ /^-/) { push(@arglist, $arg); } else { $arglist[$#arglist] .= ' '.$arg; } } } my ($opta, $optb, $optc); GetOptionsFromArray(\@arglist, 'a=s' => \$opta, 'b=s' => \$optb, 'c=s' => \$optc); for ($opta, $optb, $optc) { print $_,"\n"; }
Re: easiest way to read multiple word command arguments
by mhearse (Chaplain) on Aug 15, 2013 at 18:20 UTC
    For simple command line args I recommend Getopt::Std. Use perldoc Getopt::Std to get a man page detailing usage. It's very easy to use:
    use Getopt::Std; use Data::Dumper; my %opts; getopt('a:b:c:', \%opts); print Dumper(\%opts);