[pve-devel] [RFC common 3/9] cli: refactor comand name helper
Thomas Lamprecht
t.lamprecht at proxmox.com
Mon Nov 6 14:54:48 CET 2017
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 ({
--
2.11.0
More information about the pve-devel
mailing list