mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-11 21:51:21 +00:00
Test and fix OptionParser so string opts don't consume the next opt if not string value is given.
This commit is contained in:
@@ -554,12 +554,23 @@ sub _set_option {
|
|||||||
my $long = exists $self->{opts}->{$opt} ? $opt
|
my $long = exists $self->{opts}->{$opt} ? $opt
|
||||||
: exists $self->{short_opts}->{$opt} ? $self->{short_opts}->{$opt}
|
: exists $self->{short_opts}->{$opt} ? $self->{short_opts}->{$opt}
|
||||||
: die "Getopt::Long gave a nonexistent option: $opt";
|
: die "Getopt::Long gave a nonexistent option: $opt";
|
||||||
|
|
||||||
# Reassign $opt.
|
# Reassign $opt.
|
||||||
$opt = $self->{opts}->{$long};
|
$opt = $self->{opts}->{$long};
|
||||||
if ( $opt->{is_cumulative} ) {
|
if ( $opt->{is_cumulative} ) {
|
||||||
$opt->{value}++;
|
$opt->{value}++;
|
||||||
}
|
}
|
||||||
|
elsif ( ($opt->{type} || '') eq 's' && $val =~ m/^--?(.+)/ ) {
|
||||||
|
# https://bugs.launchpad.net/percona-toolkit/+bug/1199589
|
||||||
|
my $next_opt = $1;
|
||||||
|
if ( exists $self->{opts}->{$next_opt}
|
||||||
|
|| exists $self->{short_opts}->{$next_opt} ) {
|
||||||
|
$self->save_error("--$long requires a string value");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$opt->{value} = $val;
|
||||||
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
$opt->{value} = $val;
|
$opt->{value} = $val;
|
||||||
}
|
}
|
||||||
|
@@ -2063,6 +2063,60 @@ is_deeply(
|
|||||||
"set_vars(): var=0 (bug 1182856)"
|
"set_vars(): var=0 (bug 1182856)"
|
||||||
) or diag(Dumper($vars));
|
) or diag(Dumper($vars));
|
||||||
|
|
||||||
|
|
||||||
|
# #############################################################################
|
||||||
|
# https://bugs.launchpad.net/percona-toolkit/+bug/1199589
|
||||||
|
# pt-archiver deletes data despite --dry-run
|
||||||
|
# #############################################################################
|
||||||
|
|
||||||
|
# From the issue: "One problem is that --optimize is not being used correctly:
|
||||||
|
# the option takes an argument: d, s, or ds (see --analyze). The real problem
|
||||||
|
# is that --optimize is consuming the next option, which is --dry-run in this
|
||||||
|
# case. This shouldn't happen; it means the option parser is failing to notice
|
||||||
|
# that --dry-run is not the string val to --optimize but rather an option;
|
||||||
|
# it should catch this and the tool should fail to start with an error like
|
||||||
|
# "--optimize requires a value".
|
||||||
|
|
||||||
|
@ARGV = qw(--optimize --dry-run --ascend-first --where 1=1 --purge --source localhost);
|
||||||
|
$o = new OptionParser(file => "$trunk/bin/pt-archiver");
|
||||||
|
$o->get_specs();
|
||||||
|
$o->get_opts();
|
||||||
|
|
||||||
|
$output = output(
|
||||||
|
sub { $o->usage_or_errors(undef, 1); },
|
||||||
|
);
|
||||||
|
|
||||||
|
like(
|
||||||
|
$output,
|
||||||
|
qr/--optimize requires a string value/,
|
||||||
|
"String opts don't consume the next opt (bug 1199589)"
|
||||||
|
);
|
||||||
|
|
||||||
|
is(
|
||||||
|
$o->get('optimize'),
|
||||||
|
undef,
|
||||||
|
"--optimize didn't consume --dry-run (bug 1199589)"
|
||||||
|
);
|
||||||
|
|
||||||
|
@ARGV = qw(--optimize ds --dry-run --ascend-first --where 1=1 --purge --source localhost);
|
||||||
|
$o->get_opts();
|
||||||
|
|
||||||
|
$output = output(
|
||||||
|
sub { $o->usage_or_errors(undef, 1); },
|
||||||
|
);
|
||||||
|
|
||||||
|
is(
|
||||||
|
$output,
|
||||||
|
'',
|
||||||
|
"String opts still work (bug 1199589)"
|
||||||
|
);
|
||||||
|
|
||||||
|
is(
|
||||||
|
$o->get('optimize'),
|
||||||
|
'ds',
|
||||||
|
"--optimize got its value (bug 1199589)"
|
||||||
|
);
|
||||||
|
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
# Done.
|
# Done.
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
|
Reference in New Issue
Block a user