mirror of
https://git.proxmox.com/git/pve-common
synced 2025-07-20 01:10:06 +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
|
RELEASE=2.0
|
||||||
|
|
||||||
VERSION=1.0
|
VERSION=1.0
|
||||||
PKGREL=7
|
PKGREL=8
|
||||||
|
|
||||||
PACKAGE=libpve-common-perl
|
PACKAGE=libpve-common-perl
|
||||||
|
|
||||||
|
@ -849,7 +849,7 @@ sub method_get_child_link {
|
|||||||
# a way to parse command line parameters, using a
|
# a way to parse command line parameters, using a
|
||||||
# schema to configure Getopt::Long
|
# schema to configure Getopt::Long
|
||||||
sub get_options {
|
sub get_options {
|
||||||
my ($schema, $args, $uri_param, $pwcallback, $list_param) = @_;
|
my ($schema, $args, $arg_param, $fixed_param, $pwcallback) = @_;
|
||||||
|
|
||||||
if (!$schema || !$schema->{properties}) {
|
if (!$schema || !$schema->{properties}) {
|
||||||
raise("too many arguments\n", code => HTTP_BAD_REQUEST)
|
raise("too many arguments\n", code => HTTP_BAD_REQUEST)
|
||||||
@ -857,11 +857,19 @@ sub get_options {
|
|||||||
return {};
|
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 = ();
|
my @getopt = ();
|
||||||
foreach my $prop (keys %{$schema->{properties}}) {
|
foreach my $prop (keys %{$schema->{properties}}) {
|
||||||
my $pd = $schema->{properties}->{$prop};
|
my $pd = $schema->{properties}->{$prop};
|
||||||
next if $list_param && $prop eq $list_param;
|
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) {
|
if ($prop eq 'password' && $pwcallback) {
|
||||||
# we do not accept plain password on input line, instead
|
# we do not accept plain password on input line, instead
|
||||||
@ -883,16 +891,21 @@ sub get_options {
|
|||||||
raise("unable to parse option\n", code => HTTP_BAD_REQUEST)
|
raise("unable to parse option\n", code => HTTP_BAD_REQUEST)
|
||||||
if !Getopt::Long::GetOptionsFromArray($args, $opts, @getopt);
|
if !Getopt::Long::GetOptionsFromArray($args, $opts, @getopt);
|
||||||
|
|
||||||
|
if (my $acount = scalar(@$args)) {
|
||||||
if ($list_param) {
|
if ($list_param) {
|
||||||
my $pd = $schema->{properties}->{$list_param} ||
|
$opts->{$list_param} = $args;
|
||||||
die "no schema for list_param";
|
|
||||||
|
|
||||||
$opts->{$list_param} = $args if scalar($args);
|
|
||||||
$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)
|
raise("too many arguments\n", code => HTTP_BAD_REQUEST)
|
||||||
if scalar(@$args) != 0;
|
if scalar(@$args) != 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (my $pd = $schema->{properties}->{password}) {
|
if (my $pd = $schema->{properties}->{password}) {
|
||||||
if ($pd->{type} ne 'boolean' && $pwcallback) {
|
if ($pd->{type} ne 'boolean' && $pwcallback) {
|
||||||
@ -935,8 +948,8 @@ sub get_options {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach my $p (keys %$uri_param) {
|
foreach my $p (keys %$fixed_param) {
|
||||||
$opts->{$p} = $uri_param->{$p};
|
$opts->{$p} = $fixed_param->{$p};
|
||||||
}
|
}
|
||||||
|
|
||||||
return $opts;
|
return $opts;
|
||||||
|
@ -477,29 +477,9 @@ sub cli_handler {
|
|||||||
|
|
||||||
my $info = $self->map_method_by_name($name);
|
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;
|
my $res;
|
||||||
eval {
|
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);
|
$res = $self->handle($info, $param);
|
||||||
};
|
};
|
||||||
if (my $err = $@) {
|
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
|
libpve-common-perl (1.0-7) unstable; urgency=low
|
||||||
|
|
||||||
* bug fixes (see git log)
|
* bug fixes (see git log)
|
||||||
|
Loading…
Reference in New Issue
Block a user