Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Getopt::Long defaults for options with multiple values

by PetaMem (Priest)
on Nov 21, 2014 at 12:23 UTC ( [id://1108001]=perlquestion: print w/replies, xml ) Need Help??

PetaMem has asked for the wisdom of the Perl Monks concerning the following question:

Reviewing some middle-aged code, I stumbled across this topic...

Unfortunately, the G:L documentation is silent about default values for options with multiple values: Options-with-multiple-values of Getopt::Long. Even more unfortunate seems an inconsistency compared to defaults for options with single values and maybe a semantic inconsistency at all. If you have a single value option, you may define a default like:

my $tag = 'foo'; # option variable with default value GetOptions ('tag=s' => \$tag);

This works as expected. Good. You can - of course - do a similar thing for options that take multiple values:

my $listref = ['a','b','c']; # option variable with default valu +es GetOptions ('list=s{,}' => $listref);

If you omit the -list option, the program will have the default value, which is good. If you, however, will give a list option, G:L seems to push that option to the list already given in default, which may have its applications, but is not that great as default behavior. If you want to actually replace the default given, you would have to define defaults the ugly, backward and programmatically DIY way:

my $listref = []; # no default GetOptions ('list=s{,}' => $listref); my $listref = @{$listref} ? $listref : ['a', 'b', 'c']; # DIY default

Which is actually code I see right now. Yuck! That can't be right - can it?

Bye
 PetaMem
    All Perl:   MT, NLP, NLU

Replies are listed 'Best First'.
Re: Getopt::Long defaults for options with multiple values
by fishmonger (Chaplain) on Nov 21, 2014 at 15:30 UTC

    You can use a subroutine reference and pass a comma separated list to accomplish your goal.

    my @list = qw( a b c ); GetOptions ('list=s{,}' => \&list); print Dumper \@list; sub list { # action at a distance # modifies the file scoped @list array @list = split /,/, $_[1]; }

    test without passing any arg:

    C:\test>list_test.pl $VAR1 = [ 'a', 'b', 'c' ];

    test passing arg list:

    C:\test>list_test.pl -list d,s,4,f,"test string" $VAR1 = [ 'd', 's', '4', 'f', 'test string' ];

Re: Getopt::Long defaults for options with multiple values
by toolic (Bishop) on Nov 21, 2014 at 13:26 UTC
    Unfortunately, the G:L documentation is silent about default values for options with multiple values
    If you believe the documentation can be improved, please do so via perlbug. Also note this:
    Warning: What follows is an experimental feature.
Re: Getopt::Long defaults for options with multiple values
by Anonymous Monk on Nov 21, 2014 at 13:13 UTC

    I don't have any suggestion; only want to note that working with an array is ever so slightly less bothersome than working with an array reference.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1108001]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-16 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found