From: Dietmar Maurer Date: Wed, 12 Oct 2011 09:40:45 +0000 (+0200) Subject: allow list format in getopts X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=8ba7c72bd1e69ee6212a84769c4c637ecfbd3dd7 allow list format in getopts --- diff --git a/data/PVE/JSONSchema.pm b/data/PVE/JSONSchema.pm index 3d51b4e..66a7833 100644 --- a/data/PVE/JSONSchema.pm +++ b/data/PVE/JSONSchema.pm @@ -82,6 +82,9 @@ sub register_format { } # register some common type for pve + +register_format('string', sub {}); # allow format => 'string-list' + register_format('pve-configid', \&pve_verify_configid); sub pve_verify_configid { my ($id, $noerr) = @_; @@ -867,7 +870,11 @@ sub get_options { } elsif ($pd->{type} eq 'boolean') { push @getopt, "$prop:s"; } else { - push @getopt, "$prop=s"; + if ($pd->{format} && $pd->{format} =~ m/-list/) { + push @getopt, "$prop=s@"; + } else { + push @getopt, "$prop=s"; + } } } @@ -898,6 +905,22 @@ sub get_options { } else { raise("unable to parse boolean option\n", code => HTTP_BAD_REQUEST); } + } elsif ($pd->{format} && $pd->{format} =~ m/-list/) { + + if ($pd->{format} eq 'pve-vmid-list') { + # allow --vmid 100 --vmid 101 and --vmid 100,101 + $opts->{$p} = join(",", @{$opts->{$p}}); + } else { + # we encode array as \0 separated strings + # Note: CGI.pm also use this encoding + if (scalar(@{$opts->{$p}}) != 1) { + $opts->{$p} = join("\0", @{$opts->{$p}}); + } else { + # st that split_list knows it is \0 terminated + my $v = $opts->{$p}->[0]; + $opts->{$p} = "$v\0"; + } + } } } }