Skip to content

Commit

Permalink
chore(mangen): Split out usage() function
Browse files Browse the repository at this point in the history
The function prints the usage of a (sub)command.

Signed-off-by: Paul Spooren <[email protected]>
  • Loading branch information
aparcar committed Oct 23, 2024
1 parent 7685641 commit cec57c8
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions clap_mangen/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Inline> {
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()) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cec57c8

Please sign in to comment.