mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
refactor(help): Consolidate arg suffix rendering
This ended up favoring the help implementation
This commit is contained in:
parent
8607695ed9
commit
e00abc6905
8 changed files with 45 additions and 80 deletions
|
@ -39,7 +39,7 @@ _my-app() {
|
|||
return 0
|
||||
;;
|
||||
my__app__help)
|
||||
opts="-c [<SUBCOMMAND>...]"
|
||||
opts="-c <SUBCOMMAND>..."
|
||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||
return 0
|
||||
|
|
|
@ -39,7 +39,7 @@ _my-app() {
|
|||
return 0
|
||||
;;
|
||||
my__app__help)
|
||||
opts="[<SUBCOMMAND>...]"
|
||||
opts="<SUBCOMMAND>..."
|
||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||
return 0
|
||||
|
|
|
@ -138,7 +138,7 @@ _my-app() {
|
|||
return 0
|
||||
;;
|
||||
my__app__help)
|
||||
opts="[<SUBCOMMAND>...]"
|
||||
opts="<SUBCOMMAND>..."
|
||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||
return 0
|
||||
|
|
|
@ -48,7 +48,7 @@ _my-app() {
|
|||
return 0
|
||||
;;
|
||||
my__app__help)
|
||||
opts="[<SUBCOMMAND>...]"
|
||||
opts="<SUBCOMMAND>..."
|
||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||
return 0
|
||||
|
|
|
@ -45,7 +45,7 @@ _my-app() {
|
|||
return 0
|
||||
;;
|
||||
my__app__help)
|
||||
opts="[<SUBCOMMAND>...]"
|
||||
opts="<SUBCOMMAND>..."
|
||||
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="[<SUBCOMMAND>...]"
|
||||
opts="<SUBCOMMAND>..."
|
||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||
return 0
|
||||
|
|
|
@ -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(), "[<pos>...]");
|
||||
assert_eq!(p.to_string(), "<pos>...");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -4401,7 +4401,7 @@ mod test {
|
|||
.action(ArgAction::Set);
|
||||
p._build();
|
||||
|
||||
assert_eq!(p.to_string(), "[<pos>]");
|
||||
assert_eq!(p.to_string(), "<pos>");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -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};
|
||||
|
|
|
@ -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!(
|
||||
|
|
Loading…
Reference in a new issue