Subject: | Getopt::ArgParse copy_args() causing undefined value error in Getopt/ArgParse/Parser.pm line 1184 |
Date: | Wed, 13 Jan 2016 17:39:39 +0000 |
To: | "'bug-Getopt-ArgParse [...] rt.cpan.org'" <bug-Getopt-ArgParse [...] rt.cpan.org> |
From: | "Bannister, Mark" <Mark.Bannister [...] morganstanley.com> |
I've found a bug with this library, specifically to do with the copy_args() method:
use Getopt::ArgParse;
my $ap = Getopt::ArgParse->new_parser(
prog => 'testcmd',
error_prefix => 'testcmd: ',
sortby => 'name',
description => 'Test Getopt::ArgParse.');
$ap->add_arg('--log', type => 'Scalar', help => 'log file');
my $ap2 = Getopt::ArgParse->new_parser(
prog => 'testcopy',
error_prefix => 'testcopy: ',
sortby => 'name',
description => 'Test copying Getopt::ArgParse.');
$ap2->add_arg('--logdir', type => 'Scalar', help => 'log dir');
$ap2->copy_args($ap);
$ap2->parse_args();
When executed the result is:
Can't use an undefined value as an ARRAY reference at //ms/dist/perl5/PROJ/Getopt-ArgParse/1.0.6/lib/perl5/Getopt/ArgParse/Parser.pm line 1184.
On investigation and experimentation, I've discovered that this is because copy_args() copies the default '--help' argument so there are two occurrences of this argument in the resulting '-pristine_add_arguments' hash. One fix would be for copy_args() to avoid duplicating this argument. If I replace the '$ap2->copy_args($ap)' line above with the following:
my @copyargs;
for my $a (@{$ap->{-pristine_add_arguments}}) {
push @copyargs, $a if $a->[0] ne '--help';
}
$ap2->add_args(@copyargs);
The result is as expected:
usage: testcopy [--help|-h] [--log] [--logdir]
Test copying Getopt::ArgParse.
optional named arguments:
--help, -h ? show this help message and exit
--log LOG ? log file
--logdir LOGDIR ? log dir
Would it be possible to add a fix similar to the above in a new version of the library on CPAN?
Best regards,
Mark.
Show quoted text
________________________________
NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or views contained herein are not intended to be, and do not constitute, advice within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and Consumer Protection Act. If you have received this communication in error, please destroy all electronic and paper copies; do not disclose, use or act upon the information; and notify the sender immediately. Mistransmission is not intended to waive confidentiality or privilege. Morgan Stanley reserves the right, to the extent permitted under applicable law, to monitor electronic communications. This message is subject to terms available at the following link: http://www.morganstanley.com/disclaimers If you cannot access these links, please notify us by reply message and we will send the contents to you. By messaging with Morgan Stanley you consent to the foregoing.