From d70ef84623c18a5ec9f79990024b669b5a8d93b5 Mon Sep 17 00:00:00 2001 From: Paul Spooren Date: Thu, 17 Oct 2024 14:57:06 +0200 Subject: [PATCH] chore(mangen): split out usage() function The function prints the usage of a (sub)command. Signed-off-by: Paul Spooren --- clap_mangen/src/render.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/clap_mangen/src/render.rs b/clap_mangen/src/render.rs index 81d4e8c6b1cd..009750a7ff03 100644 --- a/clap_mangen/src/render.rs +++ b/clap_mangen/src/render.rs @@ -32,7 +32,24 @@ pub(crate) fn description(roff: &mut Roff, cmd: &clap::Command) { pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) { let name = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name()); let mut line = vec![bold(name), roman(" ")]; + let mut line = usage(cmd, name); + if cmd.has_subcommands() && !flatten { + let (lhs, rhs) = subcommand_markers(cmd); + line.push(roman(lhs)); + line.push(italic( + cmd.get_subcommand_value_name() + .unwrap_or_else(|| subcommand_heading(cmd)) + .to_lowercase(), + )); + line.push(roman(rhs)); + } + roff.text(line); + } +} + +fn usage(cmd: &clap::Command, name: &str) -> Vec { + let mut line = vec![bold(name), roman(" ")]; for opt in cmd.get_arguments().filter(|i| !i.is_hide_set()) { let (lhs, rhs) = option_markers(opt); match (opt.get_short(), opt.get_long()) { @@ -74,18 +91,7 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) { line.push(roman(" ")); } - if cmd.has_subcommands() { - let (lhs, rhs) = subcommand_markers(cmd); - line.push(roman(lhs)); - line.push(italic( - cmd.get_subcommand_value_name() - .unwrap_or_else(|| subcommand_heading(cmd)) - .to_lowercase(), - )); - line.push(roman(rhs)); - } - - roff.text(line); + line } pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {