1 package PVE
::CLIHandler
;
7 use PVE
::Exception
qw(raise raise_param_exc);
11 use base
qw(PVE::RESTHandler);
15 my $cli_handler_class;
17 my $assert_initialized = sub {
19 die "$caller[0]:$caller[2] - not initialized\n"
20 if !($cmddef && $exename && $cli_handler_class);
23 my $expand_command_name = sub {
28 for my $k (keys(%$def)) {
31 $expanded = undef; # more than one match
38 $cmd = $expanded if $expanded;
43 my $complete_command_names = sub {
46 return if ref($cmddef) ne 'HASH';
48 foreach my $cmd (keys %$cmddef) {
49 next if $cmd eq 'help';
56 __PACKAGE__-
>register_method ({
60 description
=> "Get help about specified command.",
62 additionalProperties
=> 0,
65 description
=> "Command name",
68 completion
=> $complete_command_names,
71 description
=> "Verbose output format.",
77 returns
=> { type
=> 'null' },
82 $assert_initialized->();
84 my $cmd = $param->{cmd
};
86 my $verbose = defined($cmd) && $cmd;
87 $verbose = $param->{verbose
} if defined($param->{verbose
});
91 print_usage_verbose
();
93 print_usage_short
(\
*STDOUT
);
98 $cmd = &$expand_command_name($cmddef, $cmd);
100 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []};
102 raise_param_exc
({ cmd
=> "no such command '$cmd'"}) if !$class;
104 my $pwcallback = $cli_handler_class->can('read_password');
105 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
107 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
108 $verbose ?
'full' : 'short', $pwcallback,
113 print "USAGE: $str\n";
120 sub print_simple_asciidoc_synopsis
{
121 my ($class, $name, $arg_param, $uri_param) = @_;
123 $assert_initialized->();
125 my $pwcallback = $cli_handler_class->can('read_password');
126 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
128 my $synopsis = "*${name}* `help`\n\n";
130 $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param,
131 'asciidoc', $pwcallback, $stringfilemap);
136 sub print_asciidoc_synopsis
{
138 $assert_initialized->();
140 my $pwcallback = $cli_handler_class->can('read_password');
141 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
145 $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n";
148 foreach my $cmd (sort keys %$cmddef) {
149 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
150 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
151 $uri_param, 'asciidoc', $pwcallback,
153 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
155 $synopsis .= "$str\n\n";
164 sub print_usage_verbose
{
166 $assert_initialized->();
168 my $pwcallback = $cli_handler_class->can('read_password');
169 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
171 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
173 foreach my $cmd (sort keys %$cmddef) {
174 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
175 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
176 'full', $pwcallback, $stringfilemap);
181 sub sorted_commands
{
182 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
185 sub print_usage_short
{
188 $assert_initialized->();
190 my $pwcallback = $cli_handler_class->can('read_password');
191 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
193 print $fd "ERROR: $msg\n" if $msg;
194 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
197 foreach my $cmd (sorted_commands
()) {
198 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
199 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback, $stringfilemap);
200 print $fd "\n" if $oldclass && $oldclass ne $class;
206 my $print_bash_completion = sub {
207 my ($simple_cmd, $bash_command, $cur, $prev) = @_;
211 return if !(defined($cur) && defined($prev) && defined($bash_command));
212 return if !defined($ENV{COMP_LINE
});
213 return if !defined($ENV{COMP_POINT
});
215 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
216 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
218 my $args = PVE
::Tools
::split_args
($cmdline);
219 my $pos = scalar(@$args) - 2;
220 $pos += 1 if $cmdline =~ m/\s+$/;
222 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
226 my $print_result = sub {
228 print "$p\n" if $p =~ m/^$cur/;
237 &$print_result(keys %$cmddef);
243 my $def = $cmddef->{$cmd};
246 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
250 my ($class, $name, $arg_param, $uri_param) = @$def;
254 $arg_param = [ $arg_param ] if !ref($arg_param);
256 map { $skip_param->{$_} = 1; } @$arg_param;
257 map { $skip_param->{$_} = 1; } keys %$uri_param;
259 my $fpcount = scalar(@$arg_param);
261 my $info = $class->map_method_by_name($name);
263 my $schema = $info->{parameters
};
264 my $prop = $schema->{properties
};
266 my $print_parameter_completion = sub {
268 my $d = $prop->{$pname};
269 if ($d->{completion
}) {
270 my $vt = ref($d->{completion
});
272 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
273 &$print_result(@$res);
275 } elsif ($d->{type
} eq 'boolean') {
276 &$print_result('0', '1');
277 } elsif ($d->{enum
}) {
278 &$print_result(@{$d->{enum
}});
282 # positional arguments
283 $pos += 1 if $simple_cmd;
284 if ($fpcount && $pos <= $fpcount) {
285 my $pname = $arg_param->[$pos -1];
286 &$print_parameter_completion($pname);
290 my @option_list = ();
291 foreach my $key (keys %$prop) {
292 next if $skip_param->{$key};
293 push @option_list, "--$key";
297 &$print_result(@option_list);
301 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
303 &$print_parameter_completion($pname);
307 &$print_result(@option_list);
313 # simply verify all registered methods
314 PVE
::RESTHandler
::validate_method_schemas
();
317 my $get_exe_name = sub {
327 sub generate_bash_completions
{
330 # generate bash completion config
332 $exename = &$get_exe_name($class);
335 # $exename bash completion
337 # see http://tiswww.case.edu/php/chet/bash/FAQ
338 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
339 # this modifies global var, but I found no better way
340 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
342 complete -o default -C '$exename bashcomplete' $exename
346 sub generate_asciidoc_synopsys
{
348 $class->generate_asciidoc_synopsis();
351 sub generate_asciidoc_synopsis
{
354 $cli_handler_class = $class;
356 $exename = &$get_exe_name($class);
359 my $def = ${"${class}::cmddef"};
362 if (ref($def) eq 'ARRAY') {
363 print_simple_asciidoc_synopsis
(@$def);
365 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
367 print_asciidoc_synopsis
();
371 # overwrite this if you want to run/setup things early
372 sub setup_environment
{
375 # do nothing by default
378 my $handle_cmd = sub {
379 my ($args, $pwcallback, $preparefunc, $stringfilemap) = @_;
381 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
383 # call verifyapi before setup_environment(), because we do not want to
384 # execute any real code in this case
386 my $cmd = shift @$args;
388 print_usage_short
(\
*STDERR
, "no command specified");
390 } elsif ($cmd eq 'verifyapi') {
391 PVE
::RESTHandler
::validate_method_schemas
();
395 $cli_handler_class->setup_environment();
397 if ($cmd eq 'bashcomplete') {
398 &$print_bash_completion(undef, @$args);
402 &$preparefunc() if $preparefunc;
404 $cmd = &$expand_command_name($cmddef, $cmd);
406 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
409 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
413 my $prefix = "$exename $cmd";
414 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
416 &$outsub($res) if $outsub;
419 my $handle_simple_cmd = sub {
420 my ($args, $pwcallback, $preparefunc, $stringfilemap) = @_;
422 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef};
423 die "no class specified" if !$class;
425 if (scalar(@$args) >= 1) {
426 if ($args->[0] eq 'help') {
427 my $str = "USAGE: $name help\n";
428 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback, $stringfilemap);
429 print STDERR
"$str\n\n";
431 } elsif ($args->[0] eq 'verifyapi') {
432 PVE
::RESTHandler
::validate_method_schemas
();
437 $cli_handler_class->setup_environment();
439 if (scalar(@$args) >= 1) {
440 if ($args->[0] eq 'bashcomplete') {
442 &$print_bash_completion($name, @$args);
447 &$preparefunc() if $preparefunc;
449 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
451 &$outsub($res) if $outsub;
454 sub run_cli_handler
{
455 my ($class, %params) = @_;
457 $cli_handler_class = $class;
459 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
461 foreach my $key (keys %params) {
462 next if $key eq 'prepare';
463 next if $key eq 'no_init'; # not used anymore
464 next if $key eq 'no_rpcenv'; # not used anymore
465 die "unknown parameter '$key'";
468 my $preparefunc = $params{prepare
};
470 my $pwcallback = $class->can('read_password');
471 my $stringfilemap = $class->can('string_param_file_mapping');
473 $exename = &$get_exe_name($class);
478 $cmddef = ${"${class}::cmddef"};
480 if (ref($cmddef) eq 'ARRAY') {
481 &$handle_simple_cmd(\
@ARGV, $pwcallback, $preparefunc, $stringfilemap);
483 &$handle_cmd(\
@ARGV, $pwcallback, $preparefunc, $stringfilemap);