[pve-devel] [RFC pve-common v2 1/2] avoid parameter name collissions (renamed 'format' to 'output-format')

Dietmar Maurer dietmar at proxmox.com
Mon Jul 23 14:12:59 CEST 2018


Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>

Changes in v2:
use 'die' instead of 'warn'
add new schema property addStandardCliParameters

---
 src/PVE/CLIFormatter.pm |  2 +-
 src/PVE/RESTHandler.pm  | 61 +++++++++++++++++++++++++++----------------------
 2 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/src/PVE/CLIFormatter.pm b/src/PVE/CLIFormatter.pm
index 7a92f7a..22cd8f2 100644
--- a/src/PVE/CLIFormatter.pm
+++ b/src/PVE/CLIFormatter.pm
@@ -374,7 +374,7 @@ sub print_api_result {
 	$options = { %$options }; # copy
     }
 
-    my $format = $options->{format} // 'text';
+    my $format = $options->{'output-format'} // 'text';
 
     return if $result_schema->{type} eq 'null';
 
diff --git a/src/PVE/RESTHandler.pm b/src/PVE/RESTHandler.pm
index 5d0c03d..1d6b206 100644
--- a/src/PVE/RESTHandler.pm
+++ b/src/PVE/RESTHandler.pm
@@ -17,6 +17,33 @@ my $method_path_lookup = {};
 
 our $AUTOLOAD;  # it's a package global
 
+our $standard_output_options = {
+    'output-format' => PVE::JSONSchema::get_standard_option('pve-output-format'),
+    noheader => {
+	description => "Do not show column headers (for 'text' format).",
+	type => 'boolean',
+	optional => 1,
+	default => 1,
+    },
+    noborder => {
+	description => "Do not draw borders (for 'text' format).",
+	type => 'boolean',
+	optional => 1,
+	default => 1,
+    },
+    quiet => {
+        description => "Suppress printing results.",
+        type => 'boolean',
+        optional => 1,
+    },
+    'human-readable' => {
+        description => "Call output rendering functions to produce human readable text.",
+        type => 'boolean',
+        optional => 1,
+	default => 1,
+    }
+};
+
 sub api_clone_schema {
     my ($schema) = @_;
 
@@ -193,6 +220,13 @@ sub validate_method_schemas {
 
 	foreach my $info (@$ma) {
 	    PVE::JSONSchema::validate_method_info($info);
+	    if ($info->{parameters} && (my $pdef = $info->{parameters}->{properties})) {
+		foreach my $opt (keys %$standard_output_options) {
+		    if (defined($pdef->{$opt})) {
+			die "detected overwriten standard CLI parameter '$opt' in ${class}->$info->{name}\n";
+		    }
+		}
+	    }
 	}
     }
 }
@@ -746,33 +780,6 @@ my $replace_file_names_with_contents = sub {
     return $param;
 };
 
-our $standard_output_options = {
-    format => PVE::JSONSchema::get_standard_option('pve-output-format'),
-    noheader => {
-	description => "Do not show column headers (for 'text' format).",
-	type => 'boolean',
-	optional => 1,
-	default => 1,
-    },
-    noborder => {
-	description => "Do not draw borders (for 'text' format).",
-	type => 'boolean',
-	optional => 1,
-	default => 1,
-    },
-    quiet => {
-        description => "Suppress printing results.",
-        type => 'boolean',
-        optional => 1,
-    },
-    'human-readable' => {
-        description => "Call output rendering functions to produce human readable text.",
-        type => 'boolean',
-        optional => 1,
-	default => 1,
-    }
-};
-
 sub add_standard_output_parameters {
     my ($org_schema) = @_;
 
-- 
2.11.0




More information about the pve-devel mailing list