]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIHandler.pm
followup: use defined to check if a hash element can be accessed
[pve-common.git] / src / PVE / CLIHandler.pm
index 6eab3c62bbf8dda07ebc0ed1eed216cb8d288a6e..2f607cd2a59dce7f4dd7771884b25712d753beed 100644 (file)
@@ -17,7 +17,9 @@ use base qw(PVE::RESTHandler);
 # A real command is always an array consisting of its class, name, array of
 # position fixed (required) parameters and hash of predefined parameters when
 # mapping a CLI command t o an API call. Optionally an output method can be
-# passed at the end, e.g., for formatting or transformation purpose.
+# passed at the end, e.g., for formatting or transformation purpose, and
+# a schema of additional properties/arguments which are added to
+# the method schema and gets passed to the formatter.
 #
 # [class, name, fixed_params, API_pre-set params, output_sub ]
 #
@@ -200,12 +202,12 @@ sub generate_usage_str {
            foreach my $cmd (&$sortfunc($def)) {
 
                if (ref($def->{$cmd}) eq 'ARRAY') {
-                   my ($class, $name, $arg_param, $fixed_param) = @{$def->{$cmd}};
+                   my ($class, $name, $arg_param, $fixed_param, undef, $formatter_properties) = @{$def->{$cmd}};
 
                    $str .= $separator if $oldclass && $oldclass ne $class;
                    $str .= $indent;
                    $str .= $class->usage_str($name, "$prefix $cmd", $arg_param,
-                                             $fixed_param, $format, $param_cb);
+                                             $fixed_param, $format, $param_cb, $formatter_properties);
                    $oldclass = $class;
 
                } elsif (defined($def->{$cmd}->{alias}) && ($format eq 'asciidoc')) {
@@ -215,7 +217,7 @@ sub generate_usage_str {
                } else {
                    next if $def->{$cmd}->{alias};
 
-                   my $substr = $generate->($indent, $separator, $def->{$cmd}, "$prefix $cmd");
+                   my $substr = $generate->($indent, '', $def->{$cmd}, "$prefix $cmd");
                    if ($substr) {
                        $substr .= $separator if $substr !~ /\Q$separator\E{2}/;
                        $str .= $substr;
@@ -224,11 +226,11 @@ sub generate_usage_str {
 
            }
        } else {
-           my ($class, $name, $arg_param, $fixed_param) = @$def;
+           my ($class, $name, $arg_param, $fixed_param, undef, $formatter_properties) = @$def;
            $abort->("unknown command '$cmd'") if !$class;
 
            $str .= $indent;
-           $str .= $class->usage_str($name, $prefix, $arg_param, $fixed_param, $format, $param_cb);
+           $str .= $class->usage_str($name, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties);
        }
        return $str;
     };
@@ -333,9 +335,9 @@ sub print_usage_short {
     $assert_initialized->();
 
     print $fd "ERROR: $msg\n" if $msg;
-    print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
+    print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
 
-    print {$fd} generate_usage_str('short', $cmd, ' ' x 7, "\n", sub {
+    print {$fd} generate_usage_str('short', $cmd, ' ' x 7, $cmd ? '' : "\n", sub {
        my ($h) = @_;
        return sort {
            if (ref($h->{$a}) eq 'ARRAY' && ref($h->{$b}) eq 'ARRAY') {
@@ -395,7 +397,7 @@ my $print_bash_completion = sub {
 
     my $skip_param = {};
 
-    my ($class, $name, $arg_param, $uri_param) = @$def;
+    my ($class, $name, $arg_param, $uri_param, undef, $formatter_properties) = @$def;
     $arg_param //= [];
     $uri_param //= {};
 
@@ -406,7 +408,8 @@ my $print_bash_completion = sub {
 
     my $info = $class->map_method_by_name($name);
 
-    my $prop = $info->{parameters}->{properties};
+    my $prop = { %{$info->{parameters}->{properties}} }; # copy
+    $prop = { %$prop, %$formatter_properties } if $formatter_properties;
 
     my $print_parameter_completion = sub {
        my ($pname) = @_;
@@ -487,6 +490,30 @@ complete -o default -C '$exename bashcomplete' $exename
 __EOD__
 }
 
+sub generate_zsh_completions {
+    my ($class) = @_;
+
+    # generate zsh completion config
+
+    $exename = &$get_exe_name($class);
+
+    print <<__EOD__;
+#compdef _$exename $exename
+
+function _$exename() {
+    local cwords line point cmd curr prev
+    cwords=\${#words[@]}
+    line=\$words
+    point=\${#line}
+    cmd=\${words[1]}
+    curr=\${words[cwords]}
+    prev=\${words[cwords-1]}
+    compadd \$(COMP_CWORD="\$cwords" COMP_LINE="\$line" COMP_POINT="\$point" \\
+       $exename bashcomplete "\$cmd" "\$curr" "\$prev")
+}
+__EOD__
+}
+
 sub generate_asciidoc_synopsys {
     my ($class) = @_;
     $class->generate_asciidoc_synopsis();
@@ -547,21 +574,22 @@ my $handle_cmd  = sub {
 
     &$preparefunc() if $preparefunc;
 
-    my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def || []};
+    my ($class, $name, $arg_param, $uri_param, $outsub, $formatter_properties) = @{$def || []};
     $abort->("unknown command '$cmd_str'") if !$class;
 
-    my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_cb);
+    my ($res, $formatter_params) = $class->cli_handler(
+       $cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_cb, $formatter_properties);
 
     if (defined $outsub) {
        my $result_schema = $class->map_method_by_name($name)->{returns};
-       $outsub->($res, $result_schema);
+       $outsub->($res, $result_schema, $formatter_params);
     }
 };
 
 my $handle_simple_cmd = sub {
     my ($args, $preparefunc, $param_cb) = @_;
 
-    my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef};
+    my ($class, $name, $arg_param, $uri_param, $outsub, $formatter_properties) = @{$cmddef};
     die "no class specified" if !$class;
 
     if (scalar(@$args) >= 1) {
@@ -588,11 +616,12 @@ my $handle_simple_cmd = sub {
 
     &$preparefunc() if $preparefunc;
 
-    my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $param_cb);
+    my ($res, $formatter_params) = $class->cli_handler(
+       $name, $name, \@ARGV, $arg_param, $uri_param, $param_cb, $formatter_properties);
 
     if (defined $outsub) {
        my $result_schema = $class->map_method_by_name($name)->{returns};
-       $outsub->($res, $result_schema);
+       $outsub->($res, $result_schema, $formatter_params);
     }
 };