]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIHandler.pm
print_bash_completion: handle special case passing $arg_param as string
[pve-common.git] / src / PVE / CLIHandler.pm
index e6fd415b3ef4fd237c03db93eb038e835e7d1bed..0863fdc64fbe2560b7a6275a060259f1c3186a18 100644 (file)
@@ -199,9 +199,8 @@ my $print_bash_completion = sub {
     my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
     print STDERR "\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
 
-    # fixme: shell quoting??
-    my @args = split(/\s+/, $cmdline);
-    my $pos = scalar(@args) - 2;
+    my $args = PVE::Tools::split_args($cmdline);
+    my $pos = scalar(@$args) - 2;
     $pos += 1 if $cmdline =~ m/\s+$/;
 
     print STDERR "CMDLINE:$pos:$cmdline\n" if $debug;
@@ -222,7 +221,7 @@ my $print_bash_completion = sub {
            &$print_result(keys %$cmddef);
            return;
        }
-       $cmd = $args[1];
+       $cmd = $args->[1];
     }
 
     my $def = $cmddef->{$cmd};
@@ -236,6 +235,8 @@ my $print_bash_completion = sub {
     $arg_param //= [];
     $uri_param //= {};
 
+    $arg_param = [ $arg_param ] if !ref($arg_param);
+
     map { $skip_param->{$_} = 1; } @$arg_param;
     map { $skip_param->{$_} = 1; } keys %$uri_param;
 
@@ -252,7 +253,7 @@ my $print_bash_completion = sub {
        if ($d->{completion}) {
            my $vt = ref($d->{completion});
            if ($vt eq 'CODE') {
-               my $res = $d->{completion}->($cmd, $pname, $cur);
+               my $res = $d->{completion}->($cmd, $pname, $cur, $args);
                &$print_result(@$res);
            }
        } elsif ($d->{type} eq 'boolean') {
@@ -297,13 +298,22 @@ sub verify_api {
     PVE::RESTHandler::validate_method_schemas();
 }
 
+my $get_exe_name = sub {
+    my ($class) = @_;
+    
+    my $name = $class;
+    $name =~ s/^.*:://;
+    $name =~ s/_/-/g;
+
+    return $name;
+};
+
 sub generate_bash_completions {
     my ($class) = @_;
 
     # generate bash completion config
 
-    $exename = $class;
-    $exename =~ s/^.*:://;
+    $exename = &$get_exe_name($class);
 
     print <<__EOD__;
 # $exename bash completion
@@ -318,12 +328,14 @@ __EOD__
 }
 
 sub find_cli_class_source {
-    my ($exename) = @_;
+    my ($name) = @_;
 
     my $filename;
 
-    my $cpath = "PVE/CLI/${exename}.pm";
-    my $spath = "PVE/Service/${exename}.pm";
+    $name =~ s/-/_/g;
+
+    my $cpath = "PVE/CLI/${name}.pm";
+    my $spath = "PVE/Service/${name}.pm";
     foreach my $p (@INC) {
        foreach my $s (($cpath, $spath)) {
            my $testfn = "$p/$s";
@@ -341,8 +353,7 @@ sub find_cli_class_source {
 sub generate_pod_manpage {
     my ($class, $podfn) = @_;
 
-    $exename = $class;
-    $exename =~ s/^.*:://;
+    $exename = &$get_exe_name($class);
 
     $podfn = find_cli_class_source($exename) if !defined($podfn);
 
@@ -367,8 +378,7 @@ sub run_cli {
 
     $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
 
-    $exename = $class;
-    $exename =~ s/^.*:://;
+    $exename = &$get_exe_name($class);
 
     initlog($exename);