Skip to content

Commit

Permalink
fix(help): Recurse help flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 9, 2023
1 parent 4bef91c commit 9c0f7a7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
16 changes: 10 additions & 6 deletions clap_builder/src/output/help_template.rs
Expand Up @@ -479,7 +479,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
if flatten {
let mut cmd = self.cmd.clone();
cmd.build();
self.write_flat_subcommands(&cmd, first);
self.write_flat_subcommands(&cmd, &mut first);
}
}

Expand Down Expand Up @@ -881,10 +881,11 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
/// Subcommand handling
impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
/// Writes help for subcommands of a Parser Object to the wrapped stream.
fn write_flat_subcommands(&mut self, cmd: &Command, mut first: bool) {
fn write_flat_subcommands(&mut self, cmd: &Command, first: &mut bool) {
debug!(
"HelpTemplate::write_flat_subcommands, cmd={}, first={first}",
cmd.get_name()
"HelpTemplate::write_flat_subcommands, cmd={}, first={}",
cmd.get_name(),
*first
);
use std::fmt::Write as _;
let header = &self.styles.get_header();
Expand All @@ -902,10 +903,10 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
}
ord_v.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)));
for (_, _, subcommand) in ord_v {
if !first {
if !*first {
self.writer.push_str("\n\n");
}
first = false;
*first = false;

let heading = subcommand.get_usage_name_fallback();
let about = cmd
Expand Down Expand Up @@ -937,6 +938,9 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
.filter(|arg| should_show_arg(self.use_long, arg) && !arg.is_global_set())
.collect::<Vec<_>>();
sub_help.write_args(&args, heading, option_sort_key);
if subcommand.is_flatten_help_set() {
sub_help.write_flat_subcommands(subcommand, first);
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/builder/help.rs
Expand Up @@ -3285,6 +3285,44 @@ parent child1:
--child1 <child>
-h, --help Print help
parent child1 grandchild1:
some 1
--grandchild1 <grandchild>
-h, --help Print help
parent child1 grandchild1 greatgrandchild1:
some 1
--greatgrandchild1 <greatgrandchild>
-h, --help Print help
parent child1 grandchild1 greatgrandchild2:
some 1
--greatgrandchild2 <greatgrandchild>
-h, --help Print help
parent child1 grandchild1 greatgrandchild3:
some 1
--greatgrandchild3 <greatgrandchild>
-h, --help Print help
parent child1 grandchild1 help:
some 1
parent child1 grandchild2:
some 1
--grandchild2 <grandchild>
-h, --help Print help
parent child1 grandchild3:
some 1
--grandchild3 <grandchild>
-h, --help Print help
parent child1 help:
some 1
parent child2:
--child2 <child>
-h, --help Print help
Expand Down

0 comments on commit 9c0f7a7

Please sign in to comment.