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);
24 my ($reason, $cmd) = @_;
25 print_usage_short
(\
*STDERR
, $reason, $cmd);
29 my $expand_command_name = sub {
33 my @expanded = grep { /^\Q$cmd\E/ } keys %$def;
34 return $expanded[0] if scalar(@expanded) == 1; # enforce exact match
39 my $complete_command_names = sub {
40 return [ sort keys %$cmddef ];
43 __PACKAGE__-
>register_method ({
47 description
=> "Get help about specified command.",
49 additionalProperties
=> 0,
52 description
=> "Command name",
55 completion
=> $complete_command_names,
58 description
=> "Verbose output format.",
64 returns
=> { type
=> 'null' },
69 $assert_initialized->();
71 my $cmd = $param->{cmd
};
73 my $verbose = defined($cmd) && $cmd;
74 $verbose = $param->{verbose
} if defined($param->{verbose
});
78 print_usage_verbose
();
80 print_usage_short
(\
*STDOUT
);
85 $cmd = &$expand_command_name($cmddef, $cmd);
87 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []};
89 raise_param_exc
({ cmd
=> "no such command '$cmd'"}) if !$class;
91 my $pwcallback = $cli_handler_class->can('read_password');
92 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
94 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
95 $verbose ?
'full' : 'short', $pwcallback,
100 print "USAGE: $str\n";
107 sub print_simple_asciidoc_synopsis
{
108 my ($class, $name, $arg_param, $uri_param) = @_;
110 $assert_initialized->();
112 my $pwcallback = $cli_handler_class->can('read_password');
113 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
115 my $synopsis = "*${name}* `help`\n\n";
117 $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param,
118 'asciidoc', $pwcallback, $stringfilemap);
123 sub print_asciidoc_synopsis
{
125 $assert_initialized->();
127 my $pwcallback = $cli_handler_class->can('read_password');
128 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
132 $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n";
135 foreach my $cmd (sort keys %$cmddef) {
136 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
137 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
138 $uri_param, 'asciidoc', $pwcallback,
140 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
142 $synopsis .= "$str\n\n";
151 sub print_usage_verbose
{
153 $assert_initialized->();
155 my $pwcallback = $cli_handler_class->can('read_password');
156 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
158 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
160 foreach my $cmd (sort keys %$cmddef) {
161 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
162 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
163 'full', $pwcallback, $stringfilemap);
168 sub sorted_commands
{
169 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
172 sub print_usage_short
{
175 $assert_initialized->();
177 my $pwcallback = $cli_handler_class->can('read_password');
178 my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
180 print $fd "ERROR: $msg\n" if $msg;
181 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
184 foreach my $cmd (sorted_commands
()) {
185 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
186 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback, $stringfilemap);
187 print $fd "\n" if $oldclass && $oldclass ne $class;
193 my $print_bash_completion = sub {
194 my ($simple_cmd, $bash_command, $cur, $prev) = @_;
198 return if !(defined($cur) && defined($prev) && defined($bash_command));
199 return if !defined($ENV{COMP_LINE
});
200 return if !defined($ENV{COMP_POINT
});
202 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
203 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
205 my $args = PVE
::Tools
::split_args
($cmdline);
206 shift @$args; # no need for program name
207 my $print_result = sub {
209 print "$p\n" if $p =~ m/^$cur/;
213 my ($cmd, $def) = ($simple_cmd, $cmddef);
215 if (!scalar(@$args)) {
216 &$print_result(keys %$def);
224 my $pos = scalar(@$args) - 1;
225 $pos += 1 if $cmdline =~ m/\s+$/;
226 print STDERR
"pos: $pos\n" if $debug;
231 my ($class, $name, $arg_param, $uri_param) = @$def;
235 $arg_param = [ $arg_param ] if !ref($arg_param);
237 map { $skip_param->{$_} = 1; } @$arg_param;
238 map { $skip_param->{$_} = 1; } keys %$uri_param;
240 my $info = $class->map_method_by_name($name);
242 my $prop = $info->{parameters
}->{properties
};
244 my $print_parameter_completion = sub {
246 my $d = $prop->{$pname};
247 if ($d->{completion
}) {
248 my $vt = ref($d->{completion
});
250 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
251 &$print_result(@$res);
253 } elsif ($d->{type
} eq 'boolean') {
254 &$print_result('0', '1');
255 } elsif ($d->{enum
}) {
256 &$print_result(@{$d->{enum
}});
260 # positional arguments
261 $pos++ if $simple_cmd;
262 if ($pos < scalar(@$arg_param)) {
263 my $pname = $arg_param->[$pos];
264 &$print_parameter_completion($pname);
268 my @option_list = ();
269 foreach my $key (keys %$prop) {
270 next if $skip_param->{$key};
271 push @option_list, "--$key";
275 &$print_result(@option_list);
279 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
281 &$print_parameter_completion($pname);
285 &$print_result(@option_list);
291 # simply verify all registered methods
292 PVE
::RESTHandler
::validate_method_schemas
();
295 my $get_exe_name = sub {
305 sub generate_bash_completions
{
308 # generate bash completion config
310 $exename = &$get_exe_name($class);
313 # $exename bash completion
315 # see http://tiswww.case.edu/php/chet/bash/FAQ
316 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
317 # this modifies global var, but I found no better way
318 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
320 complete -o default -C '$exename bashcomplete' $exename
324 sub generate_asciidoc_synopsys
{
326 $class->generate_asciidoc_synopsis();
329 sub generate_asciidoc_synopsis
{
332 $cli_handler_class = $class;
334 $exename = &$get_exe_name($class);
337 my $def = ${"${class}::cmddef"};
340 if (ref($def) eq 'ARRAY') {
341 print_simple_asciidoc_synopsis
(@$def);
343 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
345 print_asciidoc_synopsis
();
349 # overwrite this if you want to run/setup things early
350 sub setup_environment
{
353 # do nothing by default
356 my $handle_cmd = sub {
357 my ($args, $pwcallback, $preparefunc, $stringfilemap) = @_;
359 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
362 my $cmd = shift @$args;
363 $abort->("no command specified") if !$cmd;
365 # call verifyapi before setup_environment(), don't execute any real code in
367 if ($cmd eq 'verifyapi') {
368 PVE
::RESTHandler
::validate_method_schemas
();
372 $cli_handler_class->setup_environment();
374 if ($cmd eq 'bashcomplete') {
375 &$print_bash_completion(undef, @$args);
379 &$preparefunc() if $preparefunc;
381 $cmd = &$expand_command_name($cmddef, $cmd);
383 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
384 $abort->("unknown command '$cmd'") if !$class;
386 my $prefix = "$exename $cmd";
387 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
389 &$outsub($res) if $outsub;
392 my $handle_simple_cmd = sub {
393 my ($args, $pwcallback, $preparefunc, $stringfilemap) = @_;
395 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef};
396 die "no class specified" if !$class;
398 if (scalar(@$args) >= 1) {
399 if ($args->[0] eq 'help') {
400 my $str = "USAGE: $name help\n";
401 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback, $stringfilemap);
402 print STDERR
"$str\n\n";
404 } elsif ($args->[0] eq 'verifyapi') {
405 PVE
::RESTHandler
::validate_method_schemas
();
410 $cli_handler_class->setup_environment();
412 if (scalar(@$args) >= 1) {
413 if ($args->[0] eq 'bashcomplete') {
415 &$print_bash_completion($name, @$args);
420 &$preparefunc() if $preparefunc;
422 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
424 &$outsub($res) if $outsub;
427 sub run_cli_handler
{
428 my ($class, %params) = @_;
430 $cli_handler_class = $class;
432 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
434 foreach my $key (keys %params) {
435 next if $key eq 'prepare';
436 next if $key eq 'no_init'; # not used anymore
437 next if $key eq 'no_rpcenv'; # not used anymore
438 die "unknown parameter '$key'";
441 my $preparefunc = $params{prepare
};
443 my $pwcallback = $class->can('read_password');
444 my $stringfilemap = $class->can('string_param_file_mapping');
446 $exename = &$get_exe_name($class);
451 $cmddef = ${"${class}::cmddef"};
453 if (ref($cmddef) eq 'ARRAY') {
454 &$handle_simple_cmd(\
@ARGV, $pwcallback, $preparefunc, $stringfilemap);
456 &$handle_cmd(\
@ARGV, $pwcallback, $preparefunc, $stringfilemap);