Chris911
Chris911

Reputation: 4199

Command line arguments with multiple values

I'm doing a perl script and I need to get multiple values from the command line. Example:

perl script.pl --arg1 op1 op2 op3

I'm using Getopt::Long and I can get this to work:

perl script.pl --arg1 op1 --arg1 op2 --arg1 op3

But I really need (want) the first option.

I checked in their documentation and this is supposed to do what I want:

GetOptions('arg1=s{3}' => \@myArray);

http://search.cpan.org/~jv/Getopt-Long-2.38/lib/Getopt/Long.pm#Options_with_multiple_values

But I'm getting this error:

Error in option spec: "arg1=f{3}"

Any ideas / solutions?

Upvotes: 4

Views: 9734

Answers (4)

sdaau
sdaau

Reputation: 38691

Similar question to getoptions function perl multi value not working, I guess ... Anyways, it seems that just using "optlist=s" => \@list, works for me, to store duplicate / repeat options in an array; this is my version:

$ perl --version | grep This && perl -MGetopt::Long -le'print $Getopt::Long::VERSION;'
This is perl, v5.10.1 (*) built for i686-linux-gnu-thread-multi
2.38

An example (test.pl):

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my $numone = 0;
my $numtwo = 1;
my @list=();
my $result;
$result = GetOptions (
  "numone=i" => \$numone,
  "numtwo=i"   => \$numtwo,
  "optlist=s" => \@list,
);

printf("result: %d;\n", $result);
printf("numone: %d, numtwo %d, optlist:\n", $numone, $numtwo);

foreach my $tmplist (@list) {
  printf(" entry: '%s'\n", $tmplist);
}

Test output:

$ perl test.pl --numone 10 --numtwo 20 --optlist testing --optlist more --optlist options
result: 1;
numone: 10, numtwo 20, optlist:
 entry: 'testing'
 entry: 'more'
 entry: 'options'

Upvotes: 0

Kevin
Kevin

Reputation: 21

I'm not sure why folks didn't offer this solution, but this post is so old, it's probably too late now to be helpful.

There isn't an automatic way to do this that I've found.

What you need to do is quote the multiple arguments and parse them inside the code:

perl myscript.pl -m 'a b c'

and then split the -m argument value inside the code and do whatever is necessary from there.

Upvotes: 2

ArtMat
ArtMat

Reputation: 2150

I think your problem could be f{3}. f is used for float arguments (real numbers). You should use the s specifier if you have strings as arguments. Regarding the number of arguments, the documentation says:

It is also possible to specify the minimal and maximal number of arguments an option takes. foo=s{2,4} indicates an option that takes at least two and at most 4 arguments. foo=s{,} indicates one or more values; foo:s{,} indicates zero or more option values.

Take into account hise note from docs and adjust to your needs.

Upvotes: 5

Robert Young
Robert Young

Reputation: 313

Your code works for me, but it looks like that feature was only added to Getopt::Long recently (version 2.35), so you might have an old version of Getopt::Long. Run

perl -MGetopt::Long -le'print $Getopt::Long::VERSION;'

to see what version you have.

Upvotes: 4

Related Questions