]>
git.proxmox.com Git - pve-common.git/blob - data/PVE/CLIHandler.pm
1 package PVE
::CLIHandler
;
6 use PVE
::Exception
qw(raise raise_param_exc);
10 use base
qw(PVE::RESTHandler);
15 my $expand_command_name = sub {
20 for my $k (keys(%$def)) {
23 $expanded = undef; # more than one match
30 $cmd = $expanded if $expanded;
35 __PACKAGE__-
>register_method ({
39 description
=> "Get help about specified command.",
41 additionalProperties
=> 0,
44 description
=> "Command name",
49 description
=> "Verbose output format.",
55 returns
=> { type
=> 'null' },
60 die "not initialized" if !($cmddef && $exename);
62 my $cmd = $param->{cmd
};
64 my $verbose = defined($cmd) && $cmd;
65 $verbose = $param->{verbose
} if defined($param->{verbose
});
69 print_usage_verbose
();
71 print_usage_short
(\
*STDOUT
);
76 $cmd = &$expand_command_name($cmddef, $cmd);
78 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []};
80 raise_param_exc
({ cmd
=> "no such command '$cmd'"}) if !$class;
83 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, $verbose ?
'full' : 'short');
87 print "USAGE: $str\n";
94 sub print_pod_manpage
{
97 die "not initialized" if !($cmddef && $exename);
98 die "no pod file specified" if !$podfn;
102 $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
104 my $style = 'full'; # or should we use 'short'?
106 foreach my $cmd (sorted_commands
()) {
107 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
108 my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
110 $str =~ s/^USAGE: //;
112 $synopsis .= "\n" if $oldclass && $oldclass ne $class;
114 $synopsis .= " $str\n\n";
120 my $parser = PVE
::PodParser-
>new();
121 $parser->{include
}->{synopsis
} = $synopsis;
122 $parser->parse_from_file($podfn);
125 sub print_usage_verbose
{
127 die "not initialized" if !($cmddef && $exename);
129 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
131 foreach my $cmd (sort keys %$cmddef) {
132 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
133 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'full');
138 sub sorted_commands
{
139 return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef;
142 sub print_usage_short
{
145 die "not initialized" if !($cmddef && $exename);
147 print $fd "ERROR: $msg\n" if $msg;
148 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
151 foreach my $cmd (sorted_commands
()) {
152 my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
153 my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short');
154 print $fd "\n" if $oldclass && $oldclass ne $class;
161 my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn) = @_;
166 $cmddef->{help
} = [ __PACKAGE__
, 'help', ['cmd'] ];
169 print_usage_short
(\
*STDERR
, "no command specified");
171 } elsif ($cmd eq 'verifyapi') {
172 PVE
::RESTHandler
::validate_method_schemas
();
174 } elsif ($cmd eq 'printmanpod') {
175 print_pod_manpage
($podfn);
179 $cmd = &$expand_command_name($cmddef, $cmd);
181 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []};
184 print_usage_short
(\
*STDERR
, "unknown command '$cmd'");
188 my $prefix = "$exename $cmd";
189 my $res = $class->cli_handler($prefix, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
191 &$outsub($res) if $outsub;
194 sub handle_simple_cmd
{
195 my ($def, $args, $pwcallback, $podfn) = @_;
197 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
198 die "no class specified" if !$class;
200 if (scalar(@$args) == 1) {
201 if ($args->[0] eq 'help') {
202 my $str = "USAGE: $name help\n";
203 $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
204 print STDERR
"$str\n\n";
206 } elsif ($args->[0] eq 'verifyapi') {
207 PVE
::RESTHandler
::validate_method_schemas
();
209 } elsif ($args->[0] eq 'printmanpod') {
210 my $synopsis = " $name help\n\n";
211 my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long');
216 my $parser = PVE
::PodParser-
>new();
217 $parser->{include
}->{synopsis
} = $synopsis;
218 $parser->parse_from_file($podfn);
223 my $res = $class->cli_handler($name, $name, \
@ARGV, $arg_param, $uri_param, $pwcallback);
225 &$outsub($res) if $outsub;