]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/CLIHandler.pm
1 package PVE
::CLIHandler
;
8 use PVE
::Exception
qw(raise raise_param_exc);
12 use base
qw(PVE::RESTHandler);
17 my $expand_command_name = sub {
22 for my $k (keys(%$def)) {
25 $expanded = undef; # more than one match
32 $cmd = $expanded if $expanded;
37 __PACKAGE__-
>register_method ({
41 description
=> "Get help about specified command.",
43 additionalProperties
=> 0,
46 description
=> "Command name",
51 description
=> "Verbose output format.",
57 returns
=> { type
=> 'null' },
62 die "not initialized" if !($cmddef && $exename);
64 my $cmd = $param->{cmd
};
66 my $verbose = defined($cmd) && $cmd;
67 $verbose = $param->{verbose
} if defined($param->{verbose
});
71 print_usage_verbose
();
73 print_usage_short
(\
*STDOUT
);
78 $cmd = &$expand_command_name($cmddef, $cmd);
80 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []};
82 raise_param_exc
({ cmd
=> "no such command '$cmd'"}) if !$class;
85 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, $verbose ?
'full' : 'short');
89 print "USAGE: $str\n";
96 sub print_pod_manpage
{
99 die "not initialized" if !($cmddef && $exename);
100 die "no pod file specified" if !$podfn;
104 $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
106 my $style = 'full'; # or should we use 'short'?
108 foreach my $cmd (sorted_commands
()) {
109 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
110 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
112 $str =~ s/^USAGE: //;
114 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
116 $synopsis .= " $str\n\n";
122 my $parser = PVE
::PodParser-
>new();
123 $parser->{include
}->{synopsis
} = $synopsis;
124 $parser->parse_from_file($podfn);
127 sub print_usage_verbose
{
129 die "not initialized" if !($cmddef && $exename);
131 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
133 foreach my $cmd (sort keys %$cmddef) {
134 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
135 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'full');
140 sub sorted_commands
{
141 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
144 sub print_usage_short
{
147 die "not initialized" if !($cmddef && $exename);
149 print $fd "ERROR: $msg\n" if $msg;
150 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
153 foreach my $cmd (sorted_commands
()) {
154 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
155 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short');
156 print $fd "\n" if $oldclass && $oldclass ne $class;
162 my $print_bash_completion = sub {
163 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
167 return if !(defined($cur) && defined($prev) && defined($bash_command));
168 return if !defined($ENV{COMP_LINE
});
169 return if !defined($ENV{COMP_POINT
});
171 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
172 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
174 # fixme: shell quoting??
175 my @args = split(/\s+/, $cmdline);
176 my $pos = scalar(@args) - 2;
177 $pos += 1 if $cmdline =~ m/\s+$/;
179 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
183 my $print_result = sub {
185 print "$p\n" if $p =~ m/^$cur/;
194 &$print_result(keys %$cmddef);
200 my $def = $cmddef->{$cmd};
203 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
207 my ($class, $name, $arg_param, $uri_param) = @$def;
211 map { $skip_param->{$_} = 1; } @$arg_param;
212 map { $skip_param->{$_} = 1; } keys %$uri_param;
214 my $fpcount = scalar(@$arg_param);
216 my $info = $class->map_method_by_name($name);
218 my $schema = $info->{parameters
};
219 my $prop = $schema->{properties
};
221 my $print_parameter_completion = sub {
223 my $d = $prop->{$pname};
224 if ($d->{completion
}) {
225 my $vt = ref($d->{completion
});
227 my $res = $d->{completion
}->($cmd, $pname, $cur);
228 &$print_result(@$res);
230 } elsif ($d->{type
} eq 'boolean') {
231 &$print_result('0', '1');
232 } elsif ($d->{enum
}) {
233 &$print_result(@{$d->{enum
}});
237 # positional arguments
238 $pos += 1 if $simple_cmd;
239 if ($fpcount && $pos <= $fpcount) {
240 my $pname = $arg_param->[$pos -1];
241 &$print_parameter_completion($pname);
245 my @option_list = ();
246 foreach my $key (keys %$prop) {
247 next if $skip_param->{$key};
248 push @option_list, "--$key";
252 &$print_result(@option_list);
256 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
258 &$print_parameter_completion($pname);
262 &$print_result(@option_list);
268 # simply verify all registered methods
269 PVE
::RESTHandler
::validate_method_schemas
();
272 sub generate_pod_manpage
{
273 my ($class, $podfn) = @_;
276 $cmddef = ${"${class}::cmddef"};
279 $exename =~ s/^.*:://;
281 if (!defined($podfn)) {
282 my $cpath = "$class.pm";
284 foreach my $p (@INC) {
285 my $testfn = "$p/$cpath";
293 die "unable to find source for class '$class'" if !$podfn;
295 print_pod_manpage
($podfn);
299 my ($class, $pwcallback, $preparefunc) = @_;
301 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
304 $cmddef = ${"${class}::cmddef"};
307 $exename =~ s/^.*:://;
311 die "please run as root\n" if $> != 0;
313 PVE
::INotify
::inotify_init
();
315 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
316 $rpcenv->init_request();
317 $rpcenv->set_language($ENV{LANG
});
318 $rpcenv->set_user('root@pam');
320 my $cmd = shift @ARGV;
322 handle_cmd
($cmddef, $exename, $cmd, \
@ARGV, $pwcallback, $0, $preparefunc);
328 my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_;
333 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
336 print_usage_short
(\
*STDERR
, "no command specified");
338 } elsif ($cmd eq 'verifyapi') {
339 PVE
::RESTHandler
::validate_method_schemas
();
341 } elsif ($cmd eq 'printmanpod') {
342 print_pod_manpage
($podfn);
344 } elsif ($cmd eq 'bashcomplete') {
345 &$print_bash_completion($cmddef, 0, @$args);
349 &$preparefunc() if $preparefunc;
351 $cmd = &$expand_command_name($cmddef, $cmd);
353 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
356 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
360 my $prefix = "$exename $cmd";
361 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
363 &$outsub($res) if $outsub;
366 sub handle_simple_cmd
{
367 my ($def, $args, $pwcallback, $podfn) = @_;
369 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
370 die "no class specified" if !$class;
372 if (scalar(@$args) >= 1) {
373 if ($args->[0] eq 'help') {
374 my $str = "USAGE: $name help\n";
375 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
376 print STDERR
"$str\n\n";
378 } elsif ($args->[0] eq 'bashcomplete') {
380 &$print_bash_completion({ $name => $def }, $name, @$args);
382 } elsif ($args->[0] eq 'verifyapi') {
383 PVE
::RESTHandler
::validate_method_schemas
();
385 } elsif ($args->[0] eq 'printmanpod') {
386 my $synopsis = " $name help\n\n";
387 my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
392 my $parser = PVE
::PodParser-
>new();
393 $parser->{include
}->{synopsis
} = $synopsis;
394 $parser->parse_from_file($podfn);
399 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
401 &$outsub($res) if $outsub;