[pve-devel] [RFC zsync 2/2] refactor synopsis handling

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Mar 9 16:55:43 CET 2018


move command synopsis help into the "known command" hash
use that to generalise parameter checking, printing help and
generating POD synopsis

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 pve-zsync | 218 ++++++++++++++++++++++++--------------------------------------
 1 file changed, 85 insertions(+), 133 deletions(-)

diff --git a/pve-zsync b/pve-zsync
index 20b15b2..e9a6320 100755
--- a/pve-zsync
+++ b/pve-zsync
@@ -1010,23 +1010,50 @@ sub disable_job {
 
 my $command = $ARGV[0];
 
-my $commands = {'destroy' => 1,
-		'create' => 1,
-		'sync' => 1,
-		'list' => 1,
-		'status' => 1,
-		'help' => 1,
-		'enable' => 1,
-		'disable' => 1,
-		'printpod' => 1,
-};
-
-if (!$command || !$commands->{$command}) {
-    usage();
-    die "\n";
-}
-
-my $help_sync = <<EOF;
+my $cmd_help = {
+    destroy => qq{
+$PROGNAME destroy -source <string> [OPTIONS]
+
+        remove a sync Job from the scheduler
+
+        -name      string
+
+		name of the sync job, if not set it is default
+
+        -source    string
+
+                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
+    },
+    create => qq{
+$PROGNAME create -dest <string> -source <string> [OPTIONS]
+
+        Create a sync Job
+
+        -dest      string
+
+		the destination target is like [IP]:<Pool>[/Path]
+
+        -limit     integer
+
+		max sync speed in kBytes/s, default unlimited
+
+        -maxsnap   string
+
+		how much snapshots will be kept before get erased, default 1
+
+        -name      string
+
+		name of the sync job, if not set it is default
+
+        -skip      boolean
+
+		if this flag is set it will skip the first sync
+
+        -source    string
+
+		the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+    },
+    sync => qq{
 $PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
 
 	will sync one time
@@ -1055,53 +1082,18 @@ $PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
 	-verbose   boolean
 
 		print out the sync progress.
-EOF
-
-my $help_create = <<EOF;
-$PROGNAME create -dest <string> -source <string> [OPTIONS]
-
-        Create a sync Job
-
-        -dest      string
-
-		the destination target is like [IP]:<Pool>[/Path]
-
-        -limit     integer
-
-		max sync speed in kBytes/s, default unlimited
-
-        -maxsnap   string
-
-		how much snapshots will be kept before get erased, default 1
-
-        -name      string
-
-		name of the sync job, if not set it is default
-
-        -skip      boolean
-
-		if this flag is set it will skip the first sync
-
-        -source    string
-
-		the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
-EOF
-
-my $help_destroy = <<EOF;
-$PROGNAME destroy -source <string> [OPTIONS]
-
-        remove a sync Job from the scheduler
-
-        -name      string
-
-		name of the sync job, if not set it is default
-
-        -source    string
-
-                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
-EOF
-
-my $help_help = <<EOF;
+    },
+    list => qq{
+$PROGNAME list
+
+	Get a List of all scheduled Sync Jobs
+    },
+    status => qq{
+$PROGNAME status
+
+	Get the status of all scheduled Sync Jobs
+    },
+    help => qq{
 $PROGNAME help <cmd> [OPTIONS]
 
 	Get help about specified command.
@@ -1113,21 +1105,8 @@ $PROGNAME help <cmd> [OPTIONS]
 	-verbose   boolean
 
 		Verbose output format.
-EOF
-
-my $help_list = <<EOF;
-$PROGNAME list
-
-	Get a List of all scheduled Sync Jobs
-EOF
-
-my $help_status = <<EOF;
-$PROGNAME status
-
-	Get the status of all scheduled Sync Jobs
-EOF
-
-my $help_enable = <<EOF;
+    },
+    enable => qq{
 $PROGNAME enable -source <string> [OPTIONS]
 
         enable a syncjob and reset error
@@ -1139,9 +1118,8 @@ $PROGNAME enable -source <string> [OPTIONS]
         -source    string
 
                 the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
-EOF
-
-my $help_disable = <<EOF;
+    },
+    disable => qq{
 $PROGNAME disable -source <string> [OPTIONS]
 
         pause a sync job
@@ -1153,57 +1131,42 @@ $PROGNAME disable -source <string> [OPTIONS]
         -source    string
 
                 the source can be an  <VMID> or [IP:]<ZFSPool>[/Path] 
-EOF
+    },
+    printpod => 'internal command',
 
-sub help {
-    my ($command) = @_;
-
-    if ($command eq 'help') {
-	die "$help_help\n";
-
-    } elsif ($command eq 'sync') {
-	die "$help_sync\n";
-
-    } elsif ($command eq 'destroy') {
-	die "$help_destroy\n";
-
-    } elsif ($command eq 'create') {
-	die "$help_create\n";
-
-    } elsif ($command eq 'list') {
-	die "$help_list\n";
-
-    } elsif ($command eq 'status') {
-	die "$help_status\n";
-
-    } elsif ($command eq 'enable') {
-	die "$help_enable\n";
-
-    } elsif ($command eq 'disable') {
-	die "$help_disable\n";
-
-    }
+};
 
+if (!$command) {
+    usage(); die "\n";
+} elsif (!$cmd_help->{$command}) {
+    print "ERROR: unknown command '$command'";
+    usage(1); die "\n";
 }
 
 my @arg = @ARGV;
 my $param = parse_argv(@arg);
 
+sub check_params {
+    for (@_) {
+	die "$cmd_help->{$command}\n" if !$param->{$_};
+    }
+}
+
 if ($command eq 'destroy') {
-    die "$help_destroy\n" if !$param->{source};
+    check_params(qw(source));
 
     check_target($param->{source});
     destroy_job($param);
 
 } elsif ($command eq 'sync') {
-    die "$help_sync\n" if !$param->{source} || !$param->{dest};
+    check_params(qw(source dest));
 
     check_target($param->{source});
     check_target($param->{dest});
     sync($param);
 
 } elsif ($command eq 'create') {
-    die "$help_create\n" if !$param->{source} || !$param->{dest};
+    check_params(qw(source dest));
 
     check_target($param->{source});
     check_target($param->{dest});
@@ -1218,8 +1181,8 @@ if ($command eq 'destroy') {
 } elsif ($command eq 'help') {
     my $help_command = $ARGV[1];
 
-    if ($help_command && $commands->{$help_command}) {
-	print help($help_command);
+    if ($help_command && $cmd_help->{$help_command}) {
+	die "$cmd_help->{$command}\n";
 
     }
     if ($param->{verbose}) {
@@ -1231,13 +1194,13 @@ if ($command eq 'destroy') {
     }
 
 } elsif ($command eq 'enable') {
-    die "$help_enable\n" if !$param->{source};
+    check_params(qw(source));
 
     check_target($param->{source});
     enable_job($param);
 
 } elsif ($command eq 'disable') {
-    die "$help_disable\n" if !$param->{source};
+    check_params(qw(source));
 
     check_target($param->{source});
     disable_job($param);
@@ -1267,6 +1230,9 @@ sub check_target {
 }
 
 sub print_pod {
+
+    my $synopsis = join("\n", sort values %$cmd_help);
+
     print <<EOF;
 =head1 NAME
 
@@ -1276,21 +1242,7 @@ pve-zsync - PVE ZFS Replication Manager
 
 pve-zsync <COMMAND> [ARGS] [OPTIONS]
 
-$help_help
-
-$help_create
-
-$help_destroy
-
-$help_disable
-
-$help_enable
-
-$help_list
-
-$help_status
-
-$help_sync
+$synopsis
 
 =head1 DESCRIPTION
 
-- 
2.14.2





More information about the pve-devel mailing list