Skip Menu |

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

Report information
The Basics
Id: 61039
Status: rejected
Priority: 0/
Queue: Getopt-Euclid

People
Owner: kgalinsky [...] gmail.com
Requestors: paolo [...] medeo.net
Cc:
AdminCc:

Bug Information
Severity: Critical
Broken in: 0.2.1
Fixed in: (no value)



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
Fix is now committed in the repository. Attached is the patch; Paolo, in the future, can you please submit a patch file rather than writing out the code in the comment? Thanks, -Kevin
Subject: euclid.patch
diff --git a/lib/Getopt/Euclid.pm b/lib/Getopt/Euclid.pm index 22919f4..a1f834c 100644 --- a/lib/Getopt/Euclid.pm +++ b/lib/Getopt/Euclid.pm @@ -670,6 +670,8 @@ sub _verify_args { ) if $arg_vars->{$var}{constraint} && !$arg_vars->{$var}{constraint}->($val); + $entry->{$var} = '' + unless defined( $ARGV{$arg_name} ); } next VAR; }
Should have tested this before releasing, but the supplied correct did not fix the issue. Does not look like it broke anything though. Have a branch for this bug in the git repository, putting aside for now.
I'm going to have to reject this bug. Looking through the documentation again, it is clear that this behavior is not what is expected. See the section on "Placeholder defaults": "You can also specify a default value for any placeholders that aren't given values on the command-line (either because their argument isn't provided at all, or because the placeholder is optional within the argument)."