From: Thomas Lamprecht Date: Mon, 18 Dec 2017 09:21:37 +0000 (+0100) Subject: cli: refactor comand name helper X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=7bac844eaab512d8789a9103d0efa774a1bab0d6;ds=sidebyside cli: refactor comand name helper use shorter and also a bit faster methods to expand and get comand names Signed-off-by: Thomas Lamprecht Acked-by: Wolfgang Bumiller --- diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm index f50611d..c382969 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 { /^\Q$cmd\E/ } 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 ({