Skip Menu |

This queue is for tickets about the Getopt-Long CPAN distribution.

Report information
The Basics
Id: 116124
Status: resolved
Priority: 0/
Queue: Getopt-Long

People
Owner: Nobody in particular
Requestors: ALTREUS [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: Repeatable optional value doesn't do anything at all
I'm not sure that this is a bug, but it's a situation I found myself in, and it seems like it could be supported. I want to support an option that, if present, will activate behaviour; but if present with a value (or many values), will provide parameters to that behaviour. My example is, I have a script that outputs environment information. By default it outputs PERL5LIB, but I would like it to optionally output PATH. By default, PATH is bin/ or script/, but I want the user to be able to specify their own directories too. So: --path Also output PATH, by default bin/ and script/ dirs --path=dir --path=dir2 Like path, but uses dir/ and dir2/ instead of bin/ and script/ I tried two things: first, put it in an array. GetOptions( 'path:s' => \my @paths ); @paths = () # --path @paths = ( 'dir', 'dir2' ) # --path=dir --path=dir It occurred to me I wouldn't know the difference between zero occurrences and one occurrence with no value (I half expected the empty string in $array[0]). Second, I tried putting it in an arrayref. It's this latter I expected to do something. GetOptions( 'path:s@' => \my $paths ); $paths = undef # --path $paths = [ 'dir', 'dir2' ] # --path=dir --path=dir I would anticipate that when the option is provided once but with no value, one would get [] or [undef] or ['']. The latter makes most sense because multiple instances *might* be useful for someone else, e.g. ['', '', '']
On Wed Jul 13 17:23:32 2016, ALTREUS wrote: Show quoted text
> > GetOptions( > 'path:s@' => \my $paths > ); > > $paths = undef # --path > $paths = [ 'dir', 'dir2' ] # --path=dir --path=dir >
Indeed, $paths is set to undef, even if you initialise it to an empty arrayref: my $paths = []; GetOptions( 'path:s@' => \$paths ); $paths = [] # no option $paths = undef # --path $paths = [ 'dir', 'dir2' ] # --path=dir --path=dir
Another discovery: if you do it with a hashref, you always get an array ref, even if you don't provide --path. GetOptions( \my %options, 'path:s@' ); $options{path} = [] # no options $options{path} = [] # --path $options{path} = ['dir', 'dir2'] # --path=dir --path=dir2
This is actually a regression. In 2.45 this does exactly what I expected in the first place #!/usr/bin/env perl use strict; use warnings; use 5.014; use Getopt::Long qw(:config gnu_getopt passthrough); my %options; GetOptions( \%options, 'foo!', 'path:s@' ); use Data::Dump; dd \%options; altreus@lovelace ~ $ perl options.pl --path=foo { path => ["foo"] } altreus@lovelace ~ $ perl options.pl --path { path => [""] } altreus@lovelace ~ $ perl options.pl {}
Seems to be solved.