From 17d975743a318d3f4488cc2247d593e7e4ca2421 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 15 Jun 2022 09:25:12 -0500 Subject: [PATCH 1/3] fix(help): Improve debug output --- src/output/help.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/output/help.rs b/src/output/help.rs index 152955d2..9834897b 100644 --- a/src/output/help.rs +++ b/src/output/help.rs @@ -189,8 +189,8 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> { } /// Sorts arguments by length and display order and write their help to the wrapped stream. - fn write_args(&mut self, args: &[&Arg<'help>]) -> io::Result<()> { - debug!("Help::write_args"); + fn write_args(&mut self, args: &[&Arg<'help>], _category: &str) -> io::Result<()> { + debug!("Help::write_args {}", _category); // The shortest an arg can legally be is 2 (i.e. '-x') let mut longest = 2; let mut ord_v = Vec::new(); @@ -203,9 +203,12 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> { should_show_arg(self.use_long, *arg) }) { if arg.longest_filter() { - debug!("Help::write_args: Current Longest...{}", longest); longest = longest.max(display_width(arg.to_string().as_str())); - debug!("Help::write_args: New Longest...{}", longest); + debug!( + "Help::write_args: arg={:?} longest={}", + arg.get_id(), + longest + ); } // Formatting key like this to ensure that: @@ -854,7 +857,7 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> { self.none("\n\n")?; } self.warning("OPTIONS:\n")?; - self.write_args(&non_pos)?; + self.write_args(&non_pos, "OPTIONS")?; first = false; } if !custom_headings.is_empty() { @@ -876,7 +879,7 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> { self.none("\n\n")?; } self.warning(format!("{}:\n", heading))?; - self.write_args(&args)?; + self.write_args(&args, heading)?; first = false } } @@ -1079,10 +1082,10 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> { "options" => { // Include even those with a heading as we don't have a good way of // handling help_heading in the template. - self.write_args(&self.cmd.get_non_positionals().collect::>())?; + self.write_args(&self.cmd.get_non_positionals().collect::>(), "options")?; } "positionals" => { - self.write_args(&self.cmd.get_positionals().collect::>())?; + self.write_args(&self.cmd.get_positionals().collect::>(), "positionals")?; } "subcommands" => { self.write_subcommands(self.cmd)?; From c443aa0b5d55476e54731949885b0b5260ab07ac Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 15 Jun 2022 09:31:19 -0500 Subject: [PATCH 2/3] refactor(help): Collapse useless function --- src/output/help.rs | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/output/help.rs b/src/output/help.rs index 9834897b..b4bf8feb 100644 --- a/src/output/help.rs +++ b/src/output/help.rs @@ -253,7 +253,18 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> { ) -> io::Result<()> { let spec_vals = &self.spec_vals(arg); - self.write_arg_inner(arg, spec_vals, next_line_help, longest)?; + self.short(arg)?; + self.long(arg)?; + self.val(arg)?; + self.align_to_about(arg, next_line_help, longest)?; + + let about = if self.use_long { + arg.long_help.or(arg.help).unwrap_or("") + } else { + arg.help.or(arg.long_help).unwrap_or("") + }; + + self.help(Some(arg), about, spec_vals, next_line_help, longest)?; if !last_arg { self.none("\n")?; @@ -532,29 +543,6 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> { Ok(()) } - /// Writes help for an argument to the wrapped stream. - fn write_arg_inner( - &mut self, - arg: &Arg<'help>, - spec_vals: &str, - next_line_help: bool, - longest: usize, - ) -> io::Result<()> { - self.short(arg)?; - self.long(arg)?; - self.val(arg)?; - self.align_to_about(arg, next_line_help, longest)?; - - let about = if self.use_long { - arg.long_help.or(arg.help).unwrap_or("") - } else { - arg.help.or(arg.long_help).unwrap_or("") - }; - - self.help(Some(arg), about, spec_vals, next_line_help, longest)?; - Ok(()) - } - /// Will use next line help on writing args. fn will_args_wrap(&self, args: &[&Arg<'help>], longest: usize) -> bool { args.iter() From eba753f16288486e21eeb177a3e70fcef588afeb Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 15 Jun 2022 10:03:52 -0500 Subject: [PATCH 3/3] fix(help): Further debug clean up --- src/output/help.rs | 63 +++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/src/output/help.rs b/src/output/help.rs index b4bf8feb..eb53bfa5 100644 --- a/src/output/help.rs +++ b/src/output/help.rs @@ -174,7 +174,12 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> { .filter(|arg| should_show_arg(self.use_long, *arg)) { if arg.longest_filter() { - longest = longest.max(display_width(arg.to_string().as_str())); + longest = longest.max(display_width(&arg.to_string())); + debug!( + "Help::write_args_unsorted: arg={:?} longest={}", + arg.get_id(), + longest + ); } arg_v.push(arg) } @@ -203,7 +208,7 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> { should_show_arg(self.use_long, *arg) }) { if arg.longest_filter() { - longest = longest.max(display_width(arg.to_string().as_str())); + longest = longest.max(display_width(&arg.to_string())); debug!( "Help::write_args: arg={:?} longest={}", arg.get_id(), @@ -344,35 +349,41 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> { next_line_help: bool, longest: usize, ) -> io::Result<()> { - debug!("Help::align_to_about: arg={}", arg.name); - debug!("Help::align_to_about: Has switch..."); - if self.use_long { + debug!( + "Help::align_to_about: arg={}, next_line_help={}, longest={}", + arg.name, next_line_help, longest + ); + if self.use_long || next_line_help { // long help prints messages on the next line so it doesn't need to align text debug!("Help::align_to_about: printing long help so skip alignment"); } else if !arg.is_positional() { - debug!("Yes"); - debug!("Help::align_to_about: nlh...{:?}", next_line_help); - if !next_line_help { - let self_len = display_width(arg.to_string().as_str()); - // subtract ourself - let mut spcs = longest - self_len; - // Since we're writing spaces from the tab point we first need to know if we - // had a long and short, or just short - if arg.long.is_some() { - // Only account 4 after the val - spcs += 4; - } else { - // Only account for ', --' + 4 after the val - spcs += 8; - } + let self_len = display_width(&arg.to_string()); + // Since we're writing spaces from the tab point we first need to know if we + // had a long and short, or just short + let padding = if arg.long.is_some() { + // Only account 4 after the val + 4 + } else { + // Only account for ', --' + 4 after the val + 8 + }; + let spcs = longest + padding - self_len; + debug!( + "Help::align_to_about: positional=false arg_len={}, spaces={}", + self_len, spcs + ); - self.spaces(spcs)?; - } - } else if !next_line_help { - debug!("No, and not next_line"); - self.spaces(longest + 4 - display_width(&arg.to_string()))?; + self.spaces(spcs)?; } else { - debug!("No"); + let self_len = display_width(&arg.to_string()); + let padding = 4; + let spcs = longest + padding - self_len; + debug!( + "Help::align_to_about: positional=true arg_len={}, spaces={}", + self_len, spcs + ); + + self.spaces(spcs)?; } Ok(()) }