summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeni Golov <evgeni@golov.de>2014-10-03 18:41:51 (GMT)
committerEvgeni Golov <sargentd@die-welt.net>2014-10-03 19:11:37 (GMT)
commitc29f2b286752f844a7659ec38fc22aeb02f0ff45 (patch)
tree08c0a471ccab950bda0660757f5d53bf4935fa03
parentdfae38b656242898ce962c9ad93ed66be45fc3d4 (diff)
downloadmonitoring-plugin-perl-c29f2b286752f844a7659ec38fc22aeb02f0ff45.tar.gz
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
-rw-r--r--lib/Monitoring/Plugin/Getopt.pm4
-rw-r--r--t/Monitoring-Plugin-Getopt-04.t30
2 files changed, 31 insertions, 3 deletions
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
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 (@short, @long); 85 my (@short, @long);
86 for (split /\|/, $opts) { 86 for (split /\|/, $opts) {
87 if (length $_ == 1) { 87 if (length $_ == 1) {
@@ -97,7 +97,7 @@ sub _spec_to_help
97 $help .= '=' . $label; 97 $help .= '=' . $label;
98 } 98 }
99 else { 99 else {
100 $help .= $type eq 'i' ? '=INTEGER' : '=STRING'; 100 $help .= ($type eq 'i' || $type eq '+' || $type =~ /\d+/) ? '=INTEGER' : '=STRING';
101 } 101 }
102 } 102 }
103 elsif ($label) { 103 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 @@
2 2
3use strict; 3use strict;
4 4
5use Test::More tests => 11; 5use Test::More tests => 15;
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
@@ -78,6 +78,30 @@ sub setup
78 [ undef, 'PERCENT%' ], 78 [ undef, 'PERCENT%' ],
79 ); 79 );
80 80
81 # Named args with *optional* but pre-set value
82 $ng->arg(
83 spec => 'dirport|d:9030',
84 help => 'dirport',
85 );
86
87 # Named args with *optional* string value
88 $ng->arg(
89 spec => 'enablesomething|s:s',
90 help => 'something',
91 );
92
93 # Named args with *optional* integer value (same as ":0")
94 $ng->arg(
95 spec => 'testtimeout|T:i',
96 help => 'testtimeout',
97 );
98
99 # Named args with *optional* but increasing integer value
100 $ng->arg(
101 spec => 'verbosity|v:+',
102 help => 'verbosity',
103 );
104
81 return $ng; 105 return $ng;
82} 106}
83 107
@@ -94,4 +118,8 @@ like($@, qr/\n -H, --hostname=ADDRESS\n Hostname\n/, 'hostname ok');
94like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok'); 118like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok');
95like($@, 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'); 119like($@, 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');
96like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok'); 120like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok');
121like($@, qr/\n -d, --dirport=INTEGER/, 'dirport ok');
122like($@, qr/\n -s, --enablesomething=STRING/, 'enablesomething ok');
123like($@, qr/\n -T, --testtimeout=INTEGER/, 'testtimeout ok');
124like($@, qr/\n -v, --verbosity=INTEGER/, 'verbosity ok');
97#print $@; 125#print $@;