[pve-devel] [PATCH common v2 4/9] cli: refactor print_bash_completion

Wolfgang Bumiller w.bumiller at proxmox.com
Fri Dec 29 11:31:42 CET 2017


On Mon, Dec 18, 2017 at 10:21:38AM +0100, Thomas Lamprecht wrote:
> 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>
> ---
>  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 c382969..e5fe82f 100644
> --- a/src/PVE/CLIHandler.pm
> +++ b/src/PVE/CLIHandler.pm
> @@ -197,34 +197,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);
(...)
> +    $def = $def->{$cmddef};

Should be ->{$cmd}? Otherwise this is equivalent to
$def = $cmddef->{$cmddef} (using the hash as index).
This is gone in subsequent patches though.
Since this patch is purely a refactoring of this one function, perhaps
it could be squashed into the later patch fixing this?

>      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 = {};
>  
> @@ -237,12 +231,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) = @_;
> @@ -261,9 +252,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