Darius
Darius

Reputation: 155

Getopt::Long GetOptions error in option spec

I'm getting an error with the following script:

my $h="";
my $r="";
my $i="";
my $n="";
my $forks="";
my $global="";
my $v="";

GetOptions ("h" => \$h, #print help
            "r" => \$r, #keep all temp folder
            "i" => \$i, #include additional config file
            "n" => \$n, #do not merge temp files
            "forks=f" => \$forks, #number of forks you want to use
            "global=g" => \$global, #special config line option
             "v" => \$v #verbose option
            );

The error is:

"Error in option spec: global=g".

Does anyone know a solution?

Upvotes: 1

Views: 2867

Answers (2)

Seki
Seki

Reputation: 11465

If you were looking to specify the short for of the option like -f as an alias to --forks, you need to put it after a pipe: "forks|f"

By the way, it is simpler to provide a hashref to GetOptions instead of individual variables. It will be easier to add new options. And if you need some default values, just put it in the hash before calling GetOptions.

When specifying aliases, the hash key is the first alternative for the option.

use warnings;
use strict;
use Getopt::Long;

my %args = ( verbose => 1 ); # say verbose is on by default

GetOptions (\%args,
        "help|h",     #print help
        "r",          #keep all temp folder
        "i",          #include additional config file
        "n",          #do not merge temp files
        "forks|f=i",  #number of forks you want to use
        "global|g=s", #special config line option
         "verbose|v"  #verbose option
        );
print_help() if $args{help};
print "working on $args{forks}" if $args{verbose};

Upvotes: 0

toolic
toolic

Reputation: 62037

g is not one of the supported option argument types. Using the "string" type, s, fixes the error:

use warnings;
use strict;
use Getopt::Long;

my $h="";
my $r="";
my $i="";
my $n="";
my $forks="";
my $global="";
my $v="";

GetOptions ("h" => \$h, #print help
            "r" => \$r, #keep all temp folder
            "i" => \$i, #include additional config file
            "n" => \$n, #do not merge temp files
            "forks=f" => \$forks, #number of forks you want to use
            "global=s" => \$global, #special config line option
             "v" => \$v #verbose option
            );

Refer to Summary of Option Specifications

Upvotes: 2

Related Questions