]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/CLIHandler.pm
6d3f69dd06317b26e5685cf437f115d158e7406a
1 package PVE
::CLIHandler
;
7 use PVE
::Exception
qw(raise raise_param_exc);
11 use base
qw(PVE::RESTHandler);
16 my $expand_command_name = sub {
21 for my $k (keys(%$def)) {
24 $expanded = undef; # more than one match
31 $cmd = $expanded if $expanded;
36 __PACKAGE__-
>register_method ({
40 description
=> "Get help about specified command.",
42 additionalProperties
=> 0,
45 description
=> "Command name",
50 description
=> "Verbose output format.",
56 returns
=> { type
=> 'null' },
61 die "not initialized" if !($cmddef && $exename);
63 my $cmd = $param->{cmd
};
65 my $verbose = defined($cmd) && $cmd;
66 $verbose = $param->{verbose
} if defined($param->{verbose
});
70 print_usage_verbose
();
72 print_usage_short
(\
*STDOUT
);
77 $cmd = &$expand_command_name($cmddef, $cmd);
79 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []};
81 raise_param_exc
({ cmd
=> "no such command '$cmd'"}) if !$class;
84 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, $verbose ?
'full' : 'short');
88 print "USAGE: $str\n";
95 sub print_pod_manpage
{
98 die "not initialized" if !($cmddef && $exename);
99 die "no pod file specified" if !$podfn;
103 $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
105 my $style = 'full'; # or should we use 'short'?
107 foreach my $cmd (sorted_commands
()) {
108 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
109 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
111 $str =~ s/^USAGE: //;
113 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
115 $synopsis .= " $str\n\n";
121 my $parser = PVE
::PodParser-
>new();
122 $parser->{include
}->{synopsis
} = $synopsis;
123 $parser->parse_from_file($podfn);
126 sub print_usage_verbose
{
128 die "not initialized" if !($cmddef && $exename);
130 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
132 foreach my $cmd (sort keys %$cmddef) {
133 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
134 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'full');
139 sub sorted_commands
{
140 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
143 sub print_usage_short
{
146 die "not initialized" if !($cmddef && $exename);
148 print $fd "ERROR: $msg\n" if $msg;
149 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
152 foreach my $cmd (sorted_commands
()) {
153 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
154 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short');
155 print $fd "\n" if $oldclass && $oldclass ne $class;
161 my $print_bash_completion = sub {
162 my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_;
166 return if !(defined($cur) && defined($prev) && defined($bash_command));
167 return if !defined($ENV{COMP_LINE
});
168 return if !defined($ENV{COMP_POINT
});
170 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
171 print STDERR
"\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
173 # fixme: shell quoting??
174 my @args = split(/\s+/, $cmdline);
175 my $pos = scalar(@args) - 2;
176 $pos += 1 if $cmdline =~ m/\s+$/;
178 print STDERR
"CMDLINE:$pos:$cmdline\n" if $debug;
182 my $print_result = sub {
184 print "$p\n" if $p =~ m/^$cur/;
193 &$print_result(keys %$cmddef);
199 my $def = $cmddef->{$cmd};
202 print STDERR
"CMDLINE1:$pos:$cmdline\n" if $debug;
206 my ($class, $name, $arg_param, $uri_param) = @$def;
210 map { $skip_param->{$_} = 1; } @$arg_param;
211 map { $skip_param->{$_} = 1; } keys %$uri_param;
213 my $fpcount = scalar(@$arg_param);
215 my $info = $class->map_method_by_name($name);
217 my $schema = $info->{parameters
};
218 my $prop = $schema->{properties
};
220 my $print_parameter_completion = sub {
222 my $d = $prop->{$pname};
223 if ($d->{completion
}) {
224 my $vt = ref($d->{completion
});
226 my $res = $d->{completion
}->($cmd, $pname, $cur);
227 &$print_result(@$res);
229 } elsif ($d->{type
} eq 'boolean') {
230 &$print_result('0', '1');
231 } elsif ($d->{enum
}) {
232 &$print_result(@{$d->{enum
}});
236 # positional arguments
237 $pos += 1 if $simple_cmd;
238 if ($fpcount && $pos <= $fpcount) {
239 my $pname = $arg_param->[$pos -1];
240 &$print_parameter_completion($pname);
244 my @option_list = ();
245 foreach my $key (keys %$prop) {
246 next if $skip_param->{$key};
247 push @option_list, "--$key";
251 &$print_result(@option_list);
255 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
257 &$print_parameter_completion($pname);
261 &$print_result(@option_list);
265 my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_;
270 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
273 print_usage_short
(\
*STDERR
, "no command specified");
275 } elsif ($cmd eq 'verifyapi') {
276 PVE
::RESTHandler
::validate_method_schemas
();
278 } elsif ($cmd eq 'printmanpod') {
279 print_pod_manpage
($podfn);
281 } elsif ($cmd eq 'bashcomplete') {
282 &$print_bash_completion($cmddef, 0, @$args);
286 &$preparefunc() if $preparefunc;
288 $cmd = &$expand_command_name($cmddef, $cmd);
290 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
293 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
297 my $prefix = "$exename $cmd";
298 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
300 &$outsub($res) if $outsub;
303 sub handle_simple_cmd
{
304 my ($def, $args, $pwcallback, $podfn) = @_;
306 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
307 die "no class specified" if !$class;
309 if (scalar(@$args) >= 1) {
310 if ($args->[0] eq 'help') {
311 my $str = "USAGE: $name help\n";
312 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
313 print STDERR
"$str\n\n";
315 } elsif ($args->[0] eq 'bashcomplete') {
317 &$print_bash_completion({ $name => $def }, $name, @$args);
319 } elsif ($args->[0] eq 'verifyapi') {
320 PVE
::RESTHandler
::validate_method_schemas
();
322 } elsif ($args->[0] eq 'printmanpod') {
323 my $synopsis = " $name help\n\n";
324 my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
329 my $parser = PVE
::PodParser-
>new();
330 $parser->{include
}->{synopsis
} = $synopsis;
331 $parser->parse_from_file($podfn);
336 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
338 &$outsub($res) if $outsub;