implement help
authorDietmar Maurer <dietmar@proxmox.com>
Mon, 4 Jun 2018 08:41:41 +0000 (10:41 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 4 Jun 2018 11:01:49 +0000 (13:01 +0200)
Makefile
PVE/APIClient/Commands/help.pm [new file with mode: 0644]
pveclient

index 4fe1845..c9bbbbe 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,7 @@ install:  pve-api-definition.js
        install -D -m 0644 PVE/APIClient/Config.pm ${LIB_DIR}/PVE/APIClient/Config.pm
        install -D -m 0644 PVE/APIClient/Commands/remote.pm ${LIB_DIR}/PVE/APIClient/Commands/remote.pm
        install -D -m 0644 PVE/APIClient/Commands/lxc.pm ${LIB_DIR}/PVE/APIClient/Commands/lxc.pm
+       install -D -m 0644 PVE/APIClient/Commands/help.pm ${LIB_DIR}/PVE/APIClient/Commands/help.pm
        install -D -m 0644 pve-api-definition.js ${LIB_DIR}/pve-api-definition.js
        install -D -m 0755 pveclient ${DESTDIR}/usr/bin/pveclient
        install -D -m 0644 pveclient.bash-completion ${BASHCOMPLDIR}/pveclient
diff --git a/PVE/APIClient/Commands/help.pm b/PVE/APIClient/Commands/help.pm
new file mode 100644 (file)
index 0000000..076305a
--- /dev/null
@@ -0,0 +1,70 @@
+package PVE::APIClient::Commands::help;
+
+use strict;
+use warnings;
+
+use PVE::APIClient::Commands::help;
+use PVE::APIClient::Commands::lxc;
+use PVE::APIClient::Commands::remote;
+
+use PVE::CLIHandler;
+
+use base qw(PVE::CLIHandler);
+
+__PACKAGE__->register_method ({
+    name => 'help',
+    path => 'help',
+    method => 'GET',
+    description => "Print usage information.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           verbose => {
+               description => "Verbose output - list all options.",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
+           },
+       },
+    },
+    returns => { type => 'null'},
+    code => sub {
+       my ($param) = @_;
+
+       my $text = "USAGE: pveclient <cmd> ...\n\n" if !$param->{verbose};
+
+       my $format = $param->{verbose} ? 'full' : 'short';
+
+       my $assemble_usage_string = sub {
+           my ($subcommand, $def) = @_;
+
+           my $sortfunc = sub { sort keys %{$_[0]} };
+
+           if (ref($def) eq 'HASH') {
+               foreach my $cmd (&$sortfunc($def)) {
+
+                   if (ref($def->{$cmd}) eq 'ARRAY') {
+                       my ($class, $name, $arg_param, $fixed_param) = @{$def->{$cmd}};
+                       $text .= $class->usage_str($name, "pveclient $subcommand $name", $arg_param, $fixed_param, $format);
+                   }
+               }
+           } else {
+               my ($class, $name, $arg_param, $fixed_param) = @$def;
+               $text .= $class->usage_str($name, "pveclient $name", $arg_param, $fixed_param, $format);
+           }
+       };
+
+       $assemble_usage_string->('help', $PVE::APIClient::Commands::help::cmddef);
+       $assemble_usage_string->('lxc', $PVE::APIClient::Commands::lxc::cmddef);
+       $assemble_usage_string->('remote', $PVE::APIClient::Commands::remote::cmddef);
+
+       $text .= "pveclient <get/set/create/delete> <path> {options}\n\n";
+
+       print STDERR $text;
+
+       return undef;
+    }});
+
+our $cmddef = [ __PACKAGE__, 'help', []];
+
+1;
index 03cb8ed..d44f020 100755 (executable)
--- a/pveclient
+++ b/pveclient
@@ -14,23 +14,10 @@ use PVE::APIClient::LWP;
 use PVE::APIClient::Helpers;
 use PVE::APIClient::Commands::remote;
 use PVE::APIClient::Commands::lxc;
+use PVE::APIClient::Commands::help;
 
 use JSON;
 
-sub print_usage {
-
-    my $text = "pveclient usage:\n\n";
-
-    $text .= "pveclient remote <help|add|remove> {options}\n\n";
-
-    $text .= "pveclient lxc <help|create|destroy|...> {options}\n\n";
-
-    $text .= "pveclient <get/set/create/delete> <path> {options}\n\n";
-
-    print STDERR $text;
-
-}
-
 sub call_method {
     my ($path, $method, $args) = @_;
 
@@ -64,9 +51,14 @@ sub call_method {
 my $cli_class_handlers = {
     lxc => 'PVE::APIClient::Commands::lxc',
     remote => 'PVE::APIClient::Commands::remote',
+    help => 'PVE::APIClient::Commands::help',
 };
 
-my $cmd = shift || (print_usage() && exit(-1));
+my $cmd = shift;
+if (!defined($cmd)) {
+    PVE::APIClient::Commands::help->help({});
+    exit(-1);
+}
 
 if ($cmd eq 'get') {
     my $method = 'GET';
@@ -133,7 +125,7 @@ if ($cmd eq 'get') {
     }
     print join("\n", sort(keys %$packages)) . "\n";
 } else {
-    print_usage();
+    PVE::APIClient::Commands::help->help({});
 }
 
 exit(0);