Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: GetOpts::long Multiple Parameters per option

by broomduster (Priest)
on Feb 06, 2011 at 23:42 UTC ( #886572=note: print w/ replies, xml ) Need Help??


in reply to GetOpts::long Multiple Parameters per option

I think you're making this too complicated. Check the docs for Getopt::Long. The last part of the section Options with multiple values shows how to do what you want.

use feature qw(say); use strict; use warnings; use Getopt::Long; my( @opt_a, @opt_b ); GetOptions( 'a=s{2}' => \@opt_a, 'b=s{2}' => \@opt_b, ); say "@opt_a"; say "@opt_b";
On the command line:
./pm-886569 -a a1 a2 -b b1 b2 a1 a2 b1 b2
This example specifies exactly two arguments/parameters for each option, but note that you can specify a range for the number of options, if needed.


Comment on Re: GetOpts::long Multiple Parameters per option
Select or Download Code
Re^2: GetOpts::long Multiple Parameters per option
by PyrexKidd (Monk) on Feb 07, 2011 at 01:26 UTC

    Ok, I see what you mean.
    how about this:

    #!/usr/bin/perl use v5.10.1; use strict; use warnings; use Getopt::Long; my (@a, @b); my %options = ( 'a' => \@a, 'b' => \@b, ); GetOptions ( 'a=s{2}' => \@a, 'b=s{2}' => \@b, ); say "$options{'a'}->[0], $options{'a'}->[1]"; say "$options{'b'}->[0], $options['b'}->[1]";

    the only problem I have with this is defining parameters for %options before building %options with GetOptions
    If for instance I needed to have multiple pairs perl flag, i.e.:

    $./program -a a1_1 a1_2 -a a2_1 a2_2 -a a3_1 a3_2
    Thanks for the assist.

      the only problem I have with this is defining parameters for %options before building %options with GetOptions If for instance I needed to have multiple pairs per flag ...
      It still works if you handle it properly:
      #!/usr/bin/perl use v5.10.1; use strict; use warnings; use Getopt::Long; my (@a, @b); my %options = ( 'a' => \@a, 'b' => \@b, ); GetOptions ( 'a=s{2}' => \@a, 'b=s{2}' => \@b, ); # say "$options{'a'}->[0], $options{'a'}->[1]"; # say "$options{'b'}->[0], $options{'b'}->[1]"; say "@{ $options{'a'} }"; say "@{ $options{'b'} }";

      Firstly, don't dereference the array elements one at a time. Among other things, you get

      Use of uninitialized value in concatenation (.) or string at ./pm-886582 line 20.
      when there is no -b flag and args. But more important, you've hidden the fact that you really collected all of the arguments from all of the -a flags.

      Now you will see that:

      ./pm-886582 -a a1_1 a1_2 -a a2_1 a2_2 -a a3_1 a3_2 a1_1 a1_2 a2_1 a2_2 a3_1 a3_2
      (note that the output has an empty line where non-existent @{ $options{'b'} } is not printed.)

      IOW, all of the -a pairs are gathered into your @{ $options{'a'} }. All you need now is some code to check that the array has an even number of elements and then to take it apart into (multiple) pairs.

      Thanks for the assist.
      My pleasure. One last word of caution. The docs mention that this is an experimental feature. I haven't had any problems with it so far, but YMMV.
Re^2: GetOpts::long Multiple Parameters per option
by thargas (Chaplain) on Feb 07, 2011 at 16:48 UTC

    I always like learning new things; I wasn't aware of this Getopt::Long feature.

    However, I prefer a different way of setting defaults, which mixes well with this:

    use Getopt::Long; my $opt = { grot => [qw( a bb ccc )], }; GetOptions( $opt, 'grot=s@{2}', ) or die "can't parse options";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://886572]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (15)
As of 2014-07-11 14:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (227 votes), past polls