[pve-devel] [PATCH pve-common] auto completion: let class name and binary name differ

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Sep 14 14:15:59 CEST 2015


Before this commit we assumed that the class name of an CLIHandler
derived class is equal to its executable name. This led to problems
when an cli executable name contained characters invalid for a class
name, e.g. an hyphen like ha-manager has.

Now we distinguishes between class and binary names. A, from
CLIHandler derived, class can use:

our exename = "...";

to set an specific executable name used to generate pod and
autocompletion files. If this variable is not set we keep the
current behaviour and use the classname for exename.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/CLIHandler.pm | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
index e6fd415..4c29ed3 100644
--- a/src/PVE/CLIHandler.pm
+++ b/src/PVE/CLIHandler.pm
@@ -13,6 +13,7 @@ use base qw(PVE::RESTHandler);
 
 my $cmddef;
 my $exename;
+my $classname;
 
 my $expand_command_name = sub {
     my ($def, $cmd) = @_;
@@ -34,6 +35,26 @@ my $expand_command_name = sub {
     return $cmd;
 };
 
+my $init_classname = sub {
+    my ($class) = @_;
+
+    $classname = $class;
+    $classname =~ s/^.*:://;
+};
+
+my $init_exename = sub {
+    my ($class) = @_;
+
+    no strict 'refs';
+    $exename = ${"${class}::exename"};
+
+    # if no specific exename is set default to class name
+    if (!defined($exename)) {
+	&$init_classname($class);
+	$exename = $classname;
+    }
+};
+
 my $complete_command_names = sub {
     my $res = [];
 
@@ -302,8 +323,7 @@ sub generate_bash_completions {
 
     # generate bash completion config
 
-    $exename = $class;
-    $exename =~ s/^.*:://;
+    &$init_exename($class);
 
     print <<__EOD__;
 # $exename bash completion
@@ -318,12 +338,12 @@ __EOD__
 }
 
 sub find_cli_class_source {
-    my ($exename) = @_;
+    my ($classname) = @_;
 
     my $filename;
 
-    my $cpath = "PVE/CLI/${exename}.pm";
-    my $spath = "PVE/Service/${exename}.pm";
+    my $cpath = "PVE/CLI/${classname}.pm";
+    my $spath = "PVE/Service/${classname}.pm";
     foreach my $p (@INC) {
 	foreach my $s (($cpath, $spath)) {
 	    my $testfn = "$p/$s";
@@ -341,10 +361,10 @@ sub find_cli_class_source {
 sub generate_pod_manpage {
     my ($class, $podfn) = @_;
 
-    $exename = $class;
-    $exename =~ s/^.*:://;
+    &$init_exename($class);
+    &$init_classname($class);
 
-    $podfn = find_cli_class_source($exename) if !defined($podfn);
+    $podfn = find_cli_class_source($classname) if !defined($podfn);
 
     die "unable to find source for class '$class'" if !$podfn;
 
@@ -367,8 +387,8 @@ sub run_cli {
 
     $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
 
-    $exename = $class;
-    $exename =~ s/^.*:://;
+    &$init_exename($class);
+    &$init_classname($class);
 
     initlog($exename);
 
-- 
2.1.4



More information about the pve-devel mailing list