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