Skip to content

Commit 78a738b

Browse files
aparcarepage
authored andcommitted
refactor(mangen): Split out usage() function
The function prints the usage of a (sub)command. Signed-off-by: Paul Spooren <[email protected]>
1 parent 3829964 commit 78a738b

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

clap_mangen/src/render.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,23 @@ pub(crate) fn description(roff: &mut Roff, cmd: &clap::Command) {
3131

3232
pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
3333
let name = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name());
34-
let mut line = vec![bold(name), roman(" ")];
34+
let mut line = usage(cmd, name);
35+
36+
if cmd.has_subcommands() {
37+
let (lhs, rhs) = subcommand_markers(cmd);
38+
line.push(roman(lhs));
39+
line.push(italic(
40+
cmd.get_subcommand_value_name()
41+
.unwrap_or_else(|| subcommand_heading(cmd))
42+
.to_lowercase(),
43+
));
44+
line.push(roman(rhs));
45+
}
46+
roff.text(line);
47+
}
3548

49+
fn usage(cmd: &clap::Command, name: &str) -> Vec<Inline> {
50+
let mut line = vec![bold(name), roman(" ")];
3651
for opt in cmd.get_arguments().filter(|i| !i.is_hide_set()) {
3752
let (lhs, rhs) = option_markers(opt);
3853
match (opt.get_short(), opt.get_long()) {
@@ -74,18 +89,7 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
7489
line.push(roman(" "));
7590
}
7691

77-
if cmd.has_subcommands() {
78-
let (lhs, rhs) = subcommand_markers(cmd);
79-
line.push(roman(lhs));
80-
line.push(italic(
81-
cmd.get_subcommand_value_name()
82-
.unwrap_or_else(|| subcommand_heading(cmd))
83-
.to_lowercase(),
84-
));
85-
line.push(roman(rhs));
86-
}
87-
88-
roff.text(line);
92+
line
8993
}
9094

9195
pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) {

0 commit comments

Comments
 (0)