Add Getopt :: Long Variants In The Hash, Even When Using The Repeat Specifier

Perl Getopt::Long

allows the developer to add their own options to the script. It is also possible to allow multiple values ​​for a parameter by using the repeat specifier as seen in regular expressions. For example:

GetOptions('coordinates=f{2}' => \@coor, 'rgbcolor=i{3}' => \@color);

      

Alternatively, parameter values ​​can be stored in a hash , for example:

my %h = ();
GetOptions(\%h, 'length=i');    # will store in $h{length}

      

What I'm trying to do is combine these two methods to end up with a hash of my options, even when they have multiple values.

As an example, let's say I want to allow three options: birthday (three integers), parents (one or two strings), name (exactly one string). Let's also say that I want to put these values ​​into a hash. I tried the following:

use strict;
use warnings;

use Getopt::Long;
use Data::Dumper;

my %h = ();
GetOptions(\%h, 'bday=i{3}', 'parents=s{1,2}', 'name=s{1}');

print Dumper(\%h);

      

And tested it but the result was as follows:

perl optstest.pl --bday 22 3 1986 --parents john mary --name ellen
$VAR1 = {
    'name' => 'ellen',
    'parents' => 'mary',
    'bday' => 1986
};

      

The hash only uses the last value of each option. However, I would like:

$VAR1 = {
    'name' => 'ellen',
    'parents' => ['mary', 'john'],
    'bday' => [22, 3, 1986]
};

      

If "ellen" is in an array or everything is inside a hash, that will be fine too.

Is it not possible to combine these two functions from Getopt::Long

, i.e. put options in a hash and use duplicate specifiers?

+3


source to share


3 answers


use Getopt::Long;
# enable for debugging purposes
# Getopt::Long::Configure("debug");
use Data::Dumper;

my %h = ();
GetOptions(\%h, 'bday=i{3}', 'parents=s@{1,2}', 'name=s@{1}');

print Dumper(\%h);

      

Is this what you want?



$VAR1 = { 
          'bday' => 1986,
          'name' => [ 
                      'ellen'
                    ],
          'parents' => [ 
                         'john',
                         'mary'
                       ]
        };

      

+6


source


If you want an array, you need to give it a reference to the array.

local @ARGV = qw( --x y z );
my %h = ( x => [] );
GetOptions(\%h, 'x=s{2}');
print(Dumper(\%h));

      

Or you need to specify that you want an array.



local @ARGV = qw( --x y z );
GetOptions(\my %h, 'x=s@{2}');
print(Dumper(\%h));

      

Output:

$VAR1 = {
          'x' => [
                   'y',
                   'z'
                 ]
        };

      

+3


source


The multi-valued options in the section of the documentation you referenced also says this

Warning. An experimental feature follows.

He speaks earlier in

GetOptions (\% h, 'colors = s @'); # will click on @ {$ h {colors}}

so my guess is that the author decided it would work the same way as with duplicate specifiers and that you found a bug

I suggest you inform Perl 5 Porters about this using a utility perlbug

that is part of your Perl installation

+1


source







All Articles