]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIHandler.pm
CLIHandler.pm: fix command line completion for simple commands
[pve-common.git] / src / PVE / CLIHandler.pm
index 1fd7b025223e197fd86b6efb3afc6d748fd2d7d1..316d29d72108574d3df137f4c7faaa277fd99260 100644 (file)
@@ -52,11 +52,12 @@ my $abort = sub {
 my $expand_command_name = sub {
     my ($def, $cmd) = @_;
 
-    if (!$def->{$cmd}) {
-       my @expanded = grep { /^\Q$cmd\E/ } keys %$def;
-       return $expanded[0] if scalar(@expanded) == 1; # enforce exact match
-    }
-    return $cmd;
+    return $cmd if exists $def->{$cmd}; # command is already complete
+
+    my @expanded = grep { /^\Q$cmd\E/ } keys %$def;
+    return $expanded[0] if scalar(@expanded) == 1; # enforce exact match
+
+    return undef;
 };
 
 my $get_commands = sub {
@@ -75,21 +76,30 @@ sub resolve_cmd {
     my ($argv, $is_alias) = @_;
 
     my ($def, $cmd) = ($cmddef, $argv);
+    my $cmdstr = $exename;
 
     if (ref($argv) eq 'ARRAY') {
-       my $expanded = {};
+       my $expanded_last_arg;
        my $last_arg_id = scalar(@$argv) - 1;
 
        for my $i (0..$last_arg_id) {
            $cmd = $expand_command_name->($def, $argv->[$i]);
-           $expanded->{$argv->[$i]} = $cmd if $cmd ne $argv->[$i];
+           if (defined($cmd)) {
+               # If the argument was expanded (or was already complete) and it
+               # is the final argument, tell our caller about it:
+               $expanded_last_arg = $cmd if $i == $last_arg_id;
+           } else {
+               # Otherwise continue with the unexpanded version of it.
+               $cmd = $argv->[$i]; 
+           }
+           $cmdstr .= " $cmd";
            last if !defined($def->{$cmd});
            $def = $def->{$cmd};
 
            if (ref($def) eq 'ARRAY') {
                # could expand to a real command, rest of $argv are its arguments
                my $cmd_args = [ @$argv[$i+1..$last_arg_id] ];
-               return ($cmd, $def, $cmd_args, $expanded);
+               return ($cmd, $def, $cmd_args, $expanded_last_arg, $cmdstr);
            }
 
            if (defined($def->{alias})) {
@@ -102,9 +112,9 @@ sub resolve_cmd {
        # got either a special command (bashcomplete, verifyapi) or an unknown
        # cmd, just return first entry as cmd and the rest of $argv as cmd_arg
        my $cmd_args = [ @$argv[1..$last_arg_id] ];
-       return ($argv->[0], $def, $cmd_args, $expanded);
+       return ($argv->[0], $def, $cmd_args, $expanded_last_arg, $cmdstr);
     }
-    return ($cmd, $def);
+    return ($cmd, $def, undef, undef, $cmdstr);
 }
 
 sub generate_usage_str {
@@ -121,7 +131,7 @@ sub generate_usage_str {
     my $param_mapping_func = $cli_handler_class->can('param_mapping') ||
        $cli_handler_class->can('string_param_file_mapping');
 
-    my ($subcmd, $def) = resolve_cmd($cmd);
+    my ($subcmd, $def, undef, undef, $cmdstr) = resolve_cmd($cmd);
 
     my $generate;
     $generate = sub {
@@ -168,9 +178,6 @@ sub generate_usage_str {
        return $str;
     };
 
-    my $cmdstr = $exename;
-    $cmdstr .= ' ' . join(' ', @$cmd) if defined($cmd);
-
     return $generate->($indent, $separator, $def, $cmdstr);
 }
 
@@ -312,14 +319,15 @@ my $print_bash_completion = sub {
 
     my ($cmd, $def) = ($simple_cmd, $cmddef);
     if (!$simple_cmd) {
-       ($cmd, $def, $args, my $expaned) = resolve_cmd($args);
+       ($cmd, $def, $args, my $expanded) = resolve_cmd($args);
 
-       if (ref($def) eq 'HASH') {
-           &$print_result(@{$get_commands->($def)});
+       if (defined($expanded) && $prev ne $expanded) {
+           print "$expanded\n";
            return;
        }
-       if (my $expanded_cmd = $expaned->{$cur}) {
-           print "$expanded_cmd\n";
+
+       if (ref($def) eq 'HASH') {
+           &$print_result(@{$get_commands->($def)});
            return;
        }
     }
@@ -362,7 +370,6 @@ my $print_bash_completion = sub {
     };
 
     # positional arguments
-    $pos++ if $simple_cmd;
     if ($pos < scalar(@$arg_param)) {
        my $pname = $arg_param->[$pos];
        &$print_parameter_completion($pname);
@@ -462,8 +469,7 @@ my $handle_cmd  = sub {
 
     $cmddef->{help} = [ __PACKAGE__, 'help', ['extra-args'] ];
 
-    my $cmd_str = join(' ', @$args);
-    my ($cmd, $def, $cmd_args) = resolve_cmd($args);
+    my ($cmd, $def, $cmd_args, undef, $cmd_str) = resolve_cmd($args);
 
     $abort->("no command specified") if !$cmd;
 
@@ -482,15 +488,14 @@ my $handle_cmd  = sub {
     }
 
     # checked special commands, if def is still a hash we got an incomplete sub command
-    $abort->("incomplete command '$exename $cmd_str'") if ref($def) eq 'HASH';
+    $abort->("incomplete command '$cmd_str'") if ref($def) eq 'HASH';
 
     &$preparefunc() if $preparefunc;
 
     my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def || []};
     $abort->("unknown command '$cmd_str'") if !$class;
 
-    my $prefix = "$exename $cmd_str";
-    my $res = $class->cli_handler($prefix, $name, $cmd_args, $arg_param, $uri_param, $read_password_func, $param_mapping_func);
+    my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $read_password_func, $param_mapping_func);
 
     &$outsub($res) if $outsub;
 };