[pve-devel] r5069 - pve-common/trunk

svn-commits at proxmox.com svn-commits at proxmox.com
Fri Aug 27 08:47:37 CEST 2010


Author: dietmar
Date: 2010-08-27 06:47:37 +0000 (Fri, 27 Aug 2010)
New Revision: 5069

Modified:
   pve-common/trunk/CLIHandler.pm
   pve-common/trunk/ChangeLog
   pve-common/trunk/RESTHandler.pm
Log:
	* RESTHandler.pm (cli_handler2): simplify code - allow to pass
	optional parameters as arguments.



Modified: pve-common/trunk/CLIHandler.pm
===================================================================
--- pve-common/trunk/CLIHandler.pm	2010-08-26 11:48:47 UTC (rev 5068)
+++ pve-common/trunk/CLIHandler.pm	2010-08-27 06:47:37 UTC (rev 5069)
@@ -22,6 +22,7 @@
 	    cmd => {
 		description => "Command name",
 		type => 'string',
+		optional => 1,
 	    },
 	},
     },
@@ -34,6 +35,11 @@
 
 	my $cmd = $param->{cmd};
 
+	if (!$cmd) {
+	    print_usage_short();
+	    return undef;
+	}
+
 	my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
 
 	raise_param_exc({ cmd => "no such command '$cmd'"}) if !$class;

Modified: pve-common/trunk/ChangeLog
===================================================================
--- pve-common/trunk/ChangeLog	2010-08-26 11:48:47 UTC (rev 5068)
+++ pve-common/trunk/ChangeLog	2010-08-27 06:47:37 UTC (rev 5069)
@@ -1,3 +1,8 @@
+2010-08-27  Proxmox Support Team  <support at proxmox.com>
+
+	* RESTHandler.pm (cli_handler2): simplify code - allow to pass
+	optional parameters as arguments.
+
 2010-08-26  Proxmox Support Team  <support at proxmox.com>
 
 	* RESTHandler.pm (usage_str): new '$hidepw' parameter to correctly

Modified: pve-common/trunk/RESTHandler.pm
===================================================================
--- pve-common/trunk/RESTHandler.pm	2010-08-26 11:48:47 UTC (rev 5068)
+++ pve-common/trunk/RESTHandler.pm	2010-08-27 06:47:37 UTC (rev 5069)
@@ -220,6 +220,8 @@
     $format = 'long' if !$format;
 
     my $info = $self->map_method_by_name($name);
+    my $schema = $info->{parameters};
+    my $prop = $schema->{properties};
 
     my $out = '';
 
@@ -229,26 +231,30 @@
     foreach my $p (@$arg_param) {
 	$arg_hash->{$p} = 1;
 	$args .= " " if $args;
-	$args .= "<$p>";
+	$args .= $prop->{$p} && $prop->{$p}->{optional} ? "[<$p>]" : "<$p>";
     }
 
     my $get_type_str = sub {
-	my ($phash) = @_;
+	my ($k) = @_;
 
+	my $phash = $prop->{$k};
+
 	my $type =  $phash->{format} || $phash->{type} || 'string';
 
 	return $type;
     };
 
     my $get_prop_descr = sub {
-	my ($phash, $display_name, $name) = @_;
+	my ($k, $display_name) = @_;
  
+	my $phash = $prop->{$k};
+
 	my $res = '';
 
 	my $descr = $phash->{description} || "no description available";
-	my $type =  &$get_type_str($phash);
+	my $type =  &$get_type_str($k);
 
-	if ($hidepw && $name eq 'password') {
+	if ($hidepw && $k eq 'password') {
 	    $type = '';
 	}
 
@@ -267,14 +273,11 @@
 	return $res;
     };
 
-    my $schema = $info->{parameters};
-    my $prop = $schema->{properties};
-
     my $argdescr = '';
     foreach my $k (@$arg_param) {
 	next if defined($fixed_param->{$k}); # just to be sure
 
-	$argdescr .= &$get_prop_descr($prop->{$k}, "<$k>", $k);
+	$argdescr .= &$get_prop_descr($k, "<$k>");
     }
 
     my $opts = '';
@@ -282,11 +285,11 @@
 	next if $arg_hash->{$k};
 	next if defined($fixed_param->{$k});
 
-	my $type =  &$get_type_str($prop->{$k});
+	my $type =  &$get_type_str($k);
 
 	next if $hidepw && ($k eq 'password') && !$prop->{$k}->{optional};
 
-	$opts .= &$get_prop_descr($prop->{$k}, "-$k", $k);
+	$opts .= &$get_prop_descr($k, "-$k");
 
 	if (!$prop->{$k}->{optional}) {
 	    $args .= " " if $args;
@@ -323,19 +326,7 @@
     }
 
     foreach my $p (@$arg_param) {
-	my $v = shift @$args;
-
-	if (!defined($v)) {
-	    my $usage = $self->usage_str($name, $prefix, $arg_param, $fixed_param, undef, $pwcallback);
-	    raise_param_exc({ $p => "got undefined value for required argument"}, $usage); 
-	}
-
-	if ($v =~ m/^-/) {
-	    my $usage = $self->usage_str($name, $prefix, $arg_param, $fixed_param, undef, $pwcallback);
-	    raise_param_exc({ $p => "required argument may not start with hyphen"}, $usage); 
-	}
-
-	$param->{$p} = $v;
+	$param->{$p} = shift @$args if $args->[0] && $args->[0] !~ m/^-/;
     }
 
     my $res;



More information about the pve-devel mailing list