views:

177

answers:

3

I am parsing command line options in Perl using Getopt::Long. I am forced to use prefix - (one dash) for short commands (-s) and -- (double dash) for long commands (e.g., --input=file).

My problem is that there is one special option (-r=<pattern>) so it is long option for its requirement for argument, but it has to have one dash (-) prefix not double dash (--) like other long options. Is possible to setup Getopt::Long to accept these?

+3  A: 

Are you setting "bundling" on?

If so, you can disable bundling (but then, you won't be able to do things like use myprog -abc instead of myprog -a -b -c).

Otherwise, the only thing that comes to mind right now is to use Argument Callback (<>) and manually parse that option.

DVK
+5  A: 

By default, Getopt::Long interchangeably accepts either single (-) or double dash (--). So, you can just use --r=foo. Do you get any error when you try that?

use strict;
use warnings;
use Getopt::Long;
my $input = 2;
my $s = 0;
my $r = 3;
GetOptions(
    'input=s' => \$input,
    's'       => \$s,
    'r=s'     => \$r,
);
print "input=$input\n";
print "s=$s\n";
print "r=$r\n";

These sample command lines produce the same results:

my_program.pl --r=5
my_program.pl --r 5
my_program.pl  -r=5
my_program.pl  -r 5

input=2
s=0
r=5
toolic
A: 
#!/usr/bin/perl

use strict; use warnings;

use Getopt::Long;

my $pattern;

GetOptions('r=s' => \$pattern);

print $pattern, "\n";

Output:

C:\Temp> zz -r=/test/
/test/
C:\Temp> zz -r /test/
/test/

Am I missing something?

Sinan Ünür
I think what he describes might be due to bundling being on?
DVK
@DVK That is likely the case. I did not think of that.
Sinan Ünür