return if $format eq 'regex';
- if ($format =~ m/^(.*)-list$/) {
+ if ($format =~ m/^(.*)-a?list$/) {
my $code = $format_list->{$1};
# 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)
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
} elsif ($pd->{type} eq 'boolean') {
push @getopt, "$prop:s";
} else {
- if ($pd->{format} && $pd->{format} =~ m/-list/) {
+ if ($pd->{format} && $pd->{format} =~ m/-a?list/) {
push @getopt, "$prop=s@";
} else {
push @getopt, "$prop=s";
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;
- $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}) {
} else {
raise("unable to parse boolean option\n", code => HTTP_BAD_REQUEST);
}
- } elsif ($pd->{format} && $pd->{format} =~ m/-list/) {
+ } elsif ($pd->{format}) {
- if ($pd->{format} eq 'pve-vmid-list') {
+ if ($pd->{format} =~ m/-list/) {
# allow --vmid 100 --vmid 101 and --vmid 100,101
+ # allow --dow mon --dow fri and --dow mon,fri
$opts->{$p} = join(",", @{$opts->{$p}});
- } else {
+ } elsif ($pd->{format} =~ m/-alist/) {
# we encode array as \0 separated strings
# Note: CGI.pm also use this encoding
if (scalar(@{$opts->{$p}}) != 1) {
}
}
- foreach my $p (keys %$uri_param) {
- $opts->{$p} = $uri_param->{$p};
+ foreach my $p (keys %$fixed_param) {
+ $opts->{$p} = $fixed_param->{$p};
}
return $opts;