[pve-devel] [PATCH common v2 6/9] cli: factor out generate usage string
Wolfgang Bumiller
w.bumiller at proxmox.com
Fri Dec 29 11:31:51 CET 2017
On Mon, Dec 18, 2017 at 10:21:40AM +0100, Thomas Lamprecht wrote:
> reduce code reuse and prepare for sub commands
>
> Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
> ---
> src/PVE/CLIHandler.pm | 138 ++++++++++++++++++++++++++++----------------------
> 1 file changed, 77 insertions(+), 61 deletions(-)
>
> diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
> index 061adb6..7d0804d 100644
> --- a/src/PVE/CLIHandler.pm
> +++ b/src/PVE/CLIHandler.pm
(...)
> @@ -149,24 +175,13 @@ sub print_asciidoc_synopsis {
> }
>
> sub print_usage_verbose {
> -
> $assert_initialized->();
>
> - my $pwcallback = $cli_handler_class->can('read_password');
> - my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
> -
> print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
>
> - foreach my $cmd (sort keys %$cmddef) {
> - my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
> - my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
> - 'full', $pwcallback, $stringfilemap);
> - print "$str\n\n";
> - }
> -}
> + my $str = generate_usage_str('full');
>
> -sub sorted_commands {
> - return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
Note the || here. If the package names are identical this sorts by
command names. This is not the case anymore with the $sortfunc passed
below:
> + print "$str\n";
> }
>
> sub print_usage_short {
> @@ -174,20 +189,21 @@ sub print_usage_short {
>
> $assert_initialized->();
>
> - my $pwcallback = $cli_handler_class->can('read_password');
> - my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
> -
> print $fd "ERROR: $msg\n" if $msg;
> print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
>
> - my $oldclass;
> - foreach my $cmd (sorted_commands()) {
> - my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
> - my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback, $stringfilemap);
> - print $fd "\n" if $oldclass && $oldclass ne $class;
> - print $fd " $str";
> - $oldclass = $class;
> - }
> + print {$fd} generate_usage_str('short', undef, ' ' x 7, "\n", sub {
> + my ($h) = @_;
> + return sort {
> + if (ref($h->{$a}) eq 'ARRAY' && ref($h->{$b}) eq 'ARRAY') {
> + # $a and $b are both real commands order them by their class
> + return $h->{$a}->[0] cmp $h->{$b}->[0];
^ Here.
If they're equal this returns 0, but should return ($a cmp $b).
Running any command without parameters multiple times now shuffles the
commands around while still sorting by package ;-)
> + } else {
> + # both are from the same class
> + return $a cmp $b;
> + }
> + } keys %$h;
> + });
> }
>
> my $print_bash_completion = sub {
> @@ -338,7 +354,7 @@ sub generate_asciidoc_synopsis {
> $cmddef = $def;
>
> if (ref($def) eq 'ARRAY') {
> - print_simple_asciidoc_synopsis(@$def);
> + print_simple_asciidoc_synopsis();
> } else {
> $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ];
>
> @@ -398,7 +414,7 @@ my $handle_simple_cmd = sub {
> if (scalar(@$args) >= 1) {
> if ($args->[0] eq 'help') {
> my $str = "USAGE: $name help\n";
> - $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback, $stringfilemap);
> + $str .= generate_usage_str('long');
> print STDERR "$str\n\n";
> return;
> } elsif ($args->[0] eq 'verifyapi') {
> --
> 2.11.0
More information about the pve-devel
mailing list