+ } else {
+ die "unknown format '$format'";
+ }
+
+ return $res;
+};
+
+# translate parameter mapping definition
+# $mapping_array is a array which can contain:
+# strings ... in that case we assume it is a parameter name, and
+# we want to load that parameter from a file
+# [ param_name, func, desc] ... allows you to specify a arbitrary
+# mapping func for any param
+#
+# Returns: a hash indexed by parameter_name,
+# i.e. { param_name => { func => .., desc => ... } }
+my $compute_param_mapping_hash = sub {
+ my ($mapping_array) = @_;
+
+ my $res = {};
+
+ return $res if !defined($mapping_array);
+
+ foreach my $item (@$mapping_array) {
+ my ($name, $func, $desc, $interactive);
+ if (ref($item) eq 'ARRAY') {
+ ($name, $func, $desc, $interactive) = @$item;
+ } elsif (ref($item) eq 'HASH') {
+ # just use the hash
+ $res->{$item->{name}} = $item;
+ next;
+ } else {
+ $name = $item;
+ $func = sub { return PVE::Tools::file_get_contents($_[0]) };
+ }
+ $desc //= '<filepath>';
+ $res->{$name} = { desc => $desc, func => $func, interactive => $interactive };
+ }
+
+ return $res;
+};
+
+# generate usage information for command line tools
+#
+# $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)
+# $fixed_param ... do not generate and info about those parameters
+# $format:
+# 'long' ... default (text, list all options)
+# 'short' ... command line only (text, one line)
+# 'full' ... text, include description
+# 'asciidoc' ... generate asciidoc for man pages (like 'full')
+# $param_cb ... mapping for string parameters to file path parameters
+# $formatter_properties ... additional property definitions (passed to output formatter)
+sub getopt_usage {
+ my ($info, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties) = @_;
+
+ $format = 'long' if !$format;
+
+ my $schema = $info->{parameters};
+ my $name = $info->{name};
+ my $prop = {};
+ if ($schema->{properties}) {
+ $prop = { %{$schema->{properties}} }; # copy
+ }
+
+ my $has_output_format_option = $formatter_properties->{'output-format'} ? 1 : 0;
+
+ if ($formatter_properties) {
+ foreach my $key (keys %$formatter_properties) {
+ if (!$standard_output_options->{$key}) {
+ $prop->{$key} = $formatter_properties->{$key};
+ }
+ }
+ }
+
+ # also remove $standard_output_options from $prop (pvesh, pveclient)
+ if ($prop->{'output-format'}) {
+ $has_output_format_option = 1;
+ foreach my $key (keys %$prop) {
+ if ($standard_output_options->{$key}) {
+ delete $prop->{$key};
+ }
+ }
+ }
+
+ my $out = '';
+
+ my $arg_hash = {};
+
+ my $args = '';
+
+ $arg_param = [ $arg_param ] if $arg_param && !ref($arg_param);
+
+ foreach my $p (@$arg_param) {
+ next if !$prop->{$p}; # just to be sure
+ my $pd = $prop->{$p};
+
+ $arg_hash->{$p} = 1;
+ $args .= " " if $args;
+ if ($pd->{format} && $pd->{format} =~ m/-list/) {
+ $args .= "{<$p>}";
+ } else {
+ $args .= $pd->{optional} ? "[<$p>]" : "<$p>";
+ }
+ }