CliCommandMap: add usage_skip_options to suppress options in generate_usage_str()

This is useful for commands where sub-commands all takes the same options.
You can supress them in the generated manual pages (printdoc), and instead
document them somewhere else.
This commit is contained in:
Dietmar Maurer 2021-02-05 12:23:25 +01:00
parent 50e9148853
commit d014c6f2d1
3 changed files with 20 additions and 4 deletions

View File

@ -356,7 +356,7 @@ fn prepare_cli_command(def: &CommandLineInterface) -> (String, Vec<String>) {
if args[0] == "printdoc" {
let usage = match def {
CommandLineInterface::Simple(cli_cmd) => {
generate_usage_str(&prefix, &cli_cmd, DocumentationFormat::ReST, "")
generate_usage_str(&prefix, &cli_cmd, DocumentationFormat::ReST, "", &[])
}
CommandLineInterface::Nested(map) => {
generate_nested_usage(&prefix, &map, DocumentationFormat::ReST)

View File

@ -60,12 +60,17 @@ pub fn generate_usage_str(
cli_cmd: &CliCommand,
format: DocumentationFormat,
indent: &str,
skip_options: &'static [&'static str],
) -> String {
let arg_param = cli_cmd.arg_param;
let fixed_param = &cli_cmd.fixed_param;
let schema = cli_cmd.info.parameters;
let mut done_hash = HashSet::<&str>::new();
for option in skip_options {
done_hash.insert(option);
}
let mut args = String::new();
for positional_arg in arg_param {
@ -178,7 +183,7 @@ pub fn generate_usage_str(
/// Print command usage for simple commands to ``stderr``.
pub fn print_simple_usage_error(prefix: &str, cli_cmd: &CliCommand, err_msg: &str) {
let usage = generate_usage_str(prefix, cli_cmd, DocumentationFormat::Long, "");
let usage = generate_usage_str(prefix, cli_cmd, DocumentationFormat::Long, "", &[]);
eprint!("Error: {}\nUsage: {}", err_msg, usage);
}
@ -197,6 +202,8 @@ pub fn generate_nested_usage(
let mut cmds: Vec<&String> = def.commands.keys().collect();
cmds.sort();
let skip_options = def.usage_skip_options;
let mut usage = String::new();
for cmd in cmds {
@ -211,7 +218,7 @@ pub fn generate_nested_usage(
if !usage.is_empty() && format == DocumentationFormat::ReST {
usage.push_str("----\n\n");
}
usage.push_str(&generate_usage_str(&new_prefix, cli_cmd, format, ""));
usage.push_str(&generate_usage_str(&new_prefix, cli_cmd, format, "", skip_options));
}
CommandLineInterface::Nested(map) => {
usage.push_str(&generate_nested_usage(&new_prefix, map, format));
@ -268,7 +275,7 @@ pub fn print_help(
CommandLineInterface::Simple(cli_cmd) => {
println!(
"Usage: {}",
generate_usage_str(&prefix, cli_cmd, format, "")
generate_usage_str(&prefix, cli_cmd, format, "", &[])
);
}
}

View File

@ -102,6 +102,8 @@ pub struct CliCommandMap {
/// command definitions.
pub commands: HashMap<String, CommandLineInterface>,
pub aliases: Vec<(Vec<&'static str>, Vec<&'static str>)>,
/// List of options to suppress in generate_usage
pub usage_skip_options: &'static [&'static str],
}
impl CliCommandMap {
@ -121,6 +123,13 @@ impl CliCommandMap {
self
}
pub fn usage_skip_options(mut self, list: &'static [&'static str]) -> Self {
self.usage_skip_options = list;
self
}
/// Insert the help command.
pub fn insert_help(mut self) -> Self {
self.commands