[pve-devel] [PATCH pve-common] command line completion: complete fully specified sub commands

Dietmar Maurer dietmar at proxmox.com
Thu Jun 7 09:09:09 CEST 2018


This handles the following case (example): "pvesm add"

Command 'add' is detected as sub command, so this completes
to "pvesm add " (added space).

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 src/PVE/CLIHandler.pm | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
index 222051d..389f8bd 100644
--- a/src/PVE/CLIHandler.pm
+++ b/src/PVE/CLIHandler.pm
@@ -299,6 +299,8 @@ my $print_bash_completion = sub {
     return if !defined($ENV{COMP_POINT});
 
     my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
+    my $ends_with_space = $cmdline =~ m/\s+$/ ? 1 : 0;
+
     print STDERR "\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
 
     my $args = PVE::Tools::split_args($cmdline);
@@ -321,11 +323,17 @@ my $print_bash_completion = sub {
 	    print "$expanded_cmd\n";
 	    return;
 	}
+	if (ref($def) eq 'ARRAY') {
+	    if (scalar(@$args) == 0 && !$ends_with_space) {
+		print "$cmd\n";
+		return;
+	    }
+	}
     }
     return if !$def;
 
     my $pos = scalar(@$args) - 1;
-    $pos += 1 if $cmdline =~ m/\s+$/;
+    $pos += 1 if $ends_with_space;
     print STDERR "pos: $pos\n" if $debug;
     return if $pos < 0;
 
-- 
2.11.0




More information about the pve-devel mailing list