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 --- lib/Monitoring/Plugin/Getopt.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/Monitoring/Plugin') 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) { -- cgit v1.2.3-74-g34f1