Skip to content

Commit 5094ea2

Browse files
committed
feat(mangen): Support flatten_help
The `flatten_help` argument combines all subcommands on a single page. Until now this wasn't supported for `mangen`. With this command the sections `SYNOPSIS` as well as `SUBCOMMANDS` are changed to imitate the style of `git stash --help`. Signed-off-by: Paul Spooren <mail@aparcar.org>
1 parent d70ef84 commit 5094ea2

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

clap_mangen/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ impl Man {
258258
fn _render_subcommands_section(&self, roff: &mut Roff) {
259259
let heading = subcommand_heading(&self.cmd);
260260
roff.control("SH", [heading]);
261-
render::subcommands(roff, &self.cmd, &self.section);
261+
if self.cmd.is_flatten_help_set() {
262+
render::flat_subcommands(roff, &self.cmd);
263+
} else {
264+
render::subcommands(roff, &self.cmd, &self.section);
265+
}
262266
}
263267

264268
/// Render the EXTRA section into the writer.

clap_mangen/src/render.rs

+58-13
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,46 @@ pub(crate) fn description(roff: &mut Roff, cmd: &clap::Command) {
3030
}
3131

3232
pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
33-
let name = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name());
34-
let mut line = vec![bold(name), roman(" ")];
35-
let mut line = usage(cmd, name);
36-
37-
if cmd.has_subcommands() && !flatten {
38-
let (lhs, rhs) = subcommand_markers(cmd);
39-
line.push(roman(lhs));
40-
line.push(italic(
41-
cmd.get_subcommand_value_name()
42-
.unwrap_or_else(|| subcommand_heading(cmd))
43-
.to_lowercase(),
33+
let flatten = cmd.is_flatten_help_set();
34+
35+
let mut ord_v = Vec::new();
36+
if flatten {
37+
for subcommand in cmd.get_subcommands() {
38+
ord_v.push((
39+
subcommand.get_display_order(),
40+
subcommand.get_bin_name().unwrap_or_else(|| cmd.get_name()),
41+
subcommand,
42+
));
43+
}
44+
ord_v.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)));
45+
} else {
46+
ord_v.push((
47+
cmd.get_display_order(),
48+
cmd.get_bin_name().unwrap_or_else(|| cmd.get_name()),
49+
cmd,
4450
));
45-
line.push(roman(rhs));
4651
}
47-
roff.text(line);
52+
53+
let mut first = true;
54+
for (_, name, cmd) in ord_v {
55+
if !first && flatten {
56+
roff.control("br", []);
57+
} else {
58+
first = false;
59+
}
60+
let mut line = usage(cmd, name);
61+
62+
if cmd.has_subcommands() && !flatten {
63+
let (lhs, rhs) = subcommand_markers(cmd);
64+
line.push(roman(lhs));
65+
line.push(italic(
66+
cmd.get_subcommand_value_name()
67+
.unwrap_or_else(|| subcommand_heading(cmd))
68+
.to_lowercase(),
69+
));
70+
line.push(roman(rhs));
71+
}
72+
roff.text(line);
4873
}
4974
}
5075

@@ -226,6 +251,26 @@ pub(crate) fn subcommands(roff: &mut Roff, cmd: &clap::Command, section: &str) {
226251
}
227252
}
228253

254+
pub(crate) fn flat_subcommands(roff: &mut Roff, cmd: &clap::Command) {
255+
for sub in cmd.get_subcommands().filter(|s| !s.is_hide_set()) {
256+
roff.control("TP", []);
257+
258+
let mut line = usage(sub, sub.get_name());
259+
260+
if let Some(about) = sub.get_long_about().or_else(|| sub.get_about()) {
261+
line.push(roman("\n"));
262+
line.push(roman(about.to_string()));
263+
}
264+
265+
if let Some(after_help) = sub.get_after_help() {
266+
line.push(roman("\n"));
267+
line.push(roman(after_help.to_string()));
268+
}
269+
270+
roff.text(line);
271+
}
272+
}
273+
229274
pub(crate) fn version(cmd: &clap::Command) -> String {
230275
format!(
231276
"v{}",

clap_mangen/tests/snapshots/flatten_help.roff

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
.SH NAME
55
my\-app
66
.SH SYNOPSIS
7-
\fBmy\-app\fR [\fB\-c \fR] [\fB\-v \fR] [\fB\-h\fR|\fB\-\-help\fR] [\fIsubcommands\fR]
7+
\fBmy\-app test\fR [\fB\-d \fR]... [\fB\-c \fR] [\fB\-h\fR|\fB\-\-help\fR]
8+
.br
9+
\fBmy\-app help\fR
810
.SH DESCRIPTION
911
.SH OPTIONS
1012
.TP
@@ -18,9 +20,9 @@ my\-app
1820
Print help
1921
.SH SUBCOMMANDS
2022
.TP
21-
my\-app\-test(1)
23+
\fBtest\fR [\fB\-d \fR]... [\fB\-c \fR] [\fB\-h\fR|\fB\-\-help\fR]
2224
Subcommand
2325
with a second line
2426
.TP
25-
my\-app\-help(1)
27+
\fBhelp\fR
2628
Print this message or the help of the given subcommand(s)

0 commit comments

Comments
 (0)