mirror of
https://git.proxmox.com/git/pve-common
synced 2025-07-17 10:21:29 +00:00
make cli argument parser more flexible
This commit is contained in:
parent
2de75075b0
commit
0ce82909f4
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
||||
RELEASE=2.0
|
||||
|
||||
VERSION=1.0
|
||||
PKGREL=7
|
||||
PKGREL=8
|
||||
|
||||
PACKAGE=libpve-common-perl
|
||||
|
||||
|
@ -849,7 +849,7 @@ sub method_get_child_link {
|
||||
# a way to parse command line parameters, using a
|
||||
# schema to configure Getopt::Long
|
||||
sub get_options {
|
||||
my ($schema, $args, $uri_param, $pwcallback, $list_param) = @_;
|
||||
my ($schema, $args, $arg_param, $fixed_param, $pwcallback) = @_;
|
||||
|
||||
if (!$schema || !$schema->{properties}) {
|
||||
raise("too many arguments\n", code => HTTP_BAD_REQUEST)
|
||||
@ -857,11 +857,19 @@ sub get_options {
|
||||
return {};
|
||||
}
|
||||
|
||||
my $list_param;
|
||||
if ($arg_param && !ref($arg_param)) {
|
||||
my $pd = $schema->{properties}->{$arg_param};
|
||||
die "expected list format $pd->{format}"
|
||||
if !($pd && $pd->{format} && $pd->{format} =~ m/-list/);
|
||||
$list_param = $arg_param;
|
||||
}
|
||||
|
||||
my @getopt = ();
|
||||
foreach my $prop (keys %{$schema->{properties}}) {
|
||||
my $pd = $schema->{properties}->{$prop};
|
||||
next if $list_param && $prop eq $list_param;
|
||||
next if defined($uri_param->{$prop});
|
||||
next if defined($fixed_param->{$prop});
|
||||
|
||||
if ($prop eq 'password' && $pwcallback) {
|
||||
# we do not accept plain password on input line, instead
|
||||
@ -882,18 +890,23 @@ sub get_options {
|
||||
my $opts = {};
|
||||
raise("unable to parse option\n", code => HTTP_BAD_REQUEST)
|
||||
if !Getopt::Long::GetOptionsFromArray($args, $opts, @getopt);
|
||||
|
||||
if ($list_param) {
|
||||
my $pd = $schema->{properties}->{$list_param} ||
|
||||
die "no schema for list_param";
|
||||
|
||||
$opts->{$list_param} = $args if scalar($args);
|
||||
$args = [];
|
||||
if (my $acount = scalar(@$args)) {
|
||||
if ($list_param) {
|
||||
$opts->{$list_param} = $args;
|
||||
$args = [];
|
||||
} elsif (ref($arg_param)) {
|
||||
raise("wrong number of arguments\n", code => HTTP_BAD_REQUEST)
|
||||
if scalar(@$arg_param) != $acount;
|
||||
foreach my $p (@$arg_param) {
|
||||
$opts->{$p} = shift @$args;
|
||||
}
|
||||
} else {
|
||||
raise("too many arguments\n", code => HTTP_BAD_REQUEST)
|
||||
if scalar(@$args) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
raise("too many arguments\n", code => HTTP_BAD_REQUEST)
|
||||
if scalar(@$args) != 0;
|
||||
|
||||
if (my $pd = $schema->{properties}->{password}) {
|
||||
if ($pd->{type} ne 'boolean' && $pwcallback) {
|
||||
if ($opts->{password} || !$pd->{optional}) {
|
||||
@ -935,8 +948,8 @@ sub get_options {
|
||||
}
|
||||
}
|
||||
|
||||
foreach my $p (keys %$uri_param) {
|
||||
$opts->{$p} = $uri_param->{$p};
|
||||
foreach my $p (keys %$fixed_param) {
|
||||
$opts->{$p} = $fixed_param->{$p};
|
||||
}
|
||||
|
||||
return $opts;
|
||||
|
@ -477,29 +477,9 @@ sub cli_handler {
|
||||
|
||||
my $info = $self->map_method_by_name($name);
|
||||
|
||||
my $param;
|
||||
foreach my $p (keys %$fixed_param) {
|
||||
$param->{$p} = $fixed_param->{$p};
|
||||
}
|
||||
|
||||
my $list_param;
|
||||
if ($arg_param) {
|
||||
if (ref($arg_param)) {
|
||||
foreach my $p (@$arg_param) {
|
||||
$param->{$p} = shift @$args if $args->[0] && $args->[0] !~ m/^-\S/;
|
||||
}
|
||||
} else {
|
||||
my $pd = $info->{parameters}->{properties}->{$arg_param};
|
||||
die "expected list format $pd->{format}"
|
||||
if !($pd && $pd->{format} && $pd->{format} =~ m/-list/);
|
||||
$list_param = $arg_param;
|
||||
}
|
||||
}
|
||||
|
||||
my $res;
|
||||
eval {
|
||||
my $param = PVE::JSONSchema::get_options($info->{parameters}, $args, $param, $pwcallback, $list_param);
|
||||
|
||||
my $param = PVE::JSONSchema::get_options($info->{parameters}, $args, $arg_param, $fixed_param, $pwcallback);
|
||||
$res = $self->handle($info, $param);
|
||||
};
|
||||
if (my $err = $@) {
|
||||
|
6
debian/changelog
vendored
6
debian/changelog
vendored
@ -1,3 +1,9 @@
|
||||
libpve-common-perl (1.0-8) unstable; urgency=low
|
||||
|
||||
* make cli argument parser more flexible
|
||||
|
||||
-- Proxmox Support Team <support@proxmox.com> Wed, 23 Nov 2011 08:36:30 +0100
|
||||
|
||||
libpve-common-perl (1.0-7) unstable; urgency=low
|
||||
|
||||
* bug fixes (see git log)
|
||||
|
Loading…
Reference in New Issue
Block a user