From 1d21344cf5daea0633f9e13a510e49f2bf6e8532 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 14 Oct 2011 08:29:39 +0200 Subject: [PATCH] improve get_options (allow argument lists) So far it was possiple to specific lists by using option multiple times. vzdump --vmid 100 --vmid 101 or vzdump -vmid 100,101 No we can pass the vm list as arguments (not options) vzdump 100 101 --- data/PVE/JSONSchema.pm | 11 ++++++++++- data/PVE/RESTHandler.pm | 29 ++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/data/PVE/JSONSchema.pm b/data/PVE/JSONSchema.pm index 66a7833..e87c22e 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) = @_; + my ($schema, $args, $uri_param, $pwcallback, $list_param) = @_; if (!$schema || !$schema->{properties}) { raise("too many arguments\n", code => HTTP_BAD_REQUEST) @@ -860,6 +860,7 @@ sub get_options { my @getopt = (); foreach my $prop (keys %{$schema->{properties}}) { my $pd = $schema->{properties}->{$prop}; + next if $prop eq $list_param; next if defined($uri_param->{$prop}); if ($prop eq 'password' && $pwcallback) { @@ -882,6 +883,14 @@ sub get_options { 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; + $args = []; + } + raise("too many arguments\n", code => HTTP_BAD_REQUEST) if scalar(@$args) != 0; diff --git a/data/PVE/RESTHandler.pm b/data/PVE/RESTHandler.pm index 47f66af..7e0352c 100644 --- a/data/PVE/RESTHandler.pm +++ b/data/PVE/RESTHandler.pm @@ -344,7 +344,8 @@ sub handle { # # $name ... the name of the method # $prefix ... usually something like "$exename $cmd" ('pvesm add') -# $arg_param ... list of parameters we want to get as ordered arguments on the command line +# $arg_param ... list of parameters we want to get as ordered arguments +# on the command line (or single parameter name for lists) # $fixed_param ... do not generate and info about those parameters # $format: # 'long' ... default (list all options) @@ -365,12 +366,20 @@ sub usage_str { my $arg_hash = {}; my $args = ''; + + $arg_param = [ $arg_param ] if $arg_param && !ref($arg_param); + foreach my $p (@$arg_param) { next if !$prop->{$p}; # just to be sure + my $pd = $prop->{$p}; $arg_hash->{$p} = 1; $args .= " " if $args; - $args .= $prop->{$p} && $prop->{$p}->{optional} ? "[<$p>]" : "<$p>"; + if ($pd->{format} && $pd->{format} =~ m/-list/) { + $args .= "{}"; + } else { + $args .= $pd->{optional} ? "[<$p>]" : "<$p>"; + } } my $get_prop_descr = sub { @@ -473,13 +482,23 @@ sub cli_handler { $param->{$p} = $fixed_param->{$p}; } - foreach my $p (@$arg_param) { - $param->{$p} = shift @$args if $args->[0] && $args->[0] !~ m/^-/; + 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/^-/; + } + } 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); + my $param = PVE::JSONSchema::get_options($info->{parameters}, $args, $param, $pwcallback, $list_param); $res = $self->handle($info, $param); }; -- 2.39.2