Skip to content

Commit

Permalink
do not leave Getopt::Long reconfigured
Browse files Browse the repository at this point in the history
That is: set back to previous config after being called.
  • Loading branch information
rjbs committed Dec 30, 2024
1 parent f3e06dc commit 6c5f54a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Revision history for Getopt-Long-Descriptive

{{$NEXT}}
- do not leave Getopt::Long configuration in an altered state after
getting options

0.115 2024-11-07 18:00:02+00:00 Europe/Dublin
- cope with the user forgetting the first argument, generally "%c %o",
Expand Down
4 changes: 3 additions & 1 deletion lib/Getopt/Long/Descriptive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,14 @@ sub _build_describe_options {
show_defaults => $arg->{show_defaults},
});

Getopt::Long::Configure(@go_conf);
my $old_go_conf = Getopt::Long::Configure(@go_conf);

my %return;
$usage->die unless GetOptions(\%return, grep { length } @getopt_specs);
my @given_keys = keys %return;

Getopt::Long::Configure($old_go_conf);

for my $opt (keys %return) {
my $newopt = _munge($opt);
next if $newopt eq $opt;
Expand Down
19 changes: 19 additions & 0 deletions t/descriptive.t
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,23 @@ subtest "descriptions for option value types" => sub {
is ($p->('=f%'), ' KEY=NUM...', 'float maps');
};

{
# Asking Getopt::Long::Descriptive to use a specific Getopt::Long config
# should not alter the global state after the describe_options(...) call has
# returned!
ok(!$Getopt::Long::gnu_compat, "Getopt::Long::gnu_compat starts life false");

is_opt(
[ ],
[
[ "foo-bar=i", "foo integer", { default => 17 } ],
{ getopt_conf => [ 'gnu_compat' ] },
],
{ foo_bar => 17 },
"default foo_bar with no short option name",
);

ok(!$Getopt::Long::gnu_compat, "Getopt::Long::gnu_compat still false after getopt");
}

done_testing;

0 comments on commit 6c5f54a

Please sign in to comment.