From c29f2b286752f844a7659ec38fc22aeb02f0ff45 Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Fri, 3 Oct 2014 20:41:51 +0200 Subject: GetOpt::Long optional arguments using a colon instead of an equal sign Instead of writing `foo|f=s` you can also write `foo|f:s` for a GetOpt::Long option spec [1], thus making the argument optional. The current implementation of `_spec_to_help` will wrongly render this as two long options: --dirport, --d:9030 directory port instead of a short and a long one: -d, --dirport=INTEGER directory port This commit fixes the the parsing of the spec, detection of the type and adds tests for a few common cases this could be used in. [1] http://perldoc.perl.org/Getopt/Long.html#Summary-of-Option-Specifications diff --git a/lib/Monitoring/Plugin/Getopt.pm b/lib/Monitoring/Plugin/Getopt.pm index ebdd559..e09ff62 100644 --- a/lib/Monitoring/Plugin/Getopt.pm +++ b/lib/Monitoring/Plugin/Getopt.pm @@ -81,7 +81,7 @@ sub _spec_to_help { my ($self, $spec, $label) = @_; - my ($opts, $type) = split /=/, $spec, 2; + my ($opts, $type) = split /=|:/, $spec, 2; my (@short, @long); for (split /\|/, $opts) { if (length $_ == 1) { @@ -97,7 +97,7 @@ sub _spec_to_help $help .= '=' . $label; } else { - $help .= $type eq 'i' ? '=INTEGER' : '=STRING'; + $help .= ($type eq 'i' || $type eq '+' || $type =~ /\d+/) ? '=INTEGER' : '=STRING'; } } elsif ($label) { diff --git a/t/Monitoring-Plugin-Getopt-04.t b/t/Monitoring-Plugin-Getopt-04.t index b6345d0..9b51883 100644 --- a/t/Monitoring-Plugin-Getopt-04.t +++ b/t/Monitoring-Plugin-Getopt-04.t @@ -2,7 +2,7 @@ use strict; -use Test::More tests => 11; +use Test::More tests => 15; BEGIN { use_ok('Monitoring::Plugin::Getopt') }; # Needed to get evals to work in testing @@ -78,6 +78,30 @@ sub setup [ undef, 'PERCENT%' ], ); + # Named args with *optional* but pre-set value + $ng->arg( + spec => 'dirport|d:9030', + help => 'dirport', + ); + + # Named args with *optional* string value + $ng->arg( + spec => 'enablesomething|s:s', + help => 'something', + ); + + # Named args with *optional* integer value (same as ":0") + $ng->arg( + spec => 'testtimeout|T:i', + help => 'testtimeout', + ); + + # Named args with *optional* but increasing integer value + $ng->arg( + spec => 'verbosity|v:+', + help => 'verbosity', + ); + return $ng; } @@ -94,4 +118,8 @@ like($@, qr/\n -H, --hostname=ADDRESS\n Hostname\n/, 'hostname ok'); like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok'); like($@, qr/\n --disk=BYTES\n Disk limit in BYTES\n --disk=PERCENT%\n Disk limit in PERCENT\n --disk=STRING\n Disk limit in FOOBARS \(Default: 1024\)\n/, 'disk multiline ok'); like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok'); +like($@, qr/\n -d, --dirport=INTEGER/, 'dirport ok'); +like($@, qr/\n -s, --enablesomething=STRING/, 'enablesomething ok'); +like($@, qr/\n -T, --testtimeout=INTEGER/, 'testtimeout ok'); +like($@, qr/\n -v, --verbosity=INTEGER/, 'verbosity ok'); #print $@; -- cgit v0.10-9-g596f From d17e8369ca11e7816772d074b284933adbba38e4 Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Fri, 3 Oct 2014 21:58:31 +0200 Subject: format optional arguments in square brakets diff --git a/lib/Monitoring/Plugin/Getopt.pm b/lib/Monitoring/Plugin/Getopt.pm index e09ff62..3575ce6 100644 --- a/lib/Monitoring/Plugin/Getopt.pm +++ b/lib/Monitoring/Plugin/Getopt.pm @@ -82,6 +82,7 @@ sub _spec_to_help my ($self, $spec, $label) = @_; my ($opts, $type) = split /=|:/, $spec, 2; + my $optional = ($spec =~ m/:/); my (@short, @long); for (split /\|/, $opts) { if (length $_ == 1) { @@ -93,11 +94,20 @@ sub _spec_to_help my $help = join(', ', @short, @long); if ($type) { - if ($label) { - $help .= '=' . $label; + if (!$label) { + if ($type eq 'i' || $type eq '+' || $type =~ /\d+/) { + $label = 'INTEGER'; + } + else { + $label = 'STRING'; + } + } + + if ($optional) { + $help .= '[=' . $label . ']'; } else { - $help .= ($type eq 'i' || $type eq '+' || $type =~ /\d+/) ? '=INTEGER' : '=STRING'; + $help .= '=' . $label; } } elsif ($label) { diff --git a/t/Monitoring-Plugin-Getopt-04.t b/t/Monitoring-Plugin-Getopt-04.t index 9b51883..2c91e23 100644 --- a/t/Monitoring-Plugin-Getopt-04.t +++ b/t/Monitoring-Plugin-Getopt-04.t @@ -118,8 +118,8 @@ like($@, qr/\n -H, --hostname=ADDRESS\n Hostname\n/, 'hostname ok'); like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok'); like($@, qr/\n --disk=BYTES\n Disk limit in BYTES\n --disk=PERCENT%\n Disk limit in PERCENT\n --disk=STRING\n Disk limit in FOOBARS \(Default: 1024\)\n/, 'disk multiline ok'); like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok'); -like($@, qr/\n -d, --dirport=INTEGER/, 'dirport ok'); -like($@, qr/\n -s, --enablesomething=STRING/, 'enablesomething ok'); -like($@, qr/\n -T, --testtimeout=INTEGER/, 'testtimeout ok'); -like($@, qr/\n -v, --verbosity=INTEGER/, 'verbosity ok'); +like($@, qr/\n -d, --dirport\[=INTEGER\]/, 'dirport ok'); +like($@, qr/\n -s, --enablesomething\[=STRING\]/, 'enablesomething ok'); +like($@, qr/\n -T, --testtimeout\[=INTEGER\]/, 'testtimeout ok'); +like($@, qr/\n -v, --verbosity\[=INTEGER\]/, 'verbosity ok'); #print $@; -- cgit v0.10-9-g596f