Skip to content

Commit 77e56f6

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 77e56f6

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
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

+54-14
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,41 @@ 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);
33+
if cmd.is_flatten_help_set() {
34+
let mut ord_v = Vec::new();
35+
let mut first = true;
36+
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)));
3645

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(),
44-
));
45-
line.push(roman(rhs));
46-
}
47-
roff.text(line);
46+
for (_, name, cmd) in ord_v {
47+
if !first {
48+
roff.control("br", []);
49+
} else {
50+
first = false;
51+
}
52+
roff.text(usage(cmd, name));
53+
}
54+
} else {
55+
let mut line = usage(cmd, cmd.get_bin_name().unwrap_or_else(|| cmd.get_name()));
56+
57+
if cmd.has_subcommands() {
58+
let (lhs, rhs) = subcommand_markers(cmd);
59+
line.push(roman(lhs));
60+
line.push(italic(
61+
cmd.get_subcommand_value_name()
62+
.unwrap_or_else(|| subcommand_heading(cmd))
63+
.to_lowercase(),
64+
));
65+
line.push(roman(rhs));
66+
}
67+
roff.text(line);
4868
}
4969
}
5070

@@ -226,6 +246,26 @@ pub(crate) fn subcommands(roff: &mut Roff, cmd: &clap::Command, section: &str) {
226246
}
227247
}
228248

249+
pub(crate) fn flat_subcommands(roff: &mut Roff, cmd: &clap::Command) {
250+
for sub in cmd.get_subcommands().filter(|s| !s.is_hide_set()) {
251+
roff.control("TP", []);
252+
253+
let mut line = usage(sub, sub.get_name());
254+
255+
if let Some(about) = sub.get_long_about().or_else(|| sub.get_about()) {
256+
line.push(roman("\n"));
257+
line.push(roman(about.to_string()));
258+
}
259+
260+
if let Some(after_help) = sub.get_after_help() {
261+
line.push(roman("\n"));
262+
line.push(roman(after_help.to_string()));
263+
}
264+
265+
roff.text(line);
266+
}
267+
}
268+
229269
pub(crate) fn version(cmd: &clap::Command) -> String {
230270
format!(
231271
"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)