]> git.proxmox.com Git - pve-common.git/commitdiff
make cli argument parser more flexible
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 23 Nov 2011 07:37:27 +0000 (08:37 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 23 Nov 2011 07:37:27 +0000 (08:37 +0100)
Makefile
data/PVE/JSONSchema.pm
data/PVE/RESTHandler.pm
debian/changelog

index 1350f02d80c3a0b3f2eb087bd855d57369b40f13..363fdaa184564465c2610169adf4aa6d15a72783 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 RELEASE=2.0
 
 VERSION=1.0
-PKGREL=7
+PKGREL=8
 
 PACKAGE=libpve-common-perl
 
index a3dc61ec5f07c7251541c91672a11a117fb9156b..7decdb51328b61d54071e1af27f9e53c28e403b1 100644 (file)
@@ -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;
index e68db35571798028cd0770c53014be9af6af31ab..d468a4225b764b4661150edae3c033e9c5144fb8 100644 (file)
@@ -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 = $@) {
index 43e194b4bbcfa8aa13544b7109de9f30d374c5db..5e802bfaefaf1246e3ca5f91018a1a827dbc9b66 100644 (file)
@@ -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
 
   * bug fixes (see git log)