]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/RESTHandler.pm
cli_handler: pass common output options as separate parameter
[pve-common.git] / src / PVE / RESTHandler.pm
index c303c389e4da1273122786a2e9274ccd00c4a0d6..af9024eacaed59da1320e51225b11c337d5afaee 100644 (file)
@@ -394,7 +394,7 @@ sub find_handler {
 }
 
 sub handle {
-    my ($self, $info, $param) = @_;
+    my ($self, $info, $param, $output_options) = @_;
 
     my $func = $info->{code};
 
@@ -414,7 +414,8 @@ sub handle {
        $param->{'extra-args'} = [map { /^(.*)$/ } @$extra] if $extra;
     }
 
-    my $result = &$func($param); 
+    $output_options //= {};
+    my $result = &$func($param, $output_options);
 
     # todo: this is only to be safe - disable?
     if (my $schema = $info->{returns}) {
@@ -566,7 +567,7 @@ my $compute_param_mapping_hash = sub {
 
 # generate usage information for command line tools
 #
-# $name        ... the name of the method
+# $info        ... method info
 # $prefix      ... usually something like "$exename $cmd" ('pvesm add')
 # $arg_param   ... list of parameters we want to get as ordered arguments 
 #                  on the command line (or single parameter name for lists)
@@ -577,13 +578,13 @@ my $compute_param_mapping_hash = sub {
 #   'full'     ... text, include description
 #   'asciidoc' ... generate asciidoc for man pages (like 'full')
 # $param_cb ... mapping for string parameters to file path parameters
-sub usage_str {
-    my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $param_cb) = @_;
+sub getopt_usage {
+    my ($info, $prefix, $arg_param, $fixed_param, $format, $param_cb) = @_;
 
     $format = 'long' if !$format;
 
-    my $info = $self->map_method_by_name($name);
     my $schema = $info->{parameters};
+    my $name = $info->{name};
     my $prop = $schema->{properties};
 
     my $out = '';
@@ -679,6 +680,14 @@ sub usage_str {
     return $out;
 }
 
+sub usage_str {
+    my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $param_cb) = @_;
+
+    my $info = $self->map_method_by_name($name);
+
+    return getopt_usage($info, $prefix, $arg_param, $fixed_param, $format, $param_cb);
+}
+
 # generate docs from JSON schema properties
 sub dump_properties {
     my ($prop, $format, $style, $filterFn) = @_;
@@ -737,22 +746,64 @@ 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) = @_;
+
+    my $schema = { %$org_schema };
+    $schema->{properties} = { %{$schema->{properties}}, %$standard_output_options };
+
+    return $schema;
+};
+
 sub cli_handler {
-    my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $param_cb) = @_;
+    my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $param_cb, $options) = @_;
 
     my $info = $self->map_method_by_name($name);
+    $options //= {};
 
     my $res;
     eval {
        my $param_map = {};
        $param_map = $compute_param_mapping_hash->($param_cb->($name)) if $param_cb;
-       my $param = PVE::JSONSchema::get_options($info->{parameters}, $args, $arg_param, $fixed_param, $param_map);
+       my $schema = add_standard_output_parameters($info->{parameters});
+       my $param = PVE::JSONSchema::get_options($schema, $args, $arg_param, $fixed_param, $param_map);
+
+       foreach my $opt (keys %$standard_output_options) {
+           $options->{$opt} = delete $param->{$opt} if defined($param->{$opt});
+       }
 
        if (defined($param_map)) {
            $replace_file_names_with_contents->($param, $param_map);
        }
 
-       $res = $self->handle($info, $param);
+       $res = $self->handle($info, $param, $options);
     };
     if (my $err = $@) {
        my $ec = ref($err);