From ba776e91cefd0feebe3b24c5fd91ad69e4623039 Mon Sep 17 00:00:00 2001 From: 0xadk <0xadk@users.noreply.github.com> Date: Sun, 22 Dec 2024 09:44:36 -0800 Subject: [PATCH] feat(mangen): Separate options based on `help_heading` --- clap_mangen/src/lib.rs | 34 +++++++++++++++++-- clap_mangen/src/render.rs | 4 +-- .../tests/snapshots/help_headings.bash.roff | 8 +++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/clap_mangen/src/lib.rs b/clap_mangen/src/lib.rs index 7b8d8af7900..5bfcb81cc33 100644 --- a/clap_mangen/src/lib.rs +++ b/clap_mangen/src/lib.rs @@ -244,8 +244,38 @@ impl Man { } fn _render_options_section(&self, roff: &mut Roff) { - roff.control("SH", ["OPTIONS"]); - render::options(roff, &self.cmd); + let help_headings = self + .cmd + .get_arguments() + .filter_map(|arg| arg.get_help_heading()) + .fold(vec![], |mut acc, header| { + if !acc.contains(&header) { + acc.push(header); + } + + acc + }); + + let (args, mut args_with_heading) = + self.cmd + .get_arguments() + .filter(|a| !a.is_hide_set()) + .partition::, _>(|a| a.get_help_heading().is_none()); + + if !args.is_empty() { + roff.control("SH", ["OPTIONS"]); + render::options(roff, &args); + } + + for heading in help_headings { + let args; + (args, args_with_heading) = args_with_heading + .into_iter() + .partition(|&a| a.get_help_heading() == Some(heading)); + + roff.control("SH", [heading.to_uppercase().as_str()]); + render::options(roff, &args); + } } /// Render the SUBCOMMANDS section into the writer. diff --git a/clap_mangen/src/render.rs b/clap_mangen/src/render.rs index 81d4e8c6b1c..c48d13a4812 100644 --- a/clap_mangen/src/render.rs +++ b/clap_mangen/src/render.rs @@ -88,9 +88,7 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) { roff.text(line); } -pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) { - let items: Vec<_> = cmd.get_arguments().filter(|i| !i.is_hide_set()).collect(); - +pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) { for opt in items.iter().filter(|a| !a.is_positional()) { let mut header = match (opt.get_short(), opt.get_long()) { (Some(short), Some(long)) => { diff --git a/clap_mangen/tests/snapshots/help_headings.bash.roff b/clap_mangen/tests/snapshots/help_headings.bash.roff index 250d8b3aca1..e3400e2501b 100644 --- a/clap_mangen/tests/snapshots/help_headings.bash.roff +++ b/clap_mangen/tests/snapshots/help_headings.bash.roff @@ -10,12 +10,14 @@ my\-app .TP \fB\-r\fR, \fB\-\-recursive\fR -.TP -\fB\-f\fR, \fB\-\-force\fR - .TP \fB\-h\fR, \fB\-\-help\fR Print help +.SH "CONFLICT OPTIONS" +.TP +\fB\-f\fR, \fB\-\-force\fR + +.SH "GLOBAL OPTIONS" .TP [\fIcolor\fR]