From: Dietmar Maurer Date: Wed, 23 Nov 2011 07:37:27 +0000 (+0100) Subject: make cli argument parser more flexible X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=0ce82909f4c554e088d9a15eafc555e68d0f334c make cli argument parser more flexible --- diff --git a/Makefile b/Makefile index 1350f02..363fdaa 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ RELEASE=2.0 VERSION=1.0 -PKGREL=7 +PKGREL=8 PACKAGE=libpve-common-perl diff --git a/data/PVE/JSONSchema.pm b/data/PVE/JSONSchema.pm index a3dc61e..7decdb5 100644 --- a/data/PVE/JSONSchema.pm +++ b/data/PVE/JSONSchema.pm @@ -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; diff --git a/data/PVE/RESTHandler.pm b/data/PVE/RESTHandler.pm index e68db35..d468a42 100644 --- a/data/PVE/RESTHandler.pm +++ b/data/PVE/RESTHandler.pm @@ -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 = $@) { diff --git a/debian/changelog b/debian/changelog index 43e194b..5e802bf 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +libpve-common-perl (1.0-8) unstable; urgency=low + + * make cli argument parser more flexible + + -- Proxmox Support Team Wed, 23 Nov 2011 08:36:30 +0100 + libpve-common-perl (1.0-7) unstable; urgency=low * bug fixes (see git log)