]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIHandler.pm
cli: use exists() for alias check
[pve-common.git] / src / PVE / CLIHandler.pm
index 8c911b2ebeb1f5d48026d51f6534e0f7e29e11db..a7ced38a41ccd04662297d8d5d0ab0c6c74a1bf7 100644 (file)
@@ -54,7 +54,9 @@ my $expand_command_name = sub {
 
     return $cmd if exists $def->{$cmd}; # command is already complete
 
-    my @expanded = grep { /^\Q$cmd\E/ } keys %$def;
+    my $is_alias = sub { ref($_[0]) eq 'HASH' && exists($_[0]->{alias}) };
+    my @expanded = grep { /^\Q$cmd\E/ && !$is_alias->($def->{$_}) } keys %$def;
+
     return $expanded[0] if scalar(@expanded) == 1; # enforce exact match
 
     return undef;
@@ -93,8 +95,8 @@ sub resolve_cmd {
                $cmd = $argv->[$i]; 
            }
            $cmdstr .= " $cmd";
-           last if !defined($def->{$cmd});
            $def = $def->{$cmd};
+           last if !defined($def);
 
            if (ref($def) eq 'ARRAY') {
                # could expand to a real command, rest of $argv are its arguments
@@ -132,6 +134,7 @@ sub generate_usage_str {
        $cli_handler_class->can('string_param_file_mapping');
 
     my ($subcmd, $def, undef, undef, $cmdstr) = resolve_cmd($cmd);
+    $abort->("unknown command '$cmdstr'") if !defined($def) && ref($cmd) eq 'ARRAY';
 
     my $generate;
     $generate = sub {
@@ -370,7 +373,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);
@@ -489,7 +491,7 @@ my $handle_cmd  = sub {
     }
 
     # checked special commands, if def is still a hash we got an incomplete sub command
-    $abort->("incomplete command '$cmd_str'") if ref($def) eq 'HASH';
+    $abort->("incomplete command '$cmd_str'", $args) if ref($def) eq 'HASH';
 
     &$preparefunc() if $preparefunc;