From e00abc69050cb2803940f76511d886e3e02062f8 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 25 Aug 2022 13:01:24 -0500 Subject: [PATCH] refactor(help): Consolidate arg suffix rendering This ended up favoring the help implementation --- clap_complete/tests/snapshots/basic.bash | 2 +- .../tests/snapshots/feature_sample.bash | 2 +- clap_complete/tests/snapshots/quoting.bash | 2 +- .../tests/snapshots/special_commands.bash | 2 +- .../tests/snapshots/sub_subcommands.bash | 4 +- src/builder/arg.rs | 74 +++++++++---------- src/builder/mod.rs | 1 - src/output/help.rs | 38 +--------- 8 files changed, 45 insertions(+), 80 deletions(-) diff --git a/clap_complete/tests/snapshots/basic.bash b/clap_complete/tests/snapshots/basic.bash index eabc9b50..d1c4727c 100644 --- a/clap_complete/tests/snapshots/basic.bash +++ b/clap_complete/tests/snapshots/basic.bash @@ -39,7 +39,7 @@ _my-app() { return 0 ;; my__app__help) - opts="-c [...]" + opts="-c ..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/clap_complete/tests/snapshots/feature_sample.bash b/clap_complete/tests/snapshots/feature_sample.bash index caebfbc5..925e21aa 100644 --- a/clap_complete/tests/snapshots/feature_sample.bash +++ b/clap_complete/tests/snapshots/feature_sample.bash @@ -39,7 +39,7 @@ _my-app() { return 0 ;; my__app__help) - opts="[...]" + opts="..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/clap_complete/tests/snapshots/quoting.bash b/clap_complete/tests/snapshots/quoting.bash index 80d0c14e..a69dd972 100644 --- a/clap_complete/tests/snapshots/quoting.bash +++ b/clap_complete/tests/snapshots/quoting.bash @@ -138,7 +138,7 @@ _my-app() { return 0 ;; my__app__help) - opts="[...]" + opts="..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/clap_complete/tests/snapshots/special_commands.bash b/clap_complete/tests/snapshots/special_commands.bash index ef81e2b6..13b0726c 100644 --- a/clap_complete/tests/snapshots/special_commands.bash +++ b/clap_complete/tests/snapshots/special_commands.bash @@ -48,7 +48,7 @@ _my-app() { return 0 ;; my__app__help) - opts="[...]" + opts="..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/clap_complete/tests/snapshots/sub_subcommands.bash b/clap_complete/tests/snapshots/sub_subcommands.bash index be860b1d..23108c89 100644 --- a/clap_complete/tests/snapshots/sub_subcommands.bash +++ b/clap_complete/tests/snapshots/sub_subcommands.bash @@ -45,7 +45,7 @@ _my-app() { return 0 ;; my__app__help) - opts="[...]" + opts="..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -73,7 +73,7 @@ _my-app() { return 0 ;; my__app__some_cmd__help) - opts="[...]" + opts="..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/src/builder/arg.rs b/src/builder/arg.rs index 23ffcbf6..f8ac1139 100644 --- a/src/builder/arg.rs +++ b/src/builder/arg.rs @@ -3975,6 +3975,40 @@ impl Arg { } } + pub(crate) fn stylize_arg_suffix(&self) -> StyledStr { + let mut styled = StyledStr::new(); + + let mut need_closing_bracket = false; + if self.is_takes_value_set() && !self.is_positional() { + let is_optional_val = self.get_min_vals() == 0; + let sep = if self.is_require_equals_set() { + if is_optional_val { + need_closing_bracket = true; + "[=" + } else { + "=" + } + } else if is_optional_val { + need_closing_bracket = true; + " [" + } else { + " " + }; + styled.good(sep); + } + if self.is_takes_value_set() || self.is_positional() { + let arg_val = render_arg_val(self); + styled.good(arg_val); + } else if matches!(*self.get_action(), ArgAction::Count) { + styled.good("..."); + } + if need_closing_bracket { + styled.none("]"); + } + + styled + } + /// Either multiple values or occurrences pub(crate) fn is_multiple(&self) -> bool { self.is_multiple_values_set() || matches!(*self.get_action(), ArgAction::Append) @@ -4019,41 +4053,7 @@ impl Display for Arg { } else if let Some(s) = self.get_short() { write!(f, "-{}", s)?; } - let mut need_closing_bracket = false; - let is_optional_val = self.get_min_vals() == 0; - if self.is_positional() { - if is_optional_val { - let sep = "["; - need_closing_bracket = true; - f.write_str(sep)?; - } - } else if self.is_takes_value_set() { - let sep = if self.is_require_equals_set() { - if is_optional_val { - need_closing_bracket = true; - "[=" - } else { - "=" - } - } else if is_optional_val { - need_closing_bracket = true; - " [" - } else { - " " - }; - f.write_str(sep)?; - } - if self.is_takes_value_set() || self.is_positional() { - let arg_val = render_arg_val(self); - f.write_str(&arg_val)?; - } else if matches!(*self.get_action(), ArgAction::Count) { - f.write_str("...")?; - } - if need_closing_bracket { - f.write_str("]")?; - } - - Ok(()) + self.stylize_arg_suffix().fmt(f) } } @@ -4382,7 +4382,7 @@ mod test { let mut p = Arg::new("pos").index(1).num_args(0..); p._build(); - assert_eq!(p.to_string(), "[...]"); + assert_eq!(p.to_string(), "..."); } #[test] @@ -4401,7 +4401,7 @@ mod test { .action(ArgAction::Set); p._build(); - assert_eq!(p.to_string(), "[]"); + assert_eq!(p.to_string(), ""); } #[test] diff --git a/src/builder/mod.rs b/src/builder/mod.rs index a450cbee..098ad576 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -57,5 +57,4 @@ pub use value_parser::_AnonymousValueParser; #[allow(unused_imports)] pub(crate) use self::str::Inner as StrInner; pub(crate) use action::CountType; -pub(crate) use arg::render_arg_val; pub(crate) use arg_settings::{ArgFlags, ArgSettings}; diff --git a/src/output/help.rs b/src/output/help.rs index c2e43a7c..c742b6b1 100644 --- a/src/output/help.rs +++ b/src/output/help.rs @@ -8,12 +8,11 @@ use std::usize; use crate::builder::PossibleValue; use crate::builder::Str; use crate::builder::StyledStr; -use crate::builder::{render_arg_val, Arg, Command}; +use crate::builder::{Arg, Command}; use crate::output::display_width; use crate::output::wrap; use crate::output::Usage; use crate::util::FlatSet; -use crate::ArgAction; /// `clap` Help Writer. /// @@ -184,7 +183,7 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> { self.short(arg); self.long(arg); - self.val(arg); + self.writer.extend(arg.stylize_arg_suffix().into_iter()); self.align_to_about(arg, next_line_help, longest); let about = if self.use_long { @@ -224,39 +223,6 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> { } } - /// Writes argument's possible values to the wrapped stream. - fn val(&mut self, arg: &Arg) { - debug!("Help::val: arg={}", arg.get_id()); - let mut need_closing_bracket = false; - if arg.is_takes_value_set() && !arg.is_positional() { - let is_optional_val = arg.get_min_vals() == 0; - let sep = if arg.is_require_equals_set() { - if is_optional_val { - need_closing_bracket = true; - "[=" - } else { - "=" - } - } else if is_optional_val { - need_closing_bracket = true; - " [" - } else { - " " - }; - self.none(sep); - } - - if arg.is_takes_value_set() || arg.is_positional() { - let arg_val = render_arg_val(arg); - self.good(arg_val); - } else if matches!(*arg.get_action(), ArgAction::Count) { - self.good("..."); - } - if need_closing_bracket { - self.none("]"); - } - } - /// Write alignment padding between arg's switches/values and its about message. fn align_to_about(&mut self, arg: &Arg, next_line_help: bool, longest: usize) { debug!(