Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mangen): Support flatten_help #5769

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clap_mangen/src/lib.rs
aparcar marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Differences between flattened --help and the flattened man:

  • --help prints the description at the very beginning while Man uses a
    section called DESCRIPTION below the USAGE.
  • --help prints placeholder [OPTIONS] while Man prints all options
  • --help prints positional options (aka arguments) in its own section
    called arguments while Man prints them at the end of OPTIONS.
  • --help prints subcommands as their own section after the options
    section while Man uses an extra section called SUBCOMMANDS
  • --help prints after_long_help at the very end while Man uses the
    section EXTRA which comes before the sections VERSION and AUTHORS

Most of these differences sound unrelated to flattening

Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ impl Man {
fn _render_subcommands_section(&self, roff: &mut Roff) {
let heading = subcommand_heading(&self.cmd);
roff.control("SH", [heading]);
render::subcommands(roff, &self.cmd, &self.section);
if self.cmd.is_flatten_help_set() {
render::flat_subcommands(roff, &self.cmd);
} else {
render::subcommands(roff, &self.cmd, &self.section);
}
}

/// Render the EXTRA section into the writer.
Expand Down
74 changes: 60 additions & 14 deletions clap_mangen/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,46 @@ pub(crate) fn description(roff: &mut Roff, cmd: &clap::Command) {
}

pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
let name = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name());
let mut line = vec![bold(name), roman(" ")];
let flatten = cmd.is_flatten_help_set();
let mut first = true;
if !cmd.is_subcommand_required_set() || cmd.is_args_conflicts_with_subcommands_set() {
let mut line = usage(cmd, cmd.get_bin_name().unwrap_or_else(|| cmd.get_name()));
if cmd.has_subcommands() && !flatten {
let (lhs, rhs) = subcommand_markers(cmd);
line.push(roman(lhs));
line.push(italic(
cmd.get_subcommand_value_name()
.unwrap_or_else(|| subcommand_heading(cmd))
.to_lowercase(),
));
line.push(roman(rhs));
}
roff.text(line);
first = false;
}
if flatten {
let mut ord_v = Vec::new();
for subcommand in cmd.get_subcommands() {
ord_v.push((
subcommand.get_display_order(),
subcommand.get_bin_name().unwrap_or_else(|| cmd.get_name()),
subcommand,
));
}
ord_v.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)));
for (_, name, cmd) in ord_v {
if !first {
roff.control("br", []);
} else {
first = false;
}
roff.text(usage(cmd, name));
}
}
}

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

if cmd.has_subcommands() {
let (lhs, rhs) = subcommand_markers(cmd);
line.push(roman(lhs));
line.push(italic(
cmd.get_subcommand_value_name()
.unwrap_or_else(|| subcommand_heading(cmd))
.to_lowercase(),
));
line.push(roman(rhs));
}

roff.text(line);
line
}

pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
Expand Down Expand Up @@ -220,6 +246,26 @@ pub(crate) fn subcommands(roff: &mut Roff, cmd: &clap::Command, section: &str) {
}
}

pub(crate) fn flat_subcommands(roff: &mut Roff, cmd: &clap::Command) {
for sub in cmd.get_subcommands().filter(|s| !s.is_hide_set()) {
roff.control("TP", []);

let mut line = usage(sub, sub.get_name());

if let Some(about) = sub.get_long_about().or_else(|| sub.get_about()) {
line.push(roman("\n"));
line.push(roman(about.to_string()));
}

if let Some(after_help) = sub.get_after_help() {
line.push(roman("\n"));
line.push(roman(after_help.to_string()));
}

roff.text(line);
}
}

pub(crate) fn version(cmd: &clap::Command) -> String {
format!(
"v{}",
Expand Down
27 changes: 27 additions & 0 deletions clap_mangen/tests/snapshots/flatten_arg_required.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH parent 1 "parent "
.SH NAME
parent \- parent command
.SH SYNOPSIS
\fBparent\fR <\fB\-\-parent\fR> [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent test\fR <\fB\-\-child\fR> [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent help\fR
.SH DESCRIPTION
parent command
.SH OPTIONS
.TP
\fB\-\-parent\fR

.TP
\fB\-h\fR, \fB\-\-help\fR
Print help
.SH SUBCOMMANDS
.TP
\fBtest\fR <\fB\-\-child\fR> [\fB\-h\fR|\fB\-\-help\fR]
test command
.TP
\fBhelp\fR
Print this message or the help of the given subcommand(s)
27 changes: 27 additions & 0 deletions clap_mangen/tests/snapshots/flatten_basic.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH parent 1 "parent "
.SH NAME
parent \- parent command
.SH SYNOPSIS
\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent test\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent help\fR
.SH DESCRIPTION
parent command
.SH OPTIONS
.TP
\fB\-\-parent\fR

.TP
\fB\-h\fR, \fB\-\-help\fR
Print help
.SH SUBCOMMANDS
.TP
\fBtest\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR]
test command
.TP
\fBhelp\fR
Print this message or the help of the given subcommand(s)
30 changes: 30 additions & 0 deletions clap_mangen/tests/snapshots/flatten_help.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH my-app 1 "my-app "
.SH NAME
my\-app
.SH SYNOPSIS
\fBmy\-app\fR [\fB\-c \fR] [\fB\-v \fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBmy\-app test\fR [\fB\-d \fR]... [\fB\-c \fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBmy\-app help\fR
.SH DESCRIPTION
.SH OPTIONS
.TP
\fB\-c\fR

.TP
\fB\-v\fR

.TP
\fB\-h\fR, \fB\-\-help\fR
Print help
.SH SUBCOMMANDS
.TP
\fBtest\fR [\fB\-d \fR]... [\fB\-c \fR] [\fB\-h\fR|\fB\-\-help\fR]
Subcommand
with a second line
.TP
\fBhelp\fR
Print this message or the help of the given subcommand(s)
27 changes: 27 additions & 0 deletions clap_mangen/tests/snapshots/flatten_help_cmd.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH parent 1 "parent "
.SH NAME
parent \- parent command
.SH SYNOPSIS
\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent test\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent help\fR
.SH DESCRIPTION
parent command
.SH OPTIONS
.TP
\fB\-\-parent\fR
bar
.TP
\fB\-h\fR, \fB\-\-help\fR
Print help (see a summary with \*(Aq\-h\*(Aq)
.SH SUBCOMMANDS
.TP
\fBtest\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR]
long some
.TP
\fBhelp\fR
Print this message or the help of the given subcommand(s)
28 changes: 28 additions & 0 deletions clap_mangen/tests/snapshots/flatten_help_subcommand_required.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH my-app 1 "my-app "
.SH NAME
my\-app
.SH SYNOPSIS
\fBmy\-app test\fR [\fB\-d \fR]... [\fB\-c \fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBmy\-app help\fR
.SH DESCRIPTION
.SH OPTIONS
.TP
\fB\-c\fR

.TP
\fB\-v\fR

.TP
\fB\-h\fR, \fB\-\-help\fR
Print help
.SH SUBCOMMANDS
.TP
\fBtest\fR [\fB\-d \fR]... [\fB\-c \fR] [\fB\-h\fR|\fB\-\-help\fR]
Subcommand
with a second line
.TP
\fBhelp\fR
Print this message or the help of the given subcommand(s)
34 changes: 34 additions & 0 deletions clap_mangen/tests/snapshots/flatten_hidden_command.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH parent 1 "parent "
.SH NAME
parent \- parent command
.SH SYNOPSIS
\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent child1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent child2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent child3\fR [\fB\-\-child3\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent help\fR
.SH DESCRIPTION
parent command
.SH OPTIONS
.TP
\fB\-\-parent\fR

.TP
\fB\-h\fR, \fB\-\-help\fR
Print help
.SH SUBCOMMANDS
.TP
\fBchild1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR]
child1 command
.TP
\fBchild2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR]
child2 command
.TP
\fBhelp\fR
Print this message or the help of the given subcommand(s)
37 changes: 37 additions & 0 deletions clap_mangen/tests/snapshots/flatten_not_recursive.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH parent 1 "parent "
.SH NAME
parent \- parent command
.SH SYNOPSIS
\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent child1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent child2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent child3\fR [\fB\-\-child3\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent help\fR
.SH DESCRIPTION
parent command
.SH OPTIONS
.TP
\fB\-\-parent\fR

.TP
\fB\-h\fR, \fB\-\-help\fR
Print help
.SH SUBCOMMANDS
.TP
\fBchild1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR]
child1 command
.TP
\fBchild2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR]
child2 command
.TP
\fBchild3\fR [\fB\-\-child3\fR] [\fB\-h\fR|\fB\-\-help\fR]
child3 command
.TP
\fBhelp\fR
Print this message or the help of the given subcommand(s)
34 changes: 34 additions & 0 deletions clap_mangen/tests/snapshots/flatten_recursive.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH parent 1 "parent "
.SH NAME
parent \- parent command
.SH SYNOPSIS
\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent child1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent child2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent child3\fR [\fB\-\-child3\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent help\fR
.SH DESCRIPTION
parent command
.SH OPTIONS
.TP
\fB\-\-parent\fR

.TP
\fB\-h\fR, \fB\-\-help\fR
Print help
.SH SUBCOMMANDS
.TP
\fBchild1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR]
child1 command
.TP
\fBchild2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR]
child2 command
.TP
\fBhelp\fR
Print this message or the help of the given subcommand(s)
24 changes: 24 additions & 0 deletions clap_mangen/tests/snapshots/flatten_single_hidden_command.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH parent 1 "parent "
.SH NAME
parent \- parent command
.SH SYNOPSIS
\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent child1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR]
.br
\fBparent help\fR
.SH DESCRIPTION
parent command
.SH OPTIONS
.TP
\fB\-\-parent\fR

.TP
\fB\-h\fR, \fB\-\-help\fR
Print help
.SH SUBCOMMANDS
.TP
\fBhelp\fR
Print this message or the help of the given subcommand(s)
Loading