diff options
author | Sven Nierlein <sven@nierlein.org> | 2014-12-28 14:05:08 +0100 |
---|---|---|
committer | Sven Nierlein <sven@nierlein.org> | 2014-12-28 14:05:08 +0100 |
commit | 769c840bbd0bc2f40a5de014eba4ad85008969f7 (patch) | |
tree | 70ad00d3fe0755af4c416ffe0143ff37ed9a0596 | |
parent | 9b52e7531fa4b5b27e03e9d8985d847d81ee1e46 (diff) | |
parent | d17e8369ca11e7816772d074b284933adbba38e4 (diff) | |
download | monitoring-plugin-perl-769c840bbd0bc2f40a5de014eba4ad85008969f7.tar.gz |
Merge pull request #2 from evgeni/getopt_colon_spec
GetOpt::Long optional arguments using a colon instead of an equal sign
-rw-r--r-- | lib/Monitoring/Plugin/Getopt.pm | 18 | ||||
-rw-r--r-- | t/Monitoring-Plugin-Getopt-04.t | 30 |
2 files changed, 43 insertions, 5 deletions
diff --git a/lib/Monitoring/Plugin/Getopt.pm b/lib/Monitoring/Plugin/Getopt.pm index 354f713..db98567 100644 --- a/lib/Monitoring/Plugin/Getopt.pm +++ b/lib/Monitoring/Plugin/Getopt.pm | |||
@@ -81,7 +81,8 @@ 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 (@short, @long); | 86 | my (@short, @long); |
86 | for (split /\|/, $opts) { | 87 | for (split /\|/, $opts) { |
87 | if (length $_ == 1) { | 88 | if (length $_ == 1) { |
@@ -93,11 +94,20 @@ sub _spec_to_help | |||
93 | 94 | ||
94 | my $help = join(', ', @short, @long); | 95 | my $help = join(', ', @short, @long); |
95 | if ($type) { | 96 | if ($type) { |
96 | if ($label) { | 97 | if (!$label) { |
97 | $help .= '=' . $label; | 98 | if ($type eq 'i' || $type eq '+' || $type =~ /\d+/) { |
99 | $label = 'INTEGER'; | ||
100 | } | ||
101 | else { | ||
102 | $label = 'STRING'; | ||
103 | } | ||
104 | } | ||
105 | |||
106 | if ($optional) { | ||
107 | $help .= '[=' . $label . ']'; | ||
98 | } | 108 | } |
99 | else { | 109 | else { |
100 | $help .= $type eq 'i' ? '=INTEGER' : '=STRING'; | 110 | $help .= '=' . $label; |
101 | } | 111 | } |
102 | } | 112 | } |
103 | elsif ($label) { | 113 | elsif ($label) { |
diff --git a/t/Monitoring-Plugin-Getopt-04.t b/t/Monitoring-Plugin-Getopt-04.t index b6345d0..2c91e23 100644 --- a/t/Monitoring-Plugin-Getopt-04.t +++ b/t/Monitoring-Plugin-Getopt-04.t | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use strict; | 3 | use strict; |
4 | 4 | ||
5 | use Test::More tests => 11; | 5 | use Test::More tests => 15; |
6 | BEGIN { use_ok('Monitoring::Plugin::Getopt') }; | 6 | BEGIN { 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'); | |||
94 | like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok'); | 118 | like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok'); |
95 | 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'); | 119 | 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'); |
96 | like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok'); | 120 | like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok'); |
121 | like($@, qr/\n -d, --dirport\[=INTEGER\]/, 'dirport ok'); | ||
122 | like($@, qr/\n -s, --enablesomething\[=STRING\]/, 'enablesomething ok'); | ||
123 | like($@, qr/\n -T, --testtimeout\[=INTEGER\]/, 'testtimeout ok'); | ||
124 | like($@, qr/\n -v, --verbosity\[=INTEGER\]/, 'verbosity ok'); | ||
97 | #print $@; | 125 | #print $@; |