Author Topic: [Perl] Command line arguments with multiple values  (Read 1253 times)

0 Members and 1 Guest are viewing this topic.

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
[Perl] Command line arguments with multiple values
« on: May 11, 2012, 10:00:50 pm »
Hey,

I'm doing a perl script and I need to get multiple values from the command line. Example:
Code: [Select]
perl script.pl --arg1 op1 op2 op3
I'm using Getopt::Long and I can get this to work:
Code: [Select]
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:

Code: [Select]
GetOptions('arg1=f{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:
Code: [Select]
Error in option spec: "arg1=f{3}"
Any ideas / solutions?
Satan911
Evilzone Network Administrator

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: [Perl] Command line arguments with multiple values
« Reply #1 on: May 13, 2012, 01:21:13 pm »
seems to work for me, maybe it's because f{3} expects a float. although I get a different error message:

Code: [Select]
Value "op1" invalid for option arg1 (real number expected)

maybe diff versions with different error messages?

Code: [Select]
~ $ cat test.pl
#!/usr/bin/perl
use Getopt::Long;
GetOptions('arg1=s{3}' => \@a);
print "arg: $_\n" foreach(@a);
~ $ perl test.pl --arg1 op1 op2 op3
arg: op1
arg: op2
arg: op3
~ $ cpan -l 2>/dev/null | grep Getopt
Getopt::Long    2.38
Getopt::Std    1.06

anyway I'd try out that exact code and see if it works, if not idk.

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: [Perl] Command line arguments with multiple values
« Reply #2 on: May 13, 2012, 06:58:15 pm »
Yea I checked the version and I'm also running 2.38 and perl v5. I don't know what the problem but since the feature is still experimental and requires at least 2.35 I probably won't be using it because this is part of a solution for clients and I know damn well most of them don't upgrade most of their servers. Will try to look for an alternative, possible asking the user to separate multiple arguments with , or ;
Satan911
Evilzone Network Administrator