]> git.proxmox.com Git - pve-common.git/blob - src/PVE/CLIHandler.pm
8753e73b524f781aac6975bfc57274268e5c8c78
[pve-common.git] / src / PVE / CLIHandler.pm
1 package PVE::CLIHandler;
2
3 use strict;
4 use warnings;
5 use JSON;
6
7 use PVE::SafeSyslog;
8 use PVE::Exception qw(raise raise_param_exc);
9 use PVE::RESTHandler;
10 use PVE::PTY;
11 use PVE::INotify;
12
13 use base qw(PVE::RESTHandler);
14
15 # $cmddef defines which (sub)commands are available in a specific CLI class.
16 # A real command is always an array consisting of its class, name, array of
17 # position fixed (required) parameters and hash of predefined parameters when
18 # mapping a CLI command t o an API call. Optionally an output method can be
19 # passed at the end, e.g., for formatting or transformation purpose.
20 #
21 # [class, name, fixed_params, API_pre-set params, output_sub ]
22 #
23 # In case of so called 'simple commands', the $cmddef can be also just an
24 # array.
25 #
26 # Examples:
27 # $cmddef = {
28 # command => [ 'PVE::API2::Class', 'command', [ 'arg1', 'arg2' ], { node => $nodename } ],
29 # do => {
30 # this => [ 'PVE::API2::OtherClass', 'method', [ 'arg1' ], undef, sub {
31 # my ($res) = @_;
32 # print "$res\n";
33 # }],
34 # that => [ 'PVE::API2::OtherClass', 'subroutine' [] ],
35 # },
36 # dothat => { alias => 'do that' },
37 # }
38 my $cmddef;
39 my $exename;
40 my $cli_handler_class;
41
42 my $standard_mappings = {
43 'pve-password' => {
44 name => 'password',
45 desc => '<password>',
46 interactive => 1,
47 func => sub {
48 my ($value) = @_;
49 return $value if $value;
50 return PVE::PTY::get_confirmed_password();
51 },
52 },
53 };
54
55 sub get_standard_mapping {
56 my ($name, $base) = @_;
57
58 my $std = $standard_mappings->{$name};
59 die "no such standard mapping '$name'\n" if !$std;
60
61 my $res = $base || {};
62
63 foreach my $opt (keys %$std) {
64 next if defined($res->{$opt});
65 $res->{$opt} = $std->{$opt};
66 }
67
68 return $res;
69 }
70
71 my $gen_param_mapping_func = sub {
72 my ($cli_handler_class) = @_;
73
74 my $param_mapping = $cli_handler_class->can('param_mapping');
75
76 if (!$param_mapping) {
77 my $read_password = $cli_handler_class->can('read_password');
78 my $string_param_mapping = $cli_handler_class->can('string_param_file_mapping');
79
80 return $string_param_mapping if !$read_password;
81
82 $param_mapping = sub {
83 my ($name) = @_;
84
85 my $password_map = get_standard_mapping('pve-password', {
86 func => $read_password
87 });
88 my $map = $string_param_mapping ? $string_param_mapping->($name) : [];
89 return [@$map, $password_map];
90 };
91 }
92
93 return $param_mapping;
94 };
95
96 my $assert_initialized = sub {
97 my @caller = caller;
98 die "$caller[0]:$caller[2] - not initialized\n"
99 if !($cmddef && $exename && $cli_handler_class);
100 };
101
102 my $abort = sub {
103 my ($reason, $cmd) = @_;
104 print_usage_short (\*STDERR, $reason, $cmd);
105 exit (-1);
106 };
107
108 my $expand_command_name = sub {
109 my ($def, $cmd) = @_;
110
111 return $cmd if exists $def->{$cmd}; # command is already complete
112
113 my $is_alias = sub { ref($_[0]) eq 'HASH' && exists($_[0]->{alias}) };
114 my @expanded = grep { /^\Q$cmd\E/ && !$is_alias->($def->{$_}) } keys %$def;
115
116 return $expanded[0] if scalar(@expanded) == 1; # enforce exact match
117
118 return undef;
119 };
120
121 my $get_commands = sub {
122 my $def = shift // die "no command definition passed!";
123 return [ grep { !(ref($def->{$_}) eq 'HASH' && defined($def->{$_}->{alias})) } sort keys %$def ];
124 };
125
126 my $complete_command_names = sub { $get_commands->($cmddef) };
127
128 # traverses the command definition using the $argv array, resolving one level
129 # of aliases.
130 # Returns the matching (sub) command and its definition, and argument array for
131 # this (sub) command and a hash where we marked which (sub) commands got
132 # expanded (e.g. st => status) while traversing
133 sub resolve_cmd {
134 my ($argv, $is_alias) = @_;
135
136 my ($def, $cmd) = ($cmddef, $argv);
137 my $cmdstr = $exename;
138
139 if (ref($argv) eq 'ARRAY') {
140 my $expanded_last_arg;
141 my $last_arg_id = scalar(@$argv) - 1;
142
143 for my $i (0..$last_arg_id) {
144 $cmd = $expand_command_name->($def, $argv->[$i]);
145 if (defined($cmd)) {
146 # If the argument was expanded (or was already complete) and it
147 # is the final argument, tell our caller about it:
148 $expanded_last_arg = $cmd if $i == $last_arg_id;
149 } else {
150 # Otherwise continue with the unexpanded version of it.
151 $cmd = $argv->[$i];
152 }
153 $cmdstr .= " $cmd";
154 $def = $def->{$cmd};
155 last if !defined($def);
156
157 if (ref($def) eq 'ARRAY') {
158 # could expand to a real command, rest of $argv are its arguments
159 my $cmd_args = [ @$argv[$i+1..$last_arg_id] ];
160 return ($cmd, $def, $cmd_args, $expanded_last_arg, $cmdstr);
161 }
162
163 if (defined($def->{alias})) {
164 die "alias loop detected for '$cmd'" if $is_alias; # avoids cycles
165 # replace aliased (sub)command with the expanded aliased command
166 splice @$argv, $i, 1, split(/ +/, $def->{alias});
167 return resolve_cmd($argv, 1);
168 }
169 }
170 # got either a special command (bashcomplete, verifyapi) or an unknown
171 # cmd, just return first entry as cmd and the rest of $argv as cmd_arg
172 my $cmd_args = [ @$argv[1..$last_arg_id] ];
173 return ($argv->[0], $def, $cmd_args, $expanded_last_arg, $cmdstr);
174 }
175 return ($cmd, $def, undef, undef, $cmdstr);
176 }
177
178 sub generate_usage_str {
179 my ($format, $cmd, $indent, $separator, $sortfunc) = @_;
180
181 $assert_initialized->();
182 die 'format required' if !$format;
183
184 $sortfunc //= sub { sort keys %{$_[0]} };
185 $separator //= '';
186 $indent //= '';
187
188 my $param_mapping_func = $gen_param_mapping_func->($cli_handler_class);
189
190 my ($subcmd, $def, undef, undef, $cmdstr) = resolve_cmd($cmd);
191 $abort->("unknown command '$cmdstr'") if !defined($def) && ref($cmd) eq 'ARRAY';
192
193 my $generate;
194 $generate = sub {
195 my ($indent, $separator, $def, $prefix) = @_;
196
197 my $str = '';
198 if (ref($def) eq 'HASH') {
199 my $oldclass = undef;
200 foreach my $cmd (&$sortfunc($def)) {
201
202 if (ref($def->{$cmd}) eq 'ARRAY') {
203 my ($class, $name, $arg_param, $fixed_param) = @{$def->{$cmd}};
204
205 $str .= $separator if $oldclass && $oldclass ne $class;
206 $str .= $indent;
207 $str .= $class->usage_str($name, "$prefix $cmd", $arg_param,
208 $fixed_param, $format,
209 $param_mapping_func);
210 $oldclass = $class;
211
212 } elsif (defined($def->{$cmd}->{alias}) && ($format eq 'asciidoc')) {
213
214 $str .= "*$prefix $cmd*\n\nAn alias for '$exename $def->{$cmd}->{alias}'.\n\n";
215
216 } else {
217 next if $def->{$cmd}->{alias};
218
219 my $substr = $generate->($indent, $separator, $def->{$cmd}, "$prefix $cmd");
220 if ($substr) {
221 $substr .= $separator if $substr !~ /\Q$separator\E{2}/;
222 $str .= $substr;
223 }
224 }
225
226 }
227 } else {
228 my ($class, $name, $arg_param, $fixed_param) = @$def;
229 $abort->("unknown command '$cmd'") if !$class;
230
231 $str .= $indent;
232 $str .= $class->usage_str($name, $prefix, $arg_param, $fixed_param, $format,
233 $param_mapping_func);
234 }
235 return $str;
236 };
237
238 return $generate->($indent, $separator, $def, $cmdstr);
239 }
240
241 __PACKAGE__->register_method ({
242 name => 'help',
243 path => 'help',
244 method => 'GET',
245 description => "Get help about specified command.",
246 parameters => {
247 additionalProperties => 0,
248 properties => {
249 'extra-args' => PVE::JSONSchema::get_standard_option('extra-args', {
250 description => 'Shows help for a specific command',
251 completion => $complete_command_names,
252 }),
253 verbose => {
254 description => "Verbose output format.",
255 type => 'boolean',
256 optional => 1,
257 },
258 },
259 },
260 returns => { type => 'null' },
261
262 code => sub {
263 my ($param) = @_;
264
265 $assert_initialized->();
266
267 my $cmd = $param->{'extra-args'};
268
269 my $verbose = defined($cmd) && $cmd;
270 $verbose = $param->{verbose} if defined($param->{verbose});
271
272 if (!$cmd) {
273 if ($verbose) {
274 print_usage_verbose();
275 } else {
276 print_usage_short(\*STDOUT);
277 }
278 return undef;
279 }
280
281 my $str;
282 if ($verbose) {
283 $str = generate_usage_str('full', $cmd, '');
284 } else {
285 $str = generate_usage_str('short', $cmd, ' ' x 7);
286 }
287 $str =~ s/^\s+//;
288
289 if ($verbose) {
290 print "$str\n";
291 } else {
292 print "USAGE: $str\n";
293 }
294
295 return undef;
296
297 }});
298
299 sub print_simple_asciidoc_synopsis {
300 $assert_initialized->();
301
302 my $synopsis = "*${exename}* `help`\n\n";
303 $synopsis .= generate_usage_str('asciidoc');
304
305 return $synopsis;
306 }
307
308 sub print_asciidoc_synopsis {
309 $assert_initialized->();
310
311 my $synopsis = "";
312
313 $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n";
314
315 $synopsis .= generate_usage_str('asciidoc');
316
317 $synopsis .= "\n";
318
319 return $synopsis;
320 }
321
322 sub print_usage_verbose {
323 $assert_initialized->();
324
325 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
326
327 my $str = generate_usage_str('full');
328
329 print "$str\n";
330 }
331
332 sub print_usage_short {
333 my ($fd, $msg, $cmd) = @_;
334
335 $assert_initialized->();
336
337 print $fd "ERROR: $msg\n" if $msg;
338 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
339
340 print {$fd} generate_usage_str('short', $cmd, ' ' x 7, "\n", sub {
341 my ($h) = @_;
342 return sort {
343 if (ref($h->{$a}) eq 'ARRAY' && ref($h->{$b}) eq 'ARRAY') {
344 # $a and $b are both real commands order them by their class
345 return $h->{$a}->[0] cmp $h->{$b}->[0] || $a cmp $b;
346 } elsif (ref($h->{$a}) eq 'ARRAY' xor ref($h->{$b}) eq 'ARRAY') {
347 # real command and subcommand mixed, put sub commands first
348 return ref($h->{$b}) eq 'ARRAY' ? -1 : 1;
349 } else {
350 # both are either from the same class or subcommands
351 return $a cmp $b;
352 }
353 } keys %$h;
354 });
355 }
356
357 my $print_bash_completion = sub {
358 my ($simple_cmd, $bash_command, $cur, $prev) = @_;
359
360 my $debug = 0;
361
362 return if !(defined($cur) && defined($prev) && defined($bash_command));
363 return if !defined($ENV{COMP_LINE});
364 return if !defined($ENV{COMP_POINT});
365
366 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
367 print STDERR "\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
368
369 my $args = PVE::Tools::split_args($cmdline);
370 shift @$args; # no need for program name
371 my $print_result = sub {
372 foreach my $p (@_) {
373 print "$p\n" if $p =~ m/^\Q$cur\E/;
374 }
375 };
376
377 my ($cmd, $def) = ($simple_cmd, $cmddef);
378 if (!$simple_cmd) {
379 ($cmd, $def, $args, my $expanded) = resolve_cmd($args);
380
381 if (defined($expanded) && $prev ne $expanded) {
382 print "$expanded\n";
383 return;
384 }
385
386 if (ref($def) eq 'HASH') {
387 &$print_result(@{$get_commands->($def)});
388 return;
389 }
390 }
391 return if !$def;
392
393 my $pos = scalar(@$args) - 1;
394 $pos += 1 if $cmdline =~ m/\s+$/;
395 print STDERR "pos: $pos\n" if $debug;
396 return if $pos < 0;
397
398 my $skip_param = {};
399
400 my ($class, $name, $arg_param, $uri_param) = @$def;
401 $arg_param //= [];
402 $uri_param //= {};
403
404 $arg_param = [ $arg_param ] if !ref($arg_param);
405
406 map { $skip_param->{$_} = 1; } @$arg_param;
407 map { $skip_param->{$_} = 1; } keys %$uri_param;
408
409 my $info = $class->map_method_by_name($name);
410
411 my $prop = $info->{parameters}->{properties};
412
413 my $print_parameter_completion = sub {
414 my ($pname) = @_;
415 my $d = $prop->{$pname};
416 if ($d->{completion}) {
417 my $vt = ref($d->{completion});
418 if ($vt eq 'CODE') {
419 my $res = $d->{completion}->($cmd, $pname, $cur, $args);
420 &$print_result(@$res);
421 }
422 } elsif ($d->{type} eq 'boolean') {
423 &$print_result('0', '1');
424 } elsif ($d->{enum}) {
425 &$print_result(@{$d->{enum}});
426 }
427 };
428
429 # positional arguments
430 if ($pos < scalar(@$arg_param)) {
431 my $pname = $arg_param->[$pos];
432 &$print_parameter_completion($pname);
433 return;
434 }
435
436 my @option_list = ();
437 foreach my $key (keys %$prop) {
438 next if $skip_param->{$key};
439 push @option_list, "--$key";
440 }
441
442 if ($cur =~ m/^-/) {
443 &$print_result(@option_list);
444 return;
445 }
446
447 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
448 my $pname = $1;
449 &$print_parameter_completion($pname);
450 return;
451 }
452
453 &$print_result(@option_list);
454 };
455
456 sub data_to_text {
457 my ($data) = @_;
458
459 return '' if !defined($data);
460
461 if (my $class = ref($data)) {
462 return to_json($data, { utf8 => 1, canonical => 1 });
463 } else {
464 return "$data";
465 }
466 }
467
468 # prints a formatted table with a title row.
469 # $data - the data to print (array of objects)
470 # $returnprops -json schema property description
471 # $props_to_print - ordered list of properties to print
472 # $sort_key can be used to sort after a column, if it isn't set we sort
473 # after the leftmost column (with no undef value in $data) this can be
474 # turned off by passing 0 as $sort_key
475 sub print_text_table {
476 my ($data, $returnprops, $props_to_print, $sort_key) = @_;
477
478 my $autosort = 1;
479 if (defined($sort_key) && $sort_key eq 0) {
480 $autosort = 0;
481 $sort_key = undef;
482 }
483
484 my $colopts = {};
485 my $formatstring = '';
486
487 my $column_count = scalar(@$props_to_print);
488
489 for (my $i = 0; $i < $column_count; $i++) {
490 my $prop = $props_to_print->[$i];
491 my $propinfo = $returnprops->{$prop} // {};
492
493 my $title = $propinfo->{title} // $prop;
494 my $cutoff = $propinfo->{print_width} // $propinfo->{maxLength};
495
496 # calculate maximal print width and cutoff
497 my $titlelen = length($title);
498
499 my $longest = $titlelen;
500 my $sortable = $autosort;
501 foreach my $entry (@$data) {
502 my $len = length(data_to_text($entry->{$prop})) // 0;
503 $longest = $len if $len > $longest;
504 $sortable = 0 if !defined($entry->{$prop});
505 }
506 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
507 $sort_key //= $prop if $sortable;
508
509 $colopts->{$prop} = {
510 title => $title,
511 default => $propinfo->{default} // '',
512 cutoff => $cutoff,
513 };
514
515 # skip alignment and cutoff on last column
516 $formatstring .= ($i == ($column_count - 1)) ? "%s\n" : "%-${cutoff}s ";
517 }
518
519 printf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
520
521 if (defined($sort_key)) {
522 my $type = $returnprops->{$sort_key}->{type} // 'string';
523 if ($type eq 'integer' || $type eq 'number') {
524 @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
525 } else {
526 @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
527 }
528 }
529
530 foreach my $entry (@$data) {
531 printf $formatstring, map {
532 substr(data_to_text($entry->{$_}) // $colopts->{$_}->{default},
533 0, $colopts->{$_}->{cutoff});
534 } @$props_to_print;
535 }
536 }
537
538 # prints the result of an API GET call returning an array as a table.
539 # takes formatting information from the results property of the call
540 # if $props_to_print is provided, prints only those columns. otherwise
541 # takes all fields of the results property, with a fallback
542 # to all fields occuring in items of $data.
543 sub print_api_list {
544 my ($data, $result_schema, $props_to_print, $sort_key) = @_;
545
546 die "can only print object lists\n"
547 if !($result_schema->{type} eq 'array' && $result_schema->{items}->{type} eq 'object');
548
549 my $returnprops = $result_schema->{items}->{properties};
550
551 if (!defined($props_to_print)) {
552 $props_to_print = [ sort keys %$returnprops ];
553 if (!scalar(@$props_to_print)) {
554 my $all_props = {};
555 foreach my $obj (@{$data}) {
556 foreach my $key (keys %{$obj}) {
557 $all_props->{ $key } = 1;
558 }
559 }
560 $props_to_print = [ sort keys %{$all_props} ];
561 }
562 die "unable to detect list properties\n" if !scalar(@$props_to_print);
563 }
564
565 print_text_table($data, $returnprops, $props_to_print, $sort_key);
566 }
567
568 sub print_api_result {
569 my ($format, $data, $result_schema, $props_to_print, $sort_key) = @_;
570
571 return if $result_schema->{type} eq 'null';
572
573 if ($format eq 'json') {
574 print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
575 } elsif ($format eq 'text') {
576 my $type = $result_schema->{type};
577 if ($type eq 'object') {
578 $props_to_print = [ sort keys %$data ] if !defined($props_to_print);
579 foreach my $key (@$props_to_print) {
580 print $key . ": " . data_to_text($data->{$key}) . "\n";
581 }
582 } elsif ($type eq 'array') {
583 return if !scalar(@$data);
584 my $item_type = $result_schema->{items}->{type};
585 if ($item_type eq 'object') {
586 print_api_list($data, $result_schema, $props_to_print, $sort_key);
587 } else {
588 foreach my $entry (@$data) {
589 print data_to_text($entry) . "\n";
590 }
591 }
592 } else {
593 print "$data\n";
594 }
595 } else {
596 die "internal error: unknown output format"; # should not happen
597 }
598 }
599
600 sub verify_api {
601 my ($class) = @_;
602
603 # simply verify all registered methods
604 PVE::RESTHandler::validate_method_schemas();
605 }
606
607 my $get_exe_name = sub {
608 my ($class) = @_;
609
610 my $name = $class;
611 $name =~ s/^.*:://;
612 $name =~ s/_/-/g;
613
614 return $name;
615 };
616
617 sub generate_bash_completions {
618 my ($class) = @_;
619
620 # generate bash completion config
621
622 $exename = &$get_exe_name($class);
623
624 print <<__EOD__;
625 # $exename bash completion
626
627 # see http://tiswww.case.edu/php/chet/bash/FAQ
628 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
629 # this modifies global var, but I found no better way
630 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
631
632 complete -o default -C '$exename bashcomplete' $exename
633 __EOD__
634 }
635
636 sub generate_asciidoc_synopsys {
637 my ($class) = @_;
638 $class->generate_asciidoc_synopsis();
639 };
640
641 sub generate_asciidoc_synopsis {
642 my ($class) = @_;
643
644 $cli_handler_class = $class;
645
646 $exename = &$get_exe_name($class);
647
648 no strict 'refs';
649 my $def = ${"${class}::cmddef"};
650 $cmddef = $def;
651
652 if (ref($def) eq 'ARRAY') {
653 print_simple_asciidoc_synopsis();
654 } else {
655 $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ];
656
657 print_asciidoc_synopsis();
658 }
659 }
660
661 # overwrite this if you want to run/setup things early
662 sub setup_environment {
663 my ($class) = @_;
664
665 # do nothing by default
666 }
667
668 my $handle_cmd = sub {
669 my ($args, $preparefunc, $param_mapping_func) = @_;
670
671 $cmddef->{help} = [ __PACKAGE__, 'help', ['extra-args'] ];
672
673 my ($cmd, $def, $cmd_args, undef, $cmd_str) = resolve_cmd($args);
674
675 $abort->("no command specified") if !$cmd;
676
677 # call verifyapi before setup_environment(), don't execute any real code in
678 # this case
679 if ($cmd eq 'verifyapi') {
680 PVE::RESTHandler::validate_method_schemas();
681 return;
682 }
683
684 $cli_handler_class->setup_environment();
685
686 if ($cmd eq 'bashcomplete') {
687 &$print_bash_completion(undef, @$cmd_args);
688 return;
689 }
690
691 # checked special commands, if def is still a hash we got an incomplete sub command
692 $abort->("incomplete command '$cmd_str'", $args) if ref($def) eq 'HASH';
693
694 &$preparefunc() if $preparefunc;
695
696 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def || []};
697 $abort->("unknown command '$cmd_str'") if !$class;
698
699 my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_mapping_func);
700
701 if (defined $outsub) {
702 my $result_schema = $class->map_method_by_name($name)->{returns};
703 $outsub->($res, $result_schema);
704 }
705 };
706
707 my $handle_simple_cmd = sub {
708 my ($args, $preparefunc, $param_mapping_func) = @_;
709
710 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef};
711 die "no class specified" if !$class;
712
713 if (scalar(@$args) >= 1) {
714 if ($args->[0] eq 'help') {
715 my $str = "USAGE: $name help\n";
716 $str .= generate_usage_str('long');
717 print STDERR "$str\n\n";
718 return;
719 } elsif ($args->[0] eq 'verifyapi') {
720 PVE::RESTHandler::validate_method_schemas();
721 return;
722 }
723 }
724
725 $cli_handler_class->setup_environment();
726
727 if (scalar(@$args) >= 1) {
728 if ($args->[0] eq 'bashcomplete') {
729 shift @$args;
730 &$print_bash_completion($name, @$args);
731 return;
732 }
733 }
734
735 &$preparefunc() if $preparefunc;
736
737 my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $param_mapping_func);
738
739 if (defined $outsub) {
740 my $result_schema = $class->map_method_by_name($name)->{returns};
741 $outsub->($res, $result_schema);
742 }
743 };
744
745 sub run_cli_handler {
746 my ($class, %params) = @_;
747
748 $cli_handler_class = $class;
749
750 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
751
752 foreach my $key (keys %params) {
753 next if $key eq 'prepare';
754 next if $key eq 'no_init'; # not used anymore
755 next if $key eq 'no_rpcenv'; # not used anymore
756 die "unknown parameter '$key'";
757 }
758
759 my $preparefunc = $params{prepare};
760
761 my $param_mapping_func = $gen_param_mapping_func->($cli_handler_class);
762
763 $exename = &$get_exe_name($class);
764
765 initlog($exename);
766
767 no strict 'refs';
768 $cmddef = ${"${class}::cmddef"};
769
770 if (ref($cmddef) eq 'ARRAY') {
771 $handle_simple_cmd->(\@ARGV, $preparefunc, $param_mapping_func);
772 } else {
773 $handle_cmd->(\@ARGV, $preparefunc, $param_mapping_func);
774 }
775
776 exit 0;
777 }
778
779 1;