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

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Dec 18 10:21:37 CET 2017


use shorter and also a bit faster methods to expand and get comand
names

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

 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 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 ({
-- 
2.11.0





More information about the pve-devel mailing list