Skip Menu |

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

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

People
Owner: jv [...] cpan.org
Requestors: david.narvaez [...] computer.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 2.42
Fixed in: 2.43



Subject: Getopt::Long Passes Options to Non-Option Handler when pass_through is Enabled
Date: Sat, 25 Jan 2014 11:39:13 -0500
To: bug-Getopt-Long [...] rt.cpan.org
From: David Narvaez <david.narvaez [...] computer.org>
Consider the following minimal example use strict; use Getopt::Long qw(GetOptions :config pass_through); use Data::Dumper; my $foo = 0; my %options; print Dumper(@ARGV); print "-------\n"; GetOptions( 'foo=i' => \$foo, '<>' => sub { my $nonopt = shift; print "Nonopt: ${nonopt}\n"; } ); print "${foo}\n"; print "-------\n"; print Dumper(@ARGV); And the following output: $ perl getopttest.pl --foo 5 --bar 2 test $VAR1 = '--foo'; $VAR2 = '5'; $VAR3 = '--bar'; $VAR4 = '2'; $VAR5 = 'test'; ------- Nonopt: --bar Nonopt: 2 Nonopt: test 5 ------- The option --bar is being passed as a non-option and is being removed from @ARGV, which is against the documentation in two ways: documentation says the <> handler is for non-options (like test in the example) and documentation says that, with pass_through, unknown options are left in the array. My module version is 2.42, my Perl version is perl 5, version 16, subversion 3 (v5.16.3). David E. Narvaez
This a conflict of interest. Since pass_through means "pass everything that is not a valid option", this implies that everything that is not recognized must be passed through. Not just the --things. Using your example: --foo 5 --bar test "--bar" is unrecognized, but "test" could be an argument to "--bar". So both "--bar" and "test" must be shifted back into @ARGV. As a consequence, using "<>" with pass_through is pointless. My suggested fix is to pass everything through and issue an warning when "<>" is used in combination with pass_through. What do you think?
Subject: Re: [rt.cpan.org #92462] Getopt::Long Passes Options to Non-Option Handler when pass_through is Enabled
Date: Sat, 25 Jan 2014 17:56:54 -0500
To: bug-Getopt-Long [...] rt.cpan.org
From: David Narvaez <david.narvaez [...] computer.org>
On Sat, Jan 25, 2014 at 2:57 PM, Johan Vromans via RT <bug-Getopt-Long@rt.cpan.org> wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=92462 > > > This a conflict of interest. > > Since pass_through means "pass everything that is not a valid option", this implies that everything that is not recognized must be passed through. Not just the --things. Using your example: > > --foo 5 --bar test > > "--bar" is unrecognized, but "test" could be an argument to "--bar". So both "--bar" and "test" must be shifted back into @ARGV. > > As a consequence, using "<>" with pass_through is pointless. > > My suggested fix is to pass everything through and issue an warning when "<>" is used in combination with pass_through. > > What do you think?
Thanks for your fast response. I see the issue with knowing whether test is an argument for --bar. Arguably, the documentation says "<>" will receive the non options, so one could say --foo 5 --bar 2 test will be handled as follows: --foo 5 => $foo = 5; --bar => back to $ARGV 2 => non-option handler (because we don't know any better) test => non-option handler and --foo 5 --bar=2 test will be handled as follows: --foo 5 => $foo = 5; --bar=2 => back to $ARGV test => non-option handler But I agree this probably leads to unexpected results and would break the current behavior. So to make rules simple, I think it would be OK to warn when using pass_through and "<>" and in that case passing everything through "<>", but also to update the documentation removing the word non-option from the description of "<>" so to make it clear that everything that is not known is passed through "<>". David E. Narvaez
Since using <> together with pass_through is a programmer mistake, I made it an error instead of a warning.
Added a diagnostic message (fatal) in 2.43.