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