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. ['', '', '']