From fac40fb90fc4129704de775786fd3310bad3f8bf Mon Sep 17 00:00:00 2001 From: Manfred Stock Date: Sat, 19 Nov 2016 17:54:07 +0100 Subject: 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. --- lib/Monitoring/Plugin/Getopt.pm | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'lib/Monitoring') 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 { my ($self, $spec, $label) = @_; - my ($opts, $type) = split /=|:/, $spec, 2; + my ($opts, $type) = split /=|:|!/, $spec, 2; my $optional = ($spec =~ m/:/); + my $boolean = ($spec =~ m/!/); my (@short, @long); for (split /\|/, $opts) { if (length $_ == 1) { push @short, "-$_"; } else { - push @long, "--$_"; + push @long, $boolean ? "--[no-]$_" : "--$_"; } } @@ -207,7 +208,7 @@ sub _process_specs_getopt_long # Setup names and defaults my $spec = $arg->{spec}; # Use first arg as name (like Getopt::Long does) - $spec =~ s/[=:].*$//; + $spec =~ s/[=:!].*$//; my $name = (split /\s*\|\s*/, $spec)[0]; $arg->{name} = $name; if (defined $self->{$name}) { @@ -697,7 +698,8 @@ but basically it is a series of one or more argument names for this argument (separated by '|'), suffixed with an '=' indicator if the argument takes a value. '=s' indicates a string argument; '=i' indicates an integer argument; appending an '@' indicates multiple such arguments are accepted; -and so on. The following are some examples: +appending an '!' indicates negation using '--no'-prefix is possible; and so on. +The following are some examples: =over 4 @@ -709,6 +711,8 @@ and so on. The following are some examples: =item exclude|X=s@ +=item perfdata! + =item verbose|v+ =back -- cgit v1.2.3-74-g34f1