Subject: | When exporting variables, there is no way to distinguish between option not used or specified w/o optional value |
Current behavior:
When exporting the variables, there is no way to know if an option that
accepts an optional value has been used without value at the command
line or it hasn't been used at all.
Here is an example:
use Getopt::Euclid qw(:vars);
...
=item [-]-debug [<log_level>]
Debugging level (default: 'DEBUG')
=For Euclid:
log_level.type: string
log_level.default: 'NOT SET'
...
By running the script with or without --debug (not followed by any
value), $ARGV_debug is set to 'NOT SET'
Desired behavior:
- If the option is not used, it should take the default value;
- If the option is used without any argument, the corresponding variable
should be set to either undef or empty string.
In my case I am able to obtain the correct behavior (setting to empty
string) by adding the following line after line 672:
$entry->{$var} = '' unless defined($ARGV{$arg_name});
Old code (lines 658-675):
elsif ( ref $entry ne 'HASH' && defined $entry ) {
for my $val (
ref $entry eq 'ARRAY'
? @{$entry}
: $entry
)
{
_bad_arglist(
qq{Invalid "$arg_name" argument.\n},
qq{<$var> must be },
$arg_vars->{$var}{constraint_desc},
qq{ but the supplied value ("$val") isn't.}
)
if $arg_vars->{$var}{constraint}
&& !$arg_vars->{$var}{constraint}->($val);
}
next VAR;
}
New code (lines 658-676):
elsif ( ref $entry ne 'HASH' && defined $entry ) {
for my $val (
ref $entry eq 'ARRAY'
? @{$entry}
: $entry
)
{
_bad_arglist(
qq{Invalid "$arg_name" argument.\n},
qq{<$var> must be },
$arg_vars->{$var}{constraint_desc},
qq{ but the supplied value ("$val") isn't.}
)
if $arg_vars->{$var}{constraint}
&& !$arg_vars->{$var}{constraint}->($val);
$entry->{$var} = '' unless
defined($ARGV{$arg_name}); # This is my addition.
}
next VAR;
}
Note that this quick hack could possibly cause troubles when we are
dealing with multiple values.
Probably, it will be easier to guarantee a more coherent behavior once
this section of code will be re-written in a more intelligible way.
Thanks!
Paolo