summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/Monitoring/Plugin/Getopt.pm12
-rw-r--r--t/Monitoring-Plugin-Getopt-01.t18
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
698takes a value. '=s' indicates a string argument; '=i' indicates an integer 699takes a value. '=s' indicates a string argument; '=i' indicates an integer
699argument; appending an '@' indicates multiple such arguments are accepted; 700argument; appending an '@' indicates multiple such arguments are accepted;
700and so on. The following are some examples: 701appending an '!' indicates negation using '--no'-prefix is possible; and so on.
702The 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
3use strict; 3use strict;
4 4
5use Test::More tests => 76; 5use Test::More tests => 81;
6BEGIN { use_ok('Monitoring::Plugin::Getopt') }; 6BEGIN { 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;
47is($ng->warning, 3, 'warning set to 3'); 54is($ng->warning, 3, 'warning set to 3');
48is($ng->critical, 10, 'critical set to 10'); 55is($ng->critical, 10, 'critical set to 10');
49is($ng->timeout, 12, 'timeout set to 12'); 56is($ng->timeout, 12, 'timeout set to 12');
57is($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;
63is($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');
131like($@, qr/--verbose/, 'help includes default options 2'); 145like($@, qr/--verbose/, 'help includes default options 2');
132like($@, qr/--warning/, 'help includes custom option 1'); 146like($@, qr/--warning/, 'help includes custom option 1');
133like($@, qr/--critical/, 'help includes custom option 2'); 147like($@, qr/--critical/, 'help includes custom option 2');
148like($@, qr/--\[no-\]perfdata\n/, 'help includes custom option 3');
134unlike($@, qr/Missing arg/, 'no missing arguments'); 149unlike($@, 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');
146like($@, qr/--verbose/, 'help includes default options 2'); 161like($@, qr/--verbose/, 'help includes default options 2');
147like($@, qr/--warning/, 'help includes custom option 1'); 162like($@, qr/--warning/, 'help includes custom option 1');
148like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args'); 163like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args');
164like($@, qr/--\[no-\]perfdata\n/, 'help includes custom option 3');
149unlike($@, qr/Missing arg/, 'no missing arguments'); 165unlike($@, qr/Missing arg/, 'no missing arguments');