diff options
author | Manfred Stock <mstock@cpan.org> | 2016-11-19 17:54:07 +0100 |
---|---|---|
committer | Manfred Stock <mstock@cpan.org> | 2016-11-19 17:54:07 +0100 |
commit | fac40fb90fc4129704de775786fd3310bad3f8bf (patch) | |
tree | ef2245ad08a29d5cad77b1ce5bdd367b54df6ab7 | |
parent | c79f65a91e58b84629c2c76be49e47fc34e274a6 (diff) | |
download | monitoring-plugin-perl-fac40fb.tar.gz |
Allow negation of command line arguments using '--no'-prefix
Getopt::Long supports negatable boolean options by appending an '!' to the
option specification, so this allows to use this functionality with
Monitoring::Plugin::Getopt as well.
-rw-r--r-- | lib/Monitoring/Plugin/Getopt.pm | 12 | ||||
-rw-r--r-- | t/Monitoring-Plugin-Getopt-01.t | 18 |
2 files changed, 25 insertions, 5 deletions
diff --git a/lib/Monitoring/Plugin/Getopt.pm b/lib/Monitoring/Plugin/Getopt.pm index 106600a..9452058 100644 --- a/lib/Monitoring/Plugin/Getopt.pm +++ b/lib/Monitoring/Plugin/Getopt.pm | |||
@@ -81,14 +81,15 @@ sub _spec_to_help | |||
81 | { | 81 | { |
82 | my ($self, $spec, $label) = @_; | 82 | my ($self, $spec, $label) = @_; |
83 | 83 | ||
84 | my ($opts, $type) = split /=|:/, $spec, 2; | 84 | my ($opts, $type) = split /=|:|!/, $spec, 2; |
85 | my $optional = ($spec =~ m/:/); | 85 | my $optional = ($spec =~ m/:/); |
86 | my $boolean = ($spec =~ m/!/); | ||
86 | my (@short, @long); | 87 | my (@short, @long); |
87 | for (split /\|/, $opts) { | 88 | for (split /\|/, $opts) { |
88 | if (length $_ == 1) { | 89 | if (length $_ == 1) { |
89 | push @short, "-$_"; | 90 | push @short, "-$_"; |
90 | } else { | 91 | } else { |
91 | push @long, "--$_"; | 92 | push @long, $boolean ? "--[no-]$_" : "--$_"; |
92 | } | 93 | } |
93 | } | 94 | } |
94 | 95 | ||
@@ -207,7 +208,7 @@ sub _process_specs_getopt_long | |||
207 | # Setup names and defaults | 208 | # Setup names and defaults |
208 | my $spec = $arg->{spec}; | 209 | my $spec = $arg->{spec}; |
209 | # Use first arg as name (like Getopt::Long does) | 210 | # Use first arg as name (like Getopt::Long does) |
210 | $spec =~ s/[=:].*$//; | 211 | $spec =~ s/[=:!].*$//; |
211 | my $name = (split /\s*\|\s*/, $spec)[0]; | 212 | my $name = (split /\s*\|\s*/, $spec)[0]; |
212 | $arg->{name} = $name; | 213 | $arg->{name} = $name; |
213 | if (defined $self->{$name}) { | 214 | if (defined $self->{$name}) { |
@@ -697,7 +698,8 @@ but basically it is a series of one or more argument names for this argument | |||
697 | (separated by '|'), suffixed with an '=<type>' indicator if the argument | 698 | (separated by '|'), suffixed with an '=<type>' indicator if the argument |
698 | takes a value. '=s' indicates a string argument; '=i' indicates an integer | 699 | takes a value. '=s' indicates a string argument; '=i' indicates an integer |
699 | argument; appending an '@' indicates multiple such arguments are accepted; | 700 | 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: | ||
701 | 703 | ||
702 | =over 4 | 704 | =over 4 |
703 | 705 | ||
@@ -709,6 +711,8 @@ and so on. The following are some examples: | |||
709 | 711 | ||
710 | =item exclude|X=s@ | 712 | =item exclude|X=s@ |
711 | 713 | ||
714 | =item perfdata! | ||
715 | |||
712 | =item verbose|v+ | 716 | =item verbose|v+ |
713 | 717 | ||
714 | =back | 718 | =back |
diff --git a/t/Monitoring-Plugin-Getopt-01.t b/t/Monitoring-Plugin-Getopt-01.t index 36f1f55..5c57df7 100644 --- a/t/Monitoring-Plugin-Getopt-01.t +++ b/t/Monitoring-Plugin-Getopt-01.t | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use strict; | 3 | use strict; |
4 | 4 | ||
5 | use Test::More tests => 76; | 5 | use Test::More tests => 81; |
6 | BEGIN { use_ok('Monitoring::Plugin::Getopt') }; | 6 | BEGIN { use_ok('Monitoring::Plugin::Getopt') }; |
7 | 7 | ||
8 | # Needed to get evals to work in testing | 8 | # Needed to get evals to work in testing |
@@ -35,6 +35,13 @@ sub setup | |||
35 | required => 1, | 35 | required => 1, |
36 | ); | 36 | ); |
37 | 37 | ||
38 | # Add argument - boolean, supporting --no-prefix | ||
39 | $ng->arg( | ||
40 | spec => 'perfdata!', | ||
41 | help => qq(Provide performance data), | ||
42 | default => 1, | ||
43 | ); | ||
44 | |||
38 | return $ng; | 45 | return $ng; |
39 | } | 46 | } |
40 | 47 | ||
@@ -47,6 +54,13 @@ $ng->getopts; | |||
47 | is($ng->warning, 3, 'warning set to 3'); | 54 | is($ng->warning, 3, 'warning set to 3'); |
48 | is($ng->critical, 10, 'critical set to 10'); | 55 | is($ng->critical, 10, 'critical set to 10'); |
49 | is($ng->timeout, 12, 'timeout set to 12'); | 56 | 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'); | ||
50 | 64 | ||
51 | # Check multiple verbose flags | 65 | # Check multiple verbose flags |
52 | @ARGV = qw(-w 3 --critical 10 -v -v -v); | 66 | @ARGV = qw(-w 3 --critical 10 -v -v -v); |
@@ -131,6 +145,7 @@ like($@, qr/--version/, 'help includes default options 1'); | |||
131 | like($@, qr/--verbose/, 'help includes default options 2'); | 145 | like($@, qr/--verbose/, 'help includes default options 2'); |
132 | like($@, qr/--warning/, 'help includes custom option 1'); | 146 | like($@, qr/--warning/, 'help includes custom option 1'); |
133 | like($@, qr/--critical/, 'help includes custom option 2'); | 147 | like($@, qr/--critical/, 'help includes custom option 2'); |
148 | like($@, qr/--\[no-\]perfdata\n/, 'help includes custom option 3'); | ||
134 | unlike($@, qr/Missing arg/, 'no missing arguments'); | 149 | unlike($@, qr/Missing arg/, 'no missing arguments'); |
135 | 150 | ||
136 | @ARGV = ( '--help' ); | 151 | @ARGV = ( '--help' ); |
@@ -146,4 +161,5 @@ like($@, qr/--version/, 'help includes default options 1'); | |||
146 | like($@, qr/--verbose/, 'help includes default options 2'); | 161 | like($@, qr/--verbose/, 'help includes default options 2'); |
147 | like($@, qr/--warning/, 'help includes custom option 1'); | 162 | like($@, qr/--warning/, 'help includes custom option 1'); |
148 | like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args'); | 163 | like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args'); |
164 | like($@, qr/--\[no-\]perfdata\n/, 'help includes custom option 3'); | ||
149 | unlike($@, qr/Missing arg/, 'no missing arguments'); | 165 | unlike($@, qr/Missing arg/, 'no missing arguments'); |