[monitoring-plugin-perl] format optional arguments in square brakets

Evgeni Golov git at monitoring-plugins.org
Sun Dec 28 14:10:04 CET 2014


 Module: monitoring-plugin-perl
 Branch: master
 Commit: d17e8369ca11e7816772d074b284933adbba38e4
 Author: Evgeni Golov <evgeni at golov.de>
   Date: Fri Oct  3 21:58:31 2014 +0200
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugin-perl/commit/?id=d17e836

format optional arguments in square brakets

---

 lib/Monitoring/Plugin/Getopt.pm | 16 +++++++++++++---
 t/Monitoring-Plugin-Getopt-04.t |  8 ++++----
 2 files changed, 17 insertions(+), 7 deletions(-)

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 $@;



More information about the Commits mailing list