+# format option, display type and description
+# $name: option name
+# $display_name: for example "-$name" of "<$name>", pass undef to use "$name:"
+# $phash: json schema property hash
+# $format: 'asciidoc', 'short', 'long' or 'full'
+# $style: 'config', 'config-sub', 'arg' or 'fixed'
+# $mapdef: parameter mapping ({ desc => XXX, func => sub {...} })
+my $get_property_description = sub {
+ my ($name, $style, $phash, $format, $hidepw, $mapdef) = @_;
+
+ my $res = '';
+
+ $format = 'asciidoc' if !defined($format);
+
+ my $descr = $phash->{description} || "no description available";
+
+ if ($phash->{verbose_description} &&
+ ($style eq 'config' || $style eq 'config-sub')) {
+ $descr = $phash->{verbose_description};
+ }
+
+ chomp $descr;
+
+ my $type_text = PVE::JSONSchema::schema_get_type_text($phash, $style);
+
+ if ($hidepw && $name eq 'password') {
+ $type_text = '';
+ }
+
+ if ($mapdef && $phash->{type} eq 'string') {
+ $type_text = $mapdef->{desc};
+ }
+
+ if ($format eq 'asciidoc') {
+
+ if ($style eq 'config') {
+ $res .= "`$name`: ";
+ } elsif ($style eq 'config-sub') {
+ $res .= "`$name`=";
+ } elsif ($style eq 'arg') {
+ $res .= "`--$name` ";
+ } elsif ($style eq 'fixed') {
+ $res .= "`<$name>`: ";
+ } else {
+ die "unknown style '$style'";
+ }
+
+ $res .= "`$type_text` " if $type_text;
+
+ if (defined(my $dv = $phash->{default})) {
+ $res .= "('default =' `$dv`)";
+ }
+
+ if ($style eq 'config-sub') {
+ $res .= ";;\n\n";
+ } else {
+ $res .= "::\n\n";
+ }
+
+ my $wdescr = $descr;
+ chomp $wdescr;
+ $wdescr =~ s/^$/+/mg;
+
+ $res .= $wdescr . "\n";
+
+ if (my $req = $phash->{requires}) {
+ my $tmp .= ref($req) ? join(', ', @$req) : $req;
+ $res .= "+\nNOTE: Requires option(s): `$tmp`\n";
+ }
+ $res .= "\n";
+
+ } elsif ($format eq 'short' || $format eq 'long' || $format eq 'full') {
+
+ my $defaulttxt = '';
+ if (defined(my $dv = $phash->{default})) {
+ $defaulttxt = " (default=$dv)";
+ }
+
+ my $display_name;
+ if ($style eq 'config') {
+ $display_name = "$name:";
+ } elsif ($style eq 'arg') {
+ $display_name = "-$name";
+ } elsif ($style eq 'fixed') {
+ $display_name = "<$name>";
+ } else {
+ die "unknown style '$style'";
+ }
+
+ my $tmp = sprintf " %-10s %s$defaulttxt\n", $display_name, "$type_text";
+ my $indend = " ";
+
+ $res .= Text::Wrap::wrap('', $indend, ($tmp));
+ $res .= "\n",
+ $res .= Text::Wrap::wrap($indend, $indend, ($descr)) . "\n\n";
+
+ if (my $req = $phash->{requires}) {
+ my $tmp = "Requires option(s): ";
+ $tmp .= ref($req) ? join(', ', @$req) : $req;
+ $res .= Text::Wrap::wrap($indend, $indend, ($tmp)). "\n\n";
+ }
+
+ } 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;
+};
+