]> git.proxmox.com Git - pve-common.git/commitdiff
cli: refactor comand name helper
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 18 Dec 2017 09:21:37 +0000 (10:21 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 29 Dec 2017 10:51:20 +0000 (11:51 +0100)
use shorter and also a bit faster methods to expand and get comand
names

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
Acked-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/PVE/CLIHandler.pm

index f50611d257b6f8e6973a8dc84b33648578cb20f1..c3829694b77e46bee13acb7a2904da1664425b8a 100644 (file)
@@ -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 ({