[pve-devel] [PATCH common v3 1/5] cli: refactor print_bash_completion

Thomas Lamprecht t.lamprecht at proxmox.com
Tue Jan 9 13:25:29 CET 2018


move variables nearer to where they actually used.
drop program name early from argv array
drop unnecessary variables

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---

changes v2 -> v3:
* "$def = $def->{$cmd};" not "$def = $def->{$def};"
  while only an intermediate problem it still nice to have this right.

 src/PVE/CLIHandler.pm | 39 +++++++++++++++------------------------
 1 file changed, 15 insertions(+), 24 deletions(-)

diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
index 4307678..8cd6dc6 100644
--- a/src/PVE/CLIHandler.pm
+++ b/src/PVE/CLIHandler.pm
@@ -203,34 +203,28 @@ my $print_bash_completion = sub {
     print STDERR "\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
 
     my $args = PVE::Tools::split_args($cmdline);
-    my $pos = scalar(@$args) - 2;
-    $pos += 1 if $cmdline =~ m/\s+$/;
-
-    print STDERR "CMDLINE:$pos:$cmdline\n" if $debug;
-
-    return if $pos < 0;
-
+    shift @$args; # no need for program name
     my $print_result = sub {
 	foreach my $p (@_) {
 	    print "$p\n" if $p =~ m/^$cur/;
 	}
     };
 
-    my $cmd;
-    if ($simple_cmd) {
-	$cmd = $simple_cmd;
-    } else {
-	if ($pos == 0) {
-	    &$print_result(keys %$cmddef);
+    my ($cmd, $def) = ($simple_cmd, $cmddef);
+    if (!$simple_cmd) {
+	if (!scalar(@$args)) {
+	    &$print_result(keys %$def);
 	    return;
 	}
-	$cmd = $args->[1];
+	$cmd = $args->[0];
     }
-
-    my $def = $cmddef->{$cmd};
+    $def = $def->{$cmd};
     return if !$def;
 
-    print STDERR "CMDLINE1:$pos:$cmdline\n" if $debug;
+    my $pos = scalar(@$args) - 1;
+    $pos += 1 if $cmdline =~ m/\s+$/;
+    print STDERR "pos: $pos\n" if $debug;
+    return if $pos < 0;
 
     my $skip_param = {};
 
@@ -243,12 +237,9 @@ my $print_bash_completion = sub {
     map { $skip_param->{$_} = 1; } @$arg_param;
     map { $skip_param->{$_} = 1; } keys %$uri_param;
 
-    my $fpcount = scalar(@$arg_param);
-
     my $info = $class->map_method_by_name($name);
 
-    my $schema = $info->{parameters};
-    my $prop = $schema->{properties};
+    my $prop = $info->{parameters}->{properties};
 
     my $print_parameter_completion = sub {
 	my ($pname) = @_;
@@ -267,9 +258,9 @@ my $print_bash_completion = sub {
     };
 
     # positional arguments
-    $pos += 1 if $simple_cmd;
-    if ($fpcount && $pos <= $fpcount) {
-	my $pname = $arg_param->[$pos -1];
+    $pos++ if $simple_cmd;
+    if ($pos < scalar(@$arg_param)) {
+	my $pname = $arg_param->[$pos];
 	&$print_parameter_completion($pname);
 	return;
     }
-- 
2.11.0





More information about the pve-devel mailing list