[pve-devel] [RFC common 3/9] cli: refactor comand name helper

Dominik Csapak d.csapak at proxmox.com
Tue Dec 5 08:56:04 CET 2017


Reviewed-by: Dominik Csapak <d.csapak at proxmox.com>

On 11/06/2017 02:54 PM, Thomas Lamprecht wrote:
> use shorter and also a bit faster methods to expand and get comand
> names
> ---
>   src/PVE/CLIHandler.pm | 25 +++----------------------
>   1 file changed, 3 insertions(+), 22 deletions(-)
> 
> diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
> index c787ac4..ab1440c 100644
> --- a/src/PVE/CLIHandler.pm
> +++ b/src/PVE/CLIHandler.pm
> @@ -24,33 +24,14 @@ my $expand_command_name = sub {
>       my ($def, $cmd) = @_;
>   
>       if (!$def->{$cmd}) {
> -	my $expanded;
> -	for my $k (keys(%$def)) {
> -	    if ($k =~ m/^$cmd/) {
> -		if ($expanded) {
> -		    $expanded = undef; # more than one match
> -		    last;
> -		} else {
> -		    $expanded = $k;
> -		}
> -	    }
> -	}
> -	$cmd = $expanded if $expanded;
> +	my @expanded = grep { /^$cmd/ } keys %$def;
> +	return $expanded[0] if scalar(@expanded) == 1; # enforce exact match
>       }
>       return $cmd;
>   };
>   
>   my $complete_command_names = sub {
> -    my $res = [];
> -
> -    return if ref($cmddef) ne 'HASH';
> -
> -    foreach my $cmd (keys %$cmddef) {
> -	next if $cmd eq 'help';
> -	push @$res, $cmd;
> -    }
> -
> -    return $res;
> +    return [ sort keys %$cmddef ];
>   };
>   
>   __PACKAGE__->register_method ({
> 





More information about the pve-devel mailing list