Skip to content

Commit

Permalink
feat(mangen): Separate options based on help_heading
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAdk committed Dec 24, 2024
1 parent 5716d20 commit ba776e9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
34 changes: 32 additions & 2 deletions clap_mangen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>, _>(|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.
Expand Down
4 changes: 1 addition & 3 deletions clap_mangen/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand Down
8 changes: 5 additions & 3 deletions clap_mangen/tests/snapshots/help_headings.bash.roff
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit ba776e9

Please sign in to comment.