2
votes

I have a perl file which basically takes input as follows:

use strict;
use File::Copy;
use File::Path;
use Sys::Hostname;
use Getopt::Long;
use File::Spec;
use File::Compare;
use MIME::Base64;
use Digest::MD5 qw(md5_hex);
my %hmpParams;
my $keybindings;
my $keybindings1;
my $keybindings2;
my $filename;
my $osname;
my $copy="cp";

&parseCommandLineParams();

while ((my $k, my $v) = each %hmpParams ){
        print($k . " = " . $v . "\n");
}


sub parseCommandLineParams(){
GetOptions ( 'bindings:s' => \$hmpParams{bindings},
             'filename:s' => \$hmpParams{filename},
             'key:s' => \$hmpParams{key},
             'bindings1:s' => \$hmpParams{bindings1},
             'bindings2:s' => \$hmpParams{bindings2}
           );
}

I am executing this Perl file as follows:

perl test.pl "-bindings" "+WBAOBtud/UuM7uuCG2T+0ZvoCeuu/x24ovYkwjI2YM=" "-filename" "/tmp/keyMapperFile_2016-08-24-04:43:06" "-key" "avqijvmlf5ipq_5j0038opvhqmh_28jm8d913ptv0_3ie1ctia2cdqd" "-bindings1" "" "-bindings2" ""

Output:

Unknown option: wbaobtud/uum7uucg2t+0zvoceuu/x24ovykwji2ym
bindings2 = 
filename = /tmp/keyMapperFile_2016-08-24-04:43:06
bindings = 
bindings1 = 
key = avqijvmlf5ipq_5j0038opvhqmh_28jm8d913ptv0_3ie1ctia2cdqd

I see that whenever there is a preceding + sign in the value of an option it does not recognizes it as value but instead treats it as option. How do I get around this error? What is the reason behind this?

2
Remove double quotes from param names. Like perl test.pl --bindings "+WBAOBtud/UuM7uuCG2T+0ZvoCeuu/x24ovYkwjI2YM=" --filename ... - Chankey Pathak
@ChankeyPathak that will have no effect. (they will be removed by the shell before being passed to the script). - Drav Sloan
Ah I see. Thanks! - Chankey Pathak

2 Answers

5
votes

You need to disable the getopt_compat option to prevent leading + from being treated as equivalent to --:

getopt_compat
            Allow "+" to start options. Default is enabled unless
            environment variable POSIXLY_CORRECT has been set, in which
            case "getopt_compat" is disabled.

as in

use Getopt::Long qw(:config no_getopt_compat);

or

use Getopt::Long;
Getopt::Long::Configure("no_getopt_compat");
1
votes

The underlying problem is that you used :s when you meant to use =s. Since you said it's perfectly valid to omit --bindings's argument, and since + can be a valid option start, it thinks you mean to use --bindings with no argument.

Fix:

sub parseCommandLineParams {
    GetOptions(
        'bindings=s'  => \$hmpParams{bindings},
        'bindings1=s' => \$hmpParams{bindings1},
        'bindings2=s' => \$hmpParams{bindings2},
        'filename=s'  => \$hmpParams{filename},
        'key=s'       => \$hmpParams{key},
    )
        or die("usage\n");
}

or

sub parseCommandLineParams {
    GetOptions(
        \%hmpParams,
        'bindings=s',
        'bindings1=s',
        'bindings2=s',
        'filename=s',
        'key=s',
    )
        or die("usage\n");
}

By the way, I also use Getopt::Long in POSIX-compliance mode by using something like the following:

use Getopt::Long qw( :config posix_default );

In this mode, + is not considered the start of an option.

perl test.pl                                                      \
    --bindings +WBAOBtud/UuM7uuCG2T+0ZvoCeuu/x24ovYkwjI2YM=       \
    --filename /tmp/keyMapperFile_2016-08-24-04:43:06             \
    --key avqijvmlf5ipq_5j0038opvhqmh_28jm8d913ptv0_3ie1ctia2cdqd \
    --bindings1 ''                                                \
    --bindings2 ''