manually push option arguments to array instead of using s@

By mixing s@ for --$foo-hook options and manual pushing in --hook-dir,
it can happen that options get lost. Consider the following test:

use Getopt::Long;
my $arr = [];
GetOptions(
    'A=s@' => \$arr,
    'B=s' => sub { push @{$arr}, $_[1]; }
);
foreach my $hook (@{$arr}) { print "hook: $hook\n"; }

This works fine:

    perl test.pl --A=a1 --B=b1 --A=a2 --B=b2
    hook: a1
    hook: b1
    hook: a2
    hook: b2

This misses b1:

    perl test.pl --B=b1 --A=a2 --B=b2
    hook: a2
    hook: b2
This commit is contained in:
Johannes Schauer Marin Rodrigues 2022-05-24 04:09:41 +02:00
parent 26af846d0a
commit 0664792cd5
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -4284,11 +4284,19 @@ sub main() {
sub { push @{ $options->{noop} }, 'no-merged-usr'; }, sub { push @{ $options->{noop} }, 'no-merged-usr'; },
'force-check-gpg' => 'force-check-gpg' =>
sub { push @{ $options->{noop} }, 'force-check-gpg'; }, sub { push @{ $options->{noop} }, 'force-check-gpg'; },
'setup-hook=s@' => \$options->{setup_hook}, 'setup-hook=s' => sub {
'extract-hook=s@' => \$options->{extract_hook}, push @{ $options->{setup_hook} }, $_[1];
'essential-hook=s@' => \$options->{essential_hook}, },
'customize-hook=s@' => \$options->{customize_hook}, 'extract-hook=s' => sub {
'hook-directory=s' => sub { push @{ $options->{extract_hook} }, $_[1];
},
'essential-hook=s' => sub {
push @{ $options->{essential_hook} }, $_[1];
},
'customize-hook=s' => sub {
push @{ $options->{customize_hook} }, $_[1];
},
'hook-directory=s' => sub {
my ($opt_name, $opt_value) = @_; my ($opt_name, $opt_value) = @_;
if (!-e $opt_value) { if (!-e $opt_value) {
error "hook directory \"$opt_value\" does not exist"; error "hook directory \"$opt_value\" does not exist";