fix(help): Recurse help flattening

This commit is contained in:
Ed Page 2023-11-09 14:50:22 -06:00
parent 4bef91ca3c
commit 9c0f7a7253
2 changed files with 48 additions and 6 deletions

View file

@ -479,7 +479,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
if flatten { if flatten {
let mut cmd = self.cmd.clone(); let mut cmd = self.cmd.clone();
cmd.build(); cmd.build();
self.write_flat_subcommands(&cmd, first); self.write_flat_subcommands(&cmd, &mut first);
} }
} }
@ -881,10 +881,11 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
/// Subcommand handling /// Subcommand handling
impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> { impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
/// Writes help for subcommands of a Parser Object to the wrapped stream. /// 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!( debug!(
"HelpTemplate::write_flat_subcommands, cmd={}, first={first}", "HelpTemplate::write_flat_subcommands, cmd={}, first={}",
cmd.get_name() cmd.get_name(),
*first
); );
use std::fmt::Write as _; use std::fmt::Write as _;
let header = &self.styles.get_header(); let header = &self.styles.get_header();
@ -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))); ord_v.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)));
for (_, _, subcommand) in ord_v { for (_, _, subcommand) in ord_v {
if !first { if !*first {
self.writer.push_str("\n\n"); self.writer.push_str("\n\n");
} }
first = false; *first = false;
let heading = subcommand.get_usage_name_fallback(); let heading = subcommand.get_usage_name_fallback();
let about = cmd let about = cmd
@ -937,6 +938,9 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
.filter(|arg| should_show_arg(self.use_long, arg) && !arg.is_global_set()) .filter(|arg| should_show_arg(self.use_long, arg) && !arg.is_global_set())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
sub_help.write_args(&args, heading, option_sort_key); sub_help.write_args(&args, heading, option_sort_key);
if subcommand.is_flatten_help_set() {
sub_help.write_flat_subcommands(subcommand, first);
}
} }
} }

View file

@ -3285,6 +3285,44 @@ parent child1:
--child1 <child> --child1 <child>
-h, --help Print help -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: parent child2:
--child2 <child> --child2 <child>
-h, --help Print help -h, --help Print help