# A real command is always an array consisting of its class, name, array of
# position fixed (required) parameters and hash of predefined parameters when
# mapping a CLI command t o an API call. Optionally an output method can be
-# passed at the end, e.g., for formatting or transformation purpose.
+# passed at the end, e.g., for formatting or transformation purpose, and
+# a schema of additional properties/arguments which are added to
+# the method schema and gets passed to the formatter.
#
# [class, name, fixed_params, API_pre-set params, output_sub ]
#
foreach my $cmd (&$sortfunc($def)) {
if (ref($def->{$cmd}) eq 'ARRAY') {
- my ($class, $name, $arg_param, $fixed_param) = @{$def->{$cmd}};
+ my ($class, $name, $arg_param, $fixed_param, undef, $formatter_properties) = @{$def->{$cmd}};
$str .= $separator if $oldclass && $oldclass ne $class;
$str .= $indent;
$str .= $class->usage_str($name, "$prefix $cmd", $arg_param,
- $fixed_param, $format, $param_cb);
+ $fixed_param, $format, $param_cb, $formatter_properties);
$oldclass = $class;
} elsif (defined($def->{$cmd}->{alias}) && ($format eq 'asciidoc')) {
} else {
next if $def->{$cmd}->{alias};
- my $substr = $generate->($indent, $separator, $def->{$cmd}, "$prefix $cmd");
+ my $substr = $generate->($indent, '', $def->{$cmd}, "$prefix $cmd");
if ($substr) {
$substr .= $separator if $substr !~ /\Q$separator\E{2}/;
$str .= $substr;
}
} else {
- my ($class, $name, $arg_param, $fixed_param) = @$def;
+ my ($class, $name, $arg_param, $fixed_param, undef, $formatter_properties) = @$def;
$abort->("unknown command '$cmd'") if !$class;
$str .= $indent;
- $str .= $class->usage_str($name, $prefix, $arg_param, $fixed_param, $format, $param_cb);
+ $str .= $class->usage_str($name, $prefix, $arg_param, $fixed_param, $format, $param_cb, $formatter_properties);
}
return $str;
};
$assert_initialized->();
print $fd "ERROR: $msg\n" if $msg;
- print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
+ print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
- print {$fd} generate_usage_str('short', $cmd, ' ' x 7, "\n", sub {
+ print {$fd} generate_usage_str('short', $cmd, ' ' x 7, $cmd ? '' : "\n", sub {
my ($h) = @_;
return sort {
if (ref($h->{$a}) eq 'ARRAY' && ref($h->{$b}) eq 'ARRAY') {
my $skip_param = {};
- my ($class, $name, $arg_param, $uri_param) = @$def;
+ my ($class, $name, $arg_param, $uri_param, undef, $formatter_properties) = @$def;
$arg_param //= [];
$uri_param //= {};
my $info = $class->map_method_by_name($name);
- my $prop = $info->{parameters}->{properties};
+ my $prop = { %{$info->{parameters}->{properties}} }; # copy
+ $prop = { %$prop, %$formatter_properties } if $formatter_properties;
my $print_parameter_completion = sub {
my ($pname) = @_;
__EOD__
}
+sub generate_zsh_completions {
+ my ($class) = @_;
+
+ # generate zsh completion config
+
+ $exename = &$get_exe_name($class);
+
+ print <<__EOD__;
+#compdef _$exename $exename
+
+function _$exename() {
+ local cwords line point cmd curr prev
+ cwords=\${#words[@]}
+ line=\$words
+ point=\${#line}
+ cmd=\${words[1]}
+ curr=\${words[cwords]}
+ prev=\${words[cwords-1]}
+ compadd \$(COMP_CWORD="\$cwords" COMP_LINE="\$line" COMP_POINT="\$point" \\
+ $exename bashcomplete "\$cmd" "\$curr" "\$prev")
+}
+__EOD__
+}
+
sub generate_asciidoc_synopsys {
my ($class) = @_;
$class->generate_asciidoc_synopsis();
&$preparefunc() if $preparefunc;
- my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def || []};
+ my ($class, $name, $arg_param, $uri_param, $outsub, $formatter_properties) = @{$def || []};
$abort->("unknown command '$cmd_str'") if !$class;
- my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_cb);
+ my ($res, $formatter_params) = $class->cli_handler(
+ $cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_cb, $formatter_properties);
if (defined $outsub) {
my $result_schema = $class->map_method_by_name($name)->{returns};
- $outsub->($res, $result_schema);
+ $outsub->($res, $result_schema, $formatter_params);
}
};
my $handle_simple_cmd = sub {
my ($args, $preparefunc, $param_cb) = @_;
- my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef};
+ my ($class, $name, $arg_param, $uri_param, $outsub, $formatter_properties) = @{$cmddef};
die "no class specified" if !$class;
if (scalar(@$args) >= 1) {
&$preparefunc() if $preparefunc;
- my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $param_cb);
+ my ($res, $formatter_params) = $class->cli_handler(
+ $name, $name, \@ARGV, $arg_param, $uri_param, $param_cb, $formatter_properties);
if (defined $outsub) {
my $result_schema = $class->map_method_by_name($name)->{returns};
- $outsub->($res, $result_schema);
+ $outsub->($res, $result_schema, $formatter_params);
}
};