Skip to content

Commit c039436

Browse files
authored
Merge pull request #13 from mstock/feature/argument-negation
Allow negation of command line arguments using '--no'-prefix
2 parents c79f65a + fac40fb commit c039436

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

lib/Monitoring/Plugin/Getopt.pm

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,15 @@ sub _spec_to_help
8181
{
8282
my ($self, $spec, $label) = @_;
8383

84-
my ($opts, $type) = split /=|:/, $spec, 2;
84+
my ($opts, $type) = split /=|:|!/, $spec, 2;
8585
my $optional = ($spec =~ m/:/);
86+
my $boolean = ($spec =~ m/!/);
8687
my (@short, @long);
8788
for (split /\|/, $opts) {
8889
if (length $_ == 1) {
8990
push @short, "-$_";
9091
} else {
91-
push @long, "--$_";
92+
push @long, $boolean ? "--[no-]$_" : "--$_";
9293
}
9394
}
9495
@@ -207,7 +208,7 @@ sub _process_specs_getopt_long
207208
# Setup names and defaults
208209
my $spec = $arg->{spec};
209210
# Use first arg as name (like Getopt::Long does)
210-
$spec =~ s/[=:].*$//;
211+
$spec =~ s/[=:!].*$//;
211212
my $name = (split /\s*\|\s*/, $spec)[0];
212213
$arg->{name} = $name;
213214
if (defined $self->{$name}) {
@@ -697,7 +698,8 @@ but basically it is a series of one or more argument names for this argument
697698
(separated by '|'), suffixed with an '=<type>' indicator if the argument
698699
takes a value. '=s' indicates a string argument; '=i' indicates an integer
699700
argument; appending an '@' indicates multiple such arguments are accepted;
700-
and so on. The following are some examples:
701+
appending an '!' indicates negation using '--no'-prefix is possible; and so on.
702+
The following are some examples:
701703
702704
=over 4
703705
@@ -709,6 +711,8 @@ and so on. The following are some examples:
709711
710712
=item exclude|X=s@
711713
714+
=item perfdata!
715+
712716
=item verbose|v+
713717
714718
=back

t/Monitoring-Plugin-Getopt-01.t

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use strict;
44

5-
use Test::More tests => 76;
5+
use Test::More tests => 81;
66
BEGIN { use_ok('Monitoring::Plugin::Getopt') };
77

88
# Needed to get evals to work in testing
@@ -35,6 +35,13 @@ sub setup
3535
required => 1,
3636
);
3737

38+
# Add argument - boolean, supporting --no-prefix
39+
$ng->arg(
40+
spec => 'perfdata!',
41+
help => qq(Provide performance data),
42+
default => 1,
43+
);
44+
3845
return $ng;
3946
}
4047

@@ -47,6 +54,13 @@ $ng->getopts;
4754
is($ng->warning, 3, 'warning set to 3');
4855
is($ng->critical, 10, 'critical set to 10');
4956
is($ng->timeout, 12, 'timeout set to 12');
57+
is($ng->perfdata, 1, 'perfdata set to default of 1');
58+
59+
# Disable perfdata
60+
@ARGV = qw(--critical 10 --no-perfdata);
61+
$ng = setup;
62+
$ng->getopts;
63+
is($ng->perfdata, 0, 'perfdata set to 0');
5064

5165
# Check multiple verbose flags
5266
@ARGV = qw(-w 3 --critical 10 -v -v -v);
@@ -131,6 +145,7 @@ like($@, qr/--version/, 'help includes default options 1');
131145
like($@, qr/--verbose/, 'help includes default options 2');
132146
like($@, qr/--warning/, 'help includes custom option 1');
133147
like($@, qr/--critical/, 'help includes custom option 2');
148+
like($@, qr/--\[no-\]perfdata\n/, 'help includes custom option 3');
134149
unlike($@, qr/Missing arg/, 'no missing arguments');
135150

136151
@ARGV = ( '--help' );
@@ -146,4 +161,5 @@ like($@, qr/--version/, 'help includes default options 1');
146161
like($@, qr/--verbose/, 'help includes default options 2');
147162
like($@, qr/--warning/, 'help includes custom option 1');
148163
like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args');
164+
like($@, qr/--\[no-\]perfdata\n/, 'help includes custom option 3');
149165
unlike($@, qr/Missing arg/, 'no missing arguments');

0 commit comments

Comments
 (0)