fix: Prefer more usable &str for reflection

The downside is we can't skip allocations with
- `ValueEnum` default values
- Vaid subcommands

Otherwise, this is a big ergonomic win.
This commit is contained in:
Ed Page 2022-08-22 13:48:39 -05:00
parent 85f541d789
commit 1bbf07e574
32 changed files with 199 additions and 286 deletions

View file

@ -374,9 +374,7 @@ complete OPTIONS -F _clap_complete_NAME EXECUTABLES
if let Some((flag, value)) = arg.to_long() {
if let Ok(flag) = flag {
if let Some(value) = value {
if let Some(arg) = cmd
.get_arguments()
.find(|a| a.get_long().map(|s| s.as_str()) == Some(flag))
if let Some(arg) = cmd.get_arguments().find(|a| a.get_long() == Some(flag))
{
completions.extend(
complete_arg_value(value.to_str().ok_or(value), arg, current_dir)

View file

@ -216,17 +216,14 @@ mod tests {
let actual_flags = flags(&cmd);
assert_eq!(actual_flags.len(), 2);
assert_eq!(actual_flags[0].get_long().map(|s| s.as_str()), Some("help"));
assert_eq!(
actual_flags[1].get_long().map(|s| s.as_str()),
Some("version")
);
assert_eq!(actual_flags[0].get_long(), Some("help"));
assert_eq!(actual_flags[1].get_long(), Some("version"));
let sc_flags = flags(find_subcommand_with_path(&cmd, vec!["test"]));
assert_eq!(sc_flags.len(), 2);
assert_eq!(sc_flags[0].get_long().map(|s| s.as_str()), Some("file"));
assert_eq!(sc_flags[1].get_long().map(|s| s.as_str()), Some("help"));
assert_eq!(sc_flags[0].get_long(), Some("file"));
assert_eq!(sc_flags[1].get_long(), Some("help"));
}
#[test]
@ -235,13 +232,13 @@ mod tests {
let actual_flags = flags(&cmd);
assert_eq!(actual_flags.len(), 1);
assert_eq!(actual_flags[0].get_long().map(|s| s.as_str()), Some("help"));
assert_eq!(actual_flags[0].get_long(), Some("help"));
let sc_flags = flags(find_subcommand_with_path(&cmd, vec!["test"]));
assert_eq!(sc_flags.len(), 2);
assert_eq!(sc_flags[0].get_long().map(|s| s.as_str()), Some("file"));
assert_eq!(sc_flags[1].get_long().map(|s| s.as_str()), Some("help"));
assert_eq!(sc_flags[0].get_long(), Some("file"));
assert_eq!(sc_flags[1].get_long(), Some("help"));
}
#[test]

View file

@ -179,7 +179,7 @@ fn vals_for(o: &Arg) -> String {
"$(compgen -W \"{}\" -- \"${{cur}}\")",
vals.iter()
.filter(|pv| !pv.is_hide_set())
.map(|n| n.get_name().as_str())
.map(|n| n.get_name())
.collect::<Vec<_>>()
.join(" ")
)

View file

@ -84,7 +84,7 @@ fn generate_inner<'help>(
for option in p.get_opts() {
if let Some(shorts) = option.get_short_and_visible_aliases() {
let tooltip = get_tooltip(option.get_help().map(|s| s.as_str()), shorts[0]);
let tooltip = get_tooltip(option.get_help(), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(format!("-{} '{}'", short, tooltip).as_str());
@ -92,7 +92,7 @@ fn generate_inner<'help>(
}
if let Some(longs) = option.get_long_and_visible_aliases() {
let tooltip = get_tooltip(option.get_help().map(|s| s.as_str()), longs[0]);
let tooltip = get_tooltip(option.get_help(), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(format!("--{} '{}'", long, tooltip).as_str());
@ -102,7 +102,7 @@ fn generate_inner<'help>(
for flag in utils::flags(p) {
if let Some(shorts) = flag.get_short_and_visible_aliases() {
let tooltip = get_tooltip(flag.get_help().map(|s| s.as_str()), shorts[0]);
let tooltip = get_tooltip(flag.get_help(), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(format!("-{} '{}'", short, tooltip).as_str());
@ -110,7 +110,7 @@ fn generate_inner<'help>(
}
if let Some(longs) = flag.get_long_and_visible_aliases() {
let tooltip = get_tooltip(flag.get_help().map(|s| s.as_str()), longs[0]);
let tooltip = get_tooltip(flag.get_help(), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(format!("--{} '{}'", long, tooltip).as_str());
@ -120,7 +120,7 @@ fn generate_inner<'help>(
for subcommand in p.get_subcommands() {
let data = &subcommand.get_name();
let tooltip = get_tooltip(subcommand.get_about().map(|s| s.as_str()), data);
let tooltip = get_tooltip(subcommand.get_about(), data);
completions.push_str(&preamble);
completions.push_str(format!("{} '{}'", data, tooltip).as_str());

View file

@ -164,8 +164,7 @@ fn value_completion(option: &Arg) -> String {
Some(format!(
"{}\t{}",
escape_string(value.get_name()).as_str(),
escape_string(value.get_help().map(|s| s.as_str()).unwrap_or_default())
.as_str()
escape_string(value.get_help().unwrap_or_default()).as_str()
))
})
.collect::<Vec<_>>()

View file

@ -89,7 +89,7 @@ fn generate_inner<'help>(
for option in p.get_opts() {
if let Some(shorts) = option.get_short_and_visible_aliases() {
let tooltip = get_tooltip(option.get_help().map(|s| s.as_str()), shorts[0]);
let tooltip = get_tooltip(option.get_help(), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(
@ -103,7 +103,7 @@ fn generate_inner<'help>(
}
if let Some(longs) = option.get_long_and_visible_aliases() {
let tooltip = get_tooltip(option.get_help().map(|s| s.as_str()), longs[0]);
let tooltip = get_tooltip(option.get_help(), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(
@ -119,7 +119,7 @@ fn generate_inner<'help>(
for flag in utils::flags(p) {
if let Some(shorts) = flag.get_short_and_visible_aliases() {
let tooltip = get_tooltip(flag.get_help().map(|s| s.as_str()), shorts[0]);
let tooltip = get_tooltip(flag.get_help(), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(
@ -133,7 +133,7 @@ fn generate_inner<'help>(
}
if let Some(longs) = flag.get_long_and_visible_aliases() {
let tooltip = get_tooltip(flag.get_help().map(|s| s.as_str()), longs[0]);
let tooltip = get_tooltip(flag.get_help(), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(
@ -149,7 +149,7 @@ fn generate_inner<'help>(
for subcommand in p.get_subcommands() {
let data = &subcommand.get_name();
let tooltip = get_tooltip(subcommand.get_about().map(|s| s.as_str()), data);
let tooltip = get_tooltip(subcommand.get_about(), data);
completions.push_str(&preamble);
completions.push_str(

View file

@ -283,7 +283,7 @@ esac",
fn parser_of<'cmd>(parent: &'cmd Command, bin_name: &str) -> Option<&'cmd Command> {
debug!("parser_of: p={}, bin_name={}", parent.get_name(), bin_name);
if bin_name == *parent.get_bin_name().unwrap_or_default() {
if bin_name == parent.get_bin_name().unwrap_or_default() {
return Some(parent);
}
@ -371,12 +371,8 @@ fn value_completion(arg: &Arg) -> Option<String> {
} else {
Some(format!(
r#"{name}\:"{tooltip}""#,
name = escape_value(value.get_name().as_str()),
tooltip = value
.get_help()
.map(|s| s.as_str())
.map(escape_help)
.unwrap_or_default()
name = escape_value(value.get_name()),
tooltip = value.get_help().map(escape_help).unwrap_or_default()
))
}
})
@ -389,7 +385,7 @@ fn value_completion(arg: &Arg) -> Option<String> {
values
.iter()
.filter(|pv| !pv.is_hide_set())
.map(|n| n.get_name().as_str())
.map(|n| n.get_name())
.collect::<Vec<_>>()
.join(" ")
))
@ -449,10 +445,7 @@ fn write_opts_of(p: &Command, p_global: Option<&Command>) -> String {
for o in p.get_opts() {
debug!("write_opts_of:iter: o={}", o.get_id());
let help = o
.get_help()
.map(|s| escape_help(s.as_str()))
.unwrap_or_default();
let help = o.get_help().map(escape_help).unwrap_or_default();
let conflicts = arg_conflicts(p, o, p_global);
let multiple = "*";
@ -548,10 +541,7 @@ fn write_flags_of(p: &Command, p_global: Option<&Command>) -> String {
for f in utils::flags(p) {
debug!("write_flags_of:iter: f={}", f.get_id());
let help = f
.get_help()
.map(|s| escape_help(s.as_str()))
.unwrap_or_default();
let help = f.get_help().map(escape_help).unwrap_or_default();
let conflicts = arg_conflicts(p, &f, p_global);
let multiple = "*";

View file

@ -62,7 +62,7 @@ fn gen_fig_inner(
write!(buffer, "{:indent$}subcommands: [\n", "", indent = indent).unwrap();
// generate subcommands
for subcommand in cmd.get_subcommands() {
let mut aliases: Vec<&str> = subcommand.get_all_aliases().map(|s| s.as_str()).collect();
let mut aliases: Vec<&str> = subcommand.get_all_aliases().collect();
if !aliases.is_empty() {
aliases.insert(0, subcommand.get_name());

View file

@ -544,7 +544,7 @@ impl Attrs {
let val = if parsed.iter().any(|a| matches!(a, ValueEnum(_))) {
quote_spanned!(ident.span()=> {
{
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> impl Iterator<Item=clap::Str>
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> impl Iterator<Item=String>
where
T: ::std::borrow::Borrow<#inner_type>
{
@ -646,7 +646,7 @@ impl Attrs {
let val = if parsed.iter().any(|a| matches!(a, ValueEnum(_))) {
quote_spanned!(ident.span()=> {
{
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> impl Iterator<Item=clap::Str>
fn iter_to_vals<T>(iterable: impl IntoIterator<Item = T>) -> impl Iterator<Item=String>
where
T: ::std::borrow::Borrow<#inner_type>
{

View file

@ -228,14 +228,14 @@ pub fn gen_augment(
let next_display_order = attrs.next_display_order();
if override_required {
Some(quote_spanned! { kind.span()=>
let #old_heading_var = #app_var.get_next_help_heading().cloned();
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = <#ty as clap::Args>::augment_args_for_update(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));
})
} else {
Some(quote_spanned! { kind.span()=>
let #old_heading_var = #app_var.get_next_help_heading().cloned();
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = <#ty as clap::Args>::augment_args(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));

View file

@ -193,14 +193,14 @@ fn gen_augment(
let next_display_order = attrs.next_display_order();
let subcommand = if override_required {
quote! {
let #old_heading_var = #app_var.get_next_help_heading().cloned();
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = <#ty as clap::Subcommand>::augment_subcommands_for_update(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));
}
} else {
quote! {
let #old_heading_var = #app_var.get_next_help_heading().cloned();
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = <#ty as clap::Subcommand>::augment_subcommands(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));

View file

@ -27,7 +27,7 @@ impl Man {
/// Create a new manual page.
pub fn new(mut cmd: clap::Command) -> Self {
cmd.build();
let title = cmd.get_name().as_str().to_owned();
let title = cmd.get_name().to_owned();
let section = "1".to_owned();
let date = "".to_owned();
let source = format!(
@ -244,7 +244,7 @@ impl Man {
}
fn _render_authors_section(&self, roff: &mut Roff) {
let author = roman(self.cmd.get_author().unwrap_or_default().as_str());
let author = roman(self.cmd.get_author().unwrap_or_default());
roff.control("SH", ["AUTHORS"]);
roff.text([author]);
}

View file

@ -2,7 +2,7 @@ use roff::{bold, italic, roman, Inline, Roff};
pub(crate) fn subcommand_heading(cmd: &clap::Command) -> &str {
match cmd.get_subcommand_help_heading() {
Some(title) => title.as_str(),
Some(title) => title,
None => "SUBCOMMANDS",
}
}
@ -28,7 +28,7 @@ pub(crate) fn description(roff: &mut Roff, cmd: &clap::Command) {
}
pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
let mut line = vec![bold(cmd.get_name().as_str()), roman(" ")];
let mut line = vec![bold(cmd.get_name()), roman(" ")];
for opt in cmd.get_arguments().filter(|i| !i.is_hide_set()) {
let (lhs, rhs) = option_markers(opt);
@ -74,7 +74,6 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
line.push(roman(lhs));
line.push(italic(
cmd.get_subcommand_value_name()
.map(|s| s.as_str())
.unwrap_or_else(|| subcommand_heading(cmd))
.to_lowercase(),
));
@ -109,7 +108,7 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
let mut body = vec![];
if let Some(help) = opt.get_long_help().or_else(|| opt.get_help()) {
body.push(roman(help.as_str()));
body.push(roman(help));
}
roff.control("TP", []);

View file

@ -3539,8 +3539,8 @@ impl Arg {
/// Get the help specified for this argument, if any
#[inline]
pub fn get_help(&self) -> Option<&Str> {
self.help.as_ref()
pub fn get_help(&self) -> Option<&str> {
self.help.as_deref()
}
/// Get the long help specified for this argument, if any
@ -3550,20 +3550,20 @@ impl Arg {
/// ```rust
/// # use clap::Arg;
/// let arg = Arg::new("foo").long_help("long help");
/// assert_eq!(Some("long help"), arg.get_long_help().map(|s| s.as_str()));
/// assert_eq!(Some("long help"), arg.get_long_help());
/// ```
///
#[inline]
pub fn get_long_help(&self) -> Option<&Str> {
self.long_help.as_ref()
pub fn get_long_help(&self) -> Option<&str> {
self.long_help.as_deref()
}
/// Get the help heading specified for this argument, if any
#[inline]
pub fn get_help_heading(&self) -> Option<&Str> {
pub fn get_help_heading(&self) -> Option<&str> {
self.help_heading
.as_ref()
.map(|s| s.as_ref())
.map(|s| s.as_deref())
.unwrap_or_default()
}
@ -3614,20 +3614,20 @@ impl Arg {
/// Get the long option name for this argument, if any
#[inline]
pub fn get_long(&self) -> Option<&Str> {
self.long.as_ref()
pub fn get_long(&self) -> Option<&str> {
self.long.as_deref()
}
/// Get visible aliases for this argument, if any
#[inline]
pub fn get_visible_aliases(&self) -> Option<Vec<&Str>> {
pub fn get_visible_aliases(&self) -> Option<Vec<&str>> {
if self.aliases.is_empty() {
None
} else {
Some(
self.aliases
.iter()
.filter_map(|(s, v)| if *v { Some(s) } else { None })
.filter_map(|(s, v)| if *v { Some(s.as_str()) } else { None })
.collect(),
)
}
@ -3635,17 +3635,17 @@ impl Arg {
/// Get *all* aliases for this argument, if any, both visible and hidden.
#[inline]
pub fn get_all_aliases(&self) -> Option<Vec<&Str>> {
pub fn get_all_aliases(&self) -> Option<Vec<&str>> {
if self.aliases.is_empty() {
None
} else {
Some(self.aliases.iter().map(|(s, _)| s).collect())
Some(self.aliases.iter().map(|(s, _)| s.as_str()).collect())
}
}
/// Get the long option name and its visible aliases, if any
#[inline]
pub fn get_long_and_visible_aliases(&self) -> Option<Vec<&Str>> {
pub fn get_long_and_visible_aliases(&self) -> Option<Vec<&str>> {
let mut longs = match self.get_long() {
Some(long) => vec![long],
None => return None,

View file

@ -3151,14 +3151,14 @@ impl Command {
/// Get the name of the binary.
#[inline]
pub fn get_display_name(&self) -> Option<&Str> {
self.display_name.as_ref()
pub fn get_display_name(&self) -> Option<&str> {
self.display_name.as_deref()
}
/// Get the name of the binary.
#[inline]
pub fn get_bin_name(&self) -> Option<&Str> {
self.bin_name.as_ref()
pub fn get_bin_name(&self) -> Option<&str> {
self.bin_name.as_deref()
}
/// Set binary name. Uses `&mut self` instead of `self`.
@ -3168,26 +3168,31 @@ impl Command {
/// Get the name of the cmd.
#[inline]
pub fn get_name(&self) -> &Str {
pub fn get_name(&self) -> &str {
self.name.as_str()
}
#[inline]
pub(crate) fn get_name_str(&self) -> &Str {
&self.name
}
/// Get the version of the cmd.
#[inline]
pub fn get_version(&self) -> Option<&Str> {
self.version.as_ref()
pub fn get_version(&self) -> Option<&str> {
self.version.as_deref()
}
/// Get the long version of the cmd.
#[inline]
pub fn get_long_version(&self) -> Option<&Str> {
self.long_version.as_ref()
pub fn get_long_version(&self) -> Option<&str> {
self.long_version.as_deref()
}
/// Get the authors of the cmd.
#[inline]
pub fn get_author(&self) -> Option<&Str> {
self.author.as_ref()
pub fn get_author(&self) -> Option<&str> {
self.author.as_deref()
}
/// Get the short flag of the subcommand.
@ -3198,38 +3203,41 @@ impl Command {
/// Get the long flag of the subcommand.
#[inline]
pub fn get_long_flag(&self) -> Option<&Str> {
self.long_flag.as_ref()
pub fn get_long_flag(&self) -> Option<&str> {
self.long_flag.as_deref()
}
/// Get the help message specified via [`Command::about`].
///
/// [`Command::about`]: Command::about()
#[inline]
pub fn get_about(&self) -> Option<&Str> {
self.about.as_ref()
pub fn get_about(&self) -> Option<&str> {
self.about.as_deref()
}
/// Get the help message specified via [`Command::long_about`].
///
/// [`Command::long_about`]: Command::long_about()
#[inline]
pub fn get_long_about(&self) -> Option<&Str> {
self.long_about.as_ref()
pub fn get_long_about(&self) -> Option<&str> {
self.long_about.as_deref()
}
/// Get the custom section heading specified via [`Command::next_help_heading`].
///
/// [`Command::help_heading`]: Command::help_heading()
#[inline]
pub fn get_next_help_heading(&self) -> Option<&Str> {
self.current_help_heading.as_ref()
pub fn get_next_help_heading(&self) -> Option<&str> {
self.current_help_heading.as_deref()
}
/// Iterate through the *visible* aliases for this subcommand.
#[inline]
pub fn get_visible_aliases(&self) -> impl Iterator<Item = &Str> + '_ {
self.aliases.iter().filter(|(_, vis)| *vis).map(|a| &a.0)
pub fn get_visible_aliases(&self) -> impl Iterator<Item = &str> + '_ {
self.aliases
.iter()
.filter(|(_, vis)| *vis)
.map(|a| a.0.as_str())
}
/// Iterate through the *visible* short aliases for this subcommand.
@ -3243,17 +3251,17 @@ impl Command {
/// Iterate through the *visible* long aliases for this subcommand.
#[inline]
pub fn get_visible_long_flag_aliases(&self) -> impl Iterator<Item = &Str> + '_ {
pub fn get_visible_long_flag_aliases(&self) -> impl Iterator<Item = &str> + '_ {
self.long_flag_aliases
.iter()
.filter(|(_, vis)| *vis)
.map(|a| &a.0)
.map(|a| a.0.as_str())
}
/// Iterate through the set of *all* the aliases for this subcommand, both visible and hidden.
#[inline]
pub fn get_all_aliases(&self) -> impl Iterator<Item = &Str> + '_ {
self.aliases.iter().map(|a| &a.0)
pub fn get_all_aliases(&self) -> impl Iterator<Item = &str> + '_ {
self.aliases.iter().map(|a| a.0.as_str())
}
/// Iterate through the set of *all* the short aliases for this subcommand, both visible and hidden.
@ -3264,8 +3272,8 @@ impl Command {
/// Iterate through the set of *all* the long aliases for this subcommand, both visible and hidden.
#[inline]
pub fn get_all_long_flag_aliases(&self) -> impl Iterator<Item = &Str> + '_ {
self.long_flag_aliases.iter().map(|a| &a.0)
pub fn get_all_long_flag_aliases(&self) -> impl Iterator<Item = &str> + '_ {
self.long_flag_aliases.iter().map(|a| a.0.as_str())
}
#[inline]
@ -3314,38 +3322,38 @@ impl Command {
/// Returns the help heading for listing subcommands.
#[inline]
pub fn get_subcommand_help_heading(&self) -> Option<&Str> {
self.subcommand_heading.as_ref()
pub fn get_subcommand_help_heading(&self) -> Option<&str> {
self.subcommand_heading.as_deref()
}
/// Returns the subcommand value name.
#[inline]
pub fn get_subcommand_value_name(&self) -> Option<&Str> {
self.subcommand_value_name.as_ref()
pub fn get_subcommand_value_name(&self) -> Option<&str> {
self.subcommand_value_name.as_deref()
}
/// Returns the help heading for listing subcommands.
#[inline]
pub fn get_before_help(&self) -> Option<&Str> {
self.before_help.as_ref()
pub fn get_before_help(&self) -> Option<&str> {
self.before_help.as_deref()
}
/// Returns the help heading for listing subcommands.
#[inline]
pub fn get_before_long_help(&self) -> Option<&Str> {
self.before_long_help.as_ref()
pub fn get_before_long_help(&self) -> Option<&str> {
self.before_long_help.as_deref()
}
/// Returns the help heading for listing subcommands.
#[inline]
pub fn get_after_help(&self) -> Option<&Str> {
self.after_help.as_ref()
pub fn get_after_help(&self) -> Option<&str> {
self.after_help.as_deref()
}
/// Returns the help heading for listing subcommands.
#[inline]
pub fn get_after_long_help(&self) -> Option<&Str> {
self.after_long_help.as_ref()
pub fn get_after_long_help(&self) -> Option<&str> {
self.after_long_help.as_deref()
}
/// Find subcommand such that its name or one of aliases equals `name`.
@ -3629,16 +3637,16 @@ impl Command {
// Internally used only
impl Command {
pub(crate) fn get_override_usage(&self) -> Option<&Str> {
self.usage_str.as_ref()
pub(crate) fn get_override_usage(&self) -> Option<&str> {
self.usage_str.as_deref()
}
pub(crate) fn get_override_help(&self) -> Option<&Str> {
self.help_str.as_ref()
pub(crate) fn get_override_help(&self) -> Option<&str> {
self.help_str.as_deref()
}
pub(crate) fn get_help_template(&self) -> Option<&Str> {
self.template.as_ref()
pub(crate) fn get_help_template(&self) -> Option<&str> {
self.template.as_deref()
}
pub(crate) fn get_term_width(&self) -> Option<usize> {
@ -3830,7 +3838,7 @@ impl Command {
// a space
let bin_name = format!(
"{}{}{}",
self.bin_name.as_ref().unwrap_or_default(),
self.bin_name.as_deref().unwrap_or_default(),
if self.bin_name.is_some() { " " } else { "" },
&*sc.name
);
@ -4131,13 +4139,13 @@ impl Command {
let ver = if use_long {
self.long_version
.as_ref()
.or(self.version.as_ref())
.as_deref()
.or(self.version.as_deref())
.unwrap_or_default()
} else {
self.version
.as_ref()
.or(self.long_version.as_ref())
.as_deref()
.or(self.long_version.as_deref())
.unwrap_or_default()
};
let display_name = self.get_display_name().unwrap_or_else(|| self.get_name());
@ -4227,7 +4235,7 @@ impl Command {
#[inline]
pub(crate) fn aliases_to(&self, name: impl AsRef<std::ffi::OsStr>) -> bool {
let name = name.as_ref();
*self.get_name() == name || self.get_all_aliases().any(|alias| *alias == name)
self.get_name() == name || self.get_all_aliases().any(|alias| alias == name)
}
/// Check if this subcommand can be referred to as `name`. In other words,
@ -4244,9 +4252,9 @@ impl Command {
pub(crate) fn long_flag_aliases_to(&self, flag: &str) -> bool {
match self.long_flag.as_ref() {
Some(long_flag) => {
*long_flag == flag || self.get_all_long_flag_aliases().any(|alias| *alias == flag)
long_flag == flag || self.get_all_long_flag_aliases().any(|alias| alias == flag)
}
None => self.get_all_long_flag_aliases().any(|alias| *alias == flag),
None => self.get_all_long_flag_aliases().any(|alias| alias == flag),
}
}
@ -4271,7 +4279,7 @@ impl Command {
/// Iterate through all the names of all subcommands (not recursively), including aliases.
/// Used for suggestions.
pub(crate) fn all_subcommand_names(&self) -> impl Iterator<Item = &Str> + Captures {
pub(crate) fn all_subcommand_names(&self) -> impl Iterator<Item = &str> + Captures {
self.get_subcommands().flat_map(|sc| {
let name = sc.get_name();
let aliases = sc.get_all_aliases();
@ -4357,14 +4365,14 @@ impl Command {
}
/// Find a flag subcommand name by short flag or an alias
pub(crate) fn find_short_subcmd(&self, c: char) -> Option<&Str> {
pub(crate) fn find_short_subcmd(&self, c: char) -> Option<&str> {
self.get_subcommands()
.find(|sc| sc.short_flag_aliases_to(c))
.map(|sc| sc.get_name())
}
/// Find a flag subcommand name by long flag or an alias
pub(crate) fn find_long_subcmd(&self, long: &str) -> Option<&Str> {
pub(crate) fn find_long_subcmd(&self, long: &str) -> Option<&str> {
self.get_subcommands()
.find(|sc| sc.long_flag_aliases_to(long))
.map(|sc| sc.get_name())

View file

@ -138,22 +138,22 @@ impl PossibleValue {
impl PossibleValue {
/// Get the name of the argument value
#[inline]
pub fn get_name(&self) -> &Str {
&self.name
pub fn get_name(&self) -> &str {
self.name.as_str()
}
/// Get the help specified for this argument, if any
#[inline]
pub fn get_help(&self) -> Option<&Str> {
self.help.as_ref()
pub fn get_help(&self) -> Option<&str> {
self.help.as_deref()
}
/// Get the help specified for this argument, if any and the argument
/// value is not hidden
#[inline]
pub(crate) fn get_visible_help(&self) -> Option<&Str> {
pub(crate) fn get_visible_help(&self) -> Option<&str> {
if !self.hide {
self.help.as_ref()
self.help.as_deref()
} else {
None
}
@ -187,8 +187,8 @@ impl PossibleValue {
/// Returns all valid values of the argument value.
///
/// Namely the name and all aliases.
pub fn get_name_and_aliases(&self) -> impl Iterator<Item = &Str> + '_ {
iter::once(&self.name).chain(self.aliases.iter())
pub fn get_name_and_aliases(&self) -> impl Iterator<Item = &str> + '_ {
iter::once(self.get_name()).chain(self.aliases.iter().map(|s| s.as_str()))
}
/// Tests if the value is valid for this argument value
@ -210,7 +210,7 @@ impl PossibleValue {
pub fn matches(&self, value: &str, ignore_case: bool) -> bool {
if ignore_case {
self.get_name_and_aliases()
.any(|name| eq_ignore_case(name.as_str(), value))
.any(|name| eq_ignore_case(name, value))
} else {
self.get_name_and_aliases().any(|name| name == value)
}

View file

@ -9,11 +9,7 @@ fn propagate_version() {
.subcommand(Command::new("sub1"));
cmd._propagate();
assert_eq!(
cmd.get_subcommands()
.next()
.unwrap()
.get_version()
.map(|s| s.as_str()),
cmd.get_subcommands().next().unwrap().get_version(),
Some("1.1")
);
}

View file

@ -936,7 +936,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for E
.iter()
.filter_map(|v| v.to_possible_value())
.filter(|v| !v.is_hide_set())
.map(|v| v.get_name().as_str().to_owned())
.map(|v| v.get_name().to_owned())
.collect::<Vec<_>>()
};
@ -1064,7 +1064,7 @@ impl TypedValueParser for PossibleValuesParser {
.0
.iter()
.filter(|v| !v.is_hide_set())
.map(|v| v.get_name().as_str().to_owned())
.map(|v| v.get_name().to_owned())
.collect::<Vec<_>>();
Err(crate::Error::invalid_value(
@ -1525,7 +1525,7 @@ impl TypedValueParser for BoolValueParser {
} else {
// Intentionally showing hidden as we hide all of them
let possible_vals = Self::possible_values()
.map(|v| v.get_name().as_str().to_owned())
.map(|v| v.get_name().to_owned())
.collect::<Vec<_>>();
return Err(crate::Error::invalid_value(

View file

@ -166,11 +166,11 @@ fn append_keys(keys: &mut Vec<Key>, arg: &Arg, index: usize) {
let key = KeyType::Position(pos_index);
keys.push(Key { key, index });
} else {
if let Some(short) = arg.get_short() {
if let Some(short) = arg.short {
let key = KeyType::Short(short);
keys.push(Key { key, index });
}
if let Some(long) = arg.get_long() {
if let Some(long) = arg.long.clone() {
let key = KeyType::Long(long.into());
keys.push(Key { key, index });
}

View file

@ -80,9 +80,9 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
debug!("Help::write_help");
if let Some(h) = self.cmd.get_override_help() {
self.none(h.as_str())?;
self.none(h)?;
} else if let Some(tmpl) = self.cmd.get_help_template() {
self.write_templated_help(tmpl.as_str())?;
self.write_templated_help(tmpl)?;
} else {
let pos = self
.cmd
@ -259,13 +259,7 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
.unwrap_or_default()
};
self.help(
Some(arg),
about.as_str(),
spec_vals,
next_line_help,
longest,
)?;
self.help(Some(arg), about, spec_vals, next_line_help, longest)?;
if !last_arg {
self.none("\n")?;
@ -495,7 +489,7 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
.expect("Only called with possible value");
let help_longest = possible_vals
.iter()
.filter_map(|f| f.get_visible_help().map(|s| display_width(s.as_str())))
.filter_map(|f| f.get_visible_help().map(display_width))
.max()
.expect("Only called with possible value with help");
// should new line
@ -515,7 +509,7 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
self.none("\n")?;
self.spaces(spaces)?;
self.none("- ")?;
self.good(pv.get_name().as_str())?;
self.good(pv.get_name())?;
if let Some(help) = pv.get_help() {
debug!("Help::help: Possible Value help");
@ -567,7 +561,7 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
} else {
// force_next_line
let h = arg.get_help().unwrap_or_default();
let h_w = display_width(h.as_str()) + display_width(spec_vals);
let h_w = display_width(h) + display_width(spec_vals);
let taken = longest + 12;
self.term_w >= taken
&& (taken as f32 / self.term_w as f32) > 0.40
@ -882,8 +876,7 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
self.warning(
self.cmd
.get_subcommand_help_heading()
.unwrap_or(&default_help_heading)
.as_str(),
.unwrap_or(&default_help_heading),
)?;
self.warning(":\n")?;

View file

@ -4,10 +4,9 @@ use crate::parser::ArgMatcher;
use crate::util::ChildGraph;
use crate::util::FlatSet;
use crate::util::Id;
use crate::util::Str;
use crate::INTERNAL_ERROR_MSG;
static DEFAULT_SUB_VALUE_NAME: Str = Str::from_static_ref("SUBCOMMAND");
static DEFAULT_SUB_VALUE_NAME: &str = "SUBCOMMAND";
pub(crate) struct Usage<'cmd> {
cmd: &'cmd Command,
@ -41,7 +40,7 @@ impl<'cmd> Usage<'cmd> {
pub(crate) fn create_usage_no_title(&self, used: &[Id]) -> String {
debug!("Usage::create_usage_no_title");
if let Some(u) = self.cmd.get_override_usage() {
String::from(u.as_str())
u.to_owned()
} else if used.is_empty() {
self.create_help_usage(true)
} else {
@ -56,8 +55,8 @@ impl<'cmd> Usage<'cmd> {
let name = self
.cmd
.get_usage_name()
.or_else(|| self.cmd.get_bin_name().map(|s| s.as_str()))
.unwrap_or_else(|| self.cmd.get_name().as_str());
.or_else(|| self.cmd.get_bin_name())
.unwrap_or_else(|| self.cmd.get_name());
usage.push_str(name);
let req_string = if incl_reqs {
self.get_required_usage_from(&[], None, false)
@ -135,7 +134,7 @@ impl<'cmd> Usage<'cmd> {
let placeholder = self
.cmd
.get_subcommand_value_name()
.unwrap_or(&DEFAULT_SUB_VALUE_NAME);
.unwrap_or(DEFAULT_SUB_VALUE_NAME);
if self.cmd.is_subcommand_negates_reqs_set()
|| self.cmd.is_args_conflicts_with_subcommands_set()
{
@ -177,8 +176,8 @@ impl<'cmd> Usage<'cmd> {
usage.push_str(
self.cmd
.get_usage_name()
.or_else(|| self.cmd.get_bin_name().map(|s| s.as_str()))
.unwrap_or_else(|| self.cmd.get_name().as_str()),
.or_else(|| self.cmd.get_bin_name())
.unwrap_or_else(|| self.cmd.get_name()),
);
usage.push_str(&*r_string);
if self.cmd.is_subcommand_required_set() {
@ -311,9 +310,7 @@ impl<'cmd> Usage<'cmd> {
debug!("Usage::needs_options_tag:iter: f={}", f.get_id());
// Don't print `[OPTIONS]` just for help or version
if f.get_long().map(|s| s.as_str()) == Some("help")
|| f.get_long().map(|s| s.as_str()) == Some("version")
{
if f.get_long() == Some("help") || f.get_long() == Some("version") {
debug!("Usage::needs_options_tag:iter Option is built-in");
continue;
}

View file

@ -32,7 +32,7 @@ impl ArgMatcher {
#[cfg(debug_assertions)]
valid_subcommands: _cmd
.get_subcommands()
.map(|sc| sc.get_name().clone())
.map(|sc| sc.get_name().to_owned())
.collect(),
..Default::default()
},

View file

@ -64,7 +64,7 @@ where
let subcommand_name = subcommand.get_name();
let candidate = did_you_mean(arg, longs).pop()?;
let score = remaining_args.iter().position(|x| *subcommand_name == *x)?;
let score = remaining_args.iter().position(|x| subcommand_name == *x)?;
Some((score, (candidate, Some(subcommand_name.to_string()))))
})
.min_by_key(|(x, _)| *x)

View file

@ -66,7 +66,7 @@ pub struct ArgMatches {
#[cfg(debug_assertions)]
pub(crate) valid_args: Vec<Id>,
#[cfg(debug_assertions)]
pub(crate) valid_subcommands: Vec<Str>,
pub(crate) valid_subcommands: Vec<String>,
pub(crate) args: FlatMap<Id, MatchedArg>,
pub(crate) subcommand: Option<Box<SubCommand>>,
}
@ -863,8 +863,8 @@ impl ArgMatches {
pub fn is_valid_subcommand(&self, _name: &str) -> bool {
#[cfg(debug_assertions)]
{
let _name = Str::from(_name.to_owned());
_name == "" || self.valid_subcommands.contains(&_name)
let _name = _name.to_owned();
_name == String::default() || self.valid_subcommands.contains(&_name)
}
#[cfg(not(debug_assertions))]
{
@ -1073,8 +1073,8 @@ impl ArgMatches {
fn get_subcommand(&self, name: &str) -> Option<&SubCommand> {
#[cfg(debug_assertions)]
{
let name = Str::from(name.to_owned());
if name == "" || self.valid_subcommands.contains(&name) {
let name = name.to_owned();
if name == String::default() || self.valid_subcommands.contains(&name) {
} else {
panic!("`{}` is not a name of a subcommand.", name);
}

View file

@ -509,7 +509,6 @@ impl<'cmd> Parser<'cmd> {
self.cmd
.get_bin_name()
.unwrap_or_else(|| self.cmd.get_name())
.as_str()
.to_owned(),
Usage::new(self.cmd).create_usage_with_title(&[]),
);
@ -565,7 +564,7 @@ impl<'cmd> Parser<'cmd> {
}
// Checks if the arg matches a long flag subcommand name, or any of its aliases (if defined)
fn possible_long_flag_subcommand(&self, arg: &str) -> Option<&Str> {
fn possible_long_flag_subcommand(&self, arg: &str) -> Option<&str> {
debug!("Parser::possible_long_flag_subcommand: arg={:?}", arg);
if self.cmd.is_infer_subcommands_set() {
let options = self
@ -699,7 +698,7 @@ impl<'cmd> Parser<'cmd> {
}
}
matcher.subcommand(SubCommand {
name: sc.get_name().clone(),
name: sc.get_name_str().clone(),
matches: sc_matcher.into_inner(),
});
}
@ -746,7 +745,7 @@ impl<'cmd> Parser<'cmd> {
self.cmd.get_arguments().find_map(|a| {
if let Some(long) = a.get_long() {
if long.starts_with(long_arg) {
return Some((long.as_str(), a));
return Some((long, a));
}
}
a.aliases
@ -1269,7 +1268,7 @@ impl<'cmd> Parser<'cmd> {
&super::get_possible_values_cli(arg)
.iter()
.filter(|pv| !pv.is_hide_set())
.map(|n| n.get_name().as_str().to_owned())
.map(|n| n.get_name().to_owned())
.collect::<Vec<_>>(),
arg.to_string(),
));

View file

@ -45,7 +45,7 @@ impl<'cmd> Validator<'cmd> {
&get_possible_values_cli(o)
.iter()
.filter(|pv| !pv.is_hide_set())
.map(|n| n.get_name().as_str().to_owned())
.map(|n| n.get_name().to_owned())
.collect::<Vec<_>>(),
o.to_string(),
));

View file

@ -33,13 +33,6 @@ impl Str {
}
}
impl Default for &'_ Str {
fn default() -> Self {
static DEFAULT: Str = Str::from_static_ref("");
&DEFAULT
}
}
impl From<&'_ Str> for Str {
fn from(id: &'_ Str) -> Self {
id.clone()

View file

@ -2728,10 +2728,7 @@ fn display_name_explicit() {
.bin_name("app.exe")
.display_name("app.display");
cmd.build();
assert_eq!(
cmd.get_display_name().map(|s| s.as_str()),
Some("app.display")
);
assert_eq!(cmd.get_display_name(), Some("app.display"));
}
#[test]
@ -2739,10 +2736,7 @@ fn display_name_subcommand_default() {
let mut cmd = Command::new("parent").subcommand(Command::new("child").bin_name("child.exe"));
cmd.build();
assert_eq!(
cmd.find_subcommand("child")
.unwrap()
.get_display_name()
.map(|s| s.as_str()),
cmd.find_subcommand("child").unwrap().get_display_name(),
Some("parent-child")
);
}
@ -2756,10 +2750,7 @@ fn display_name_subcommand_explicit() {
);
cmd.build();
assert_eq!(
cmd.find_subcommand("child")
.unwrap()
.get_display_name()
.map(|s| s.as_str()),
cmd.find_subcommand("child").unwrap().get_display_name(),
Some("child.display")
);
}

View file

@ -233,11 +233,8 @@ fn doc_comment_about_handles_both_abouts() {
}
let cmd = Opts::command();
assert_eq!(
cmd.get_about().map(|s| s.as_str()),
Some("Opts doc comment summary")
);
assert_eq!(cmd.get_about(), Some("Opts doc comment summary"));
// clap will fallback to `about` on `None`. The main care about is not providing a `Sub` doc
// comment.
assert_eq!(cmd.get_long_about().map(|s| s.as_str()), None);
assert_eq!(cmd.get_long_about(), None);
}

View file

@ -94,11 +94,7 @@ fn inferred_help() {
let mut cmd = Opt::command();
cmd.build();
let arg = cmd.get_arguments().find(|a| a.get_id() == "help").unwrap();
assert_eq!(
arg.get_help().map(|s| s.as_str()),
Some("Foo"),
"Incorrect help"
);
assert_eq!(arg.get_help(), Some("Foo"), "Incorrect help");
assert!(matches!(arg.get_action(), clap::ArgAction::Help));
}
@ -118,11 +114,7 @@ fn inferred_version() {
.get_arguments()
.find(|a| a.get_id() == "version")
.unwrap();
assert_eq!(
arg.get_help().map(|s| s.as_str()),
Some("Foo"),
"Incorrect help"
);
assert_eq!(arg.get_help(), Some("Foo"), "Incorrect help");
assert!(matches!(arg.get_action(), clap::ArgAction::Version));
}

View file

@ -18,12 +18,7 @@ fn arg_help_heading_applied() {
.get_arguments()
.find(|a| a.get_id() == "should_be_in_section_a")
.unwrap();
assert_eq!(
should_be_in_section_a
.get_help_heading()
.map(|s| s.as_str()),
Some("HEADING A")
);
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
let should_be_in_section_b = cmd
.get_arguments()
@ -51,21 +46,14 @@ fn app_help_heading_applied() {
.get_arguments()
.find(|a| a.get_id() == "should_be_in_section_a")
.unwrap();
assert_eq!(
should_be_in_section_a
.get_help_heading()
.map(|s| s.as_str()),
Some("HEADING A")
);
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
let should_be_in_default_section = cmd
.get_arguments()
.find(|a| a.get_id() == "should_be_in_default_section")
.unwrap();
assert_eq!(
should_be_in_default_section
.get_help_heading()
.map(|s| s.as_str()),
should_be_in_default_section.get_help_heading(),
Some("DEFAULT")
);
}
@ -135,23 +123,13 @@ fn app_help_heading_flattened() {
.get_arguments()
.find(|a| a.get_id() == "should_be_in_section_a")
.unwrap();
assert_eq!(
should_be_in_section_a
.get_help_heading()
.map(|s| s.as_str()),
Some("HEADING A")
);
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
let should_be_in_section_b = cmd
.get_arguments()
.find(|a| a.get_id() == "should_be_in_section_b")
.unwrap();
assert_eq!(
should_be_in_section_b
.get_help_heading()
.map(|s| s.as_str()),
Some("HEADING B")
);
assert_eq!(should_be_in_section_b.get_help_heading(), Some("HEADING B"));
let should_be_in_default_section = cmd
.get_arguments()
@ -165,10 +143,7 @@ fn app_help_heading_flattened() {
.get_arguments()
.find(|a| a.get_id() == "should_be_in_sub_a")
.unwrap();
assert_eq!(
should_be_in_sub_a.get_help_heading().map(|s| s.as_str()),
Some("SUB A")
);
assert_eq!(should_be_in_sub_a.get_help_heading(), Some("SUB A"));
let sub_b_one = cmd.find_subcommand("sub-b-one").unwrap();
@ -176,10 +151,7 @@ fn app_help_heading_flattened() {
.get_arguments()
.find(|a| a.get_id() == "should_be_in_sub_b")
.unwrap();
assert_eq!(
should_be_in_sub_b.get_help_heading().map(|s| s.as_str()),
Some("SUB B")
);
assert_eq!(should_be_in_sub_b.get_help_heading(), Some("SUB B"));
let sub_c = cmd.find_subcommand("sub-c").unwrap();
let sub_c_one = sub_c.find_subcommand("sub-c-one").unwrap();
@ -188,10 +160,7 @@ fn app_help_heading_flattened() {
.get_arguments()
.find(|a| a.get_id() == "should_be_in_sub_c")
.unwrap();
assert_eq!(
should_be_in_sub_c.get_help_heading().map(|s| s.as_str()),
Some("SUB C")
);
assert_eq!(should_be_in_sub_c.get_help_heading(), Some("SUB C"));
}
#[test]
@ -215,12 +184,7 @@ fn flatten_field_with_help_heading() {
.get_arguments()
.find(|a| a.get_id() == "should_be_in_section_a")
.unwrap();
assert_eq!(
should_be_in_section_a
.get_help_heading()
.map(|s| s.as_str()),
Some("HEADING A")
);
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
}
// The challenge with this test is creating an error situation not caught by `clap`'s error checking

View file

@ -3,7 +3,7 @@ mod arg {
fn name_explicit() {
let arg = clap::arg!(foo: --bar <NUM>);
assert_eq!(arg.get_id(), "foo");
assert_eq!(arg.get_long().map(|s| s.as_str()), Some("bar"));
assert_eq!(arg.get_long(), Some("bar"));
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
assert!(arg.is_required_set());
}
@ -12,7 +12,7 @@ mod arg {
fn name_from_long() {
let arg = clap::arg!(--bar <NUM>);
assert_eq!(arg.get_id(), "bar");
assert_eq!(arg.get_long().map(|s| s.as_str()), Some("bar"));
assert_eq!(arg.get_long(), Some("bar"));
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
assert!(arg.is_required_set());
}
@ -21,7 +21,7 @@ mod arg {
fn name_from_value() {
let arg = clap::arg!(<NUM>);
assert_eq!(arg.get_id(), "NUM");
assert_eq!(arg.get_long().map(|s| s.as_str()), None);
assert_eq!(arg.get_long(), None);
assert_eq!(arg.get_value_names(), Some(vec!["NUM".into()].as_slice()));
assert!(arg.is_required_set());
}
@ -46,7 +46,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
assert_eq!(arg.get_num_args(), None);
assert!(!arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(foo: -'b');
assert_eq!(arg.get_id(), "foo");
@ -54,7 +54,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
assert_eq!(arg.get_num_args(), None);
assert!(!arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(foo: -b ...);
assert_eq!(arg.get_id(), "foo");
@ -62,7 +62,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Count));
assert_eq!(arg.get_num_args(), None);
assert!(!arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(foo: -b "How to use it");
assert_eq!(arg.get_id(), "foo");
@ -70,46 +70,46 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
assert_eq!(arg.get_num_args(), None);
assert!(!arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), Some("How to use it"));
assert_eq!(arg.get_help(), Some("How to use it"));
}
#[test]
fn short_and_long() {
let arg = clap::arg!(foo: -b --hello);
assert_eq!(arg.get_id(), "foo");
assert_eq!(arg.get_long().map(|s| s.as_str()), Some("hello"));
assert_eq!(arg.get_long(), Some("hello"));
assert_eq!(arg.get_short(), Some('b'));
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
assert_eq!(arg.get_num_args(), None);
assert!(!arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(foo: -'b' --hello);
assert_eq!(arg.get_id(), "foo");
assert_eq!(arg.get_long().map(|s| s.as_str()), Some("hello"));
assert_eq!(arg.get_long(), Some("hello"));
assert_eq!(arg.get_short(), Some('b'));
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
assert_eq!(arg.get_num_args(), None);
assert!(!arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(foo: -b --hello ...);
assert_eq!(arg.get_id(), "foo");
assert_eq!(arg.get_long().map(|s| s.as_str()), Some("hello"));
assert_eq!(arg.get_long(), Some("hello"));
assert_eq!(arg.get_short(), Some('b'));
assert!(matches!(arg.get_action(), clap::ArgAction::Count));
assert_eq!(arg.get_num_args(), None);
assert!(!arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(foo: -b --hello "How to use it");
assert_eq!(arg.get_id(), "foo");
assert_eq!(arg.get_long().map(|s| s.as_str()), Some("hello"));
assert_eq!(arg.get_long(), Some("hello"));
assert_eq!(arg.get_short(), Some('b'));
assert!(matches!(arg.get_action(), clap::ArgAction::SetTrue));
assert_eq!(arg.get_num_args(), None);
assert!(!arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), Some("How to use it"));
assert_eq!(arg.get_help(), Some("How to use it"));
}
#[test]
@ -186,7 +186,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
assert_eq!(arg.get_num_args(), None);
assert!(arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(foo: -'b' <NUM>);
assert_eq!(arg.get_id(), "foo");
@ -194,7 +194,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
assert_eq!(arg.get_num_args(), None);
assert!(arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(foo: -b <NUM> ...);
assert_eq!(arg.get_id(), "foo");
@ -202,7 +202,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Append));
assert_eq!(arg.get_num_args(), None);
assert!(arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(foo: -b <NUM> "How to use it");
assert_eq!(arg.get_id(), "foo");
@ -210,7 +210,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
assert_eq!(arg.get_num_args(), None);
assert!(arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), Some("How to use it"));
assert_eq!(arg.get_help(), Some("How to use it"));
}
#[test]
@ -221,7 +221,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
assert_eq!(arg.get_num_args(), None);
assert!(arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!([NUM]);
assert_eq!(arg.get_id(), "NUM");
@ -229,7 +229,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
assert_eq!(arg.get_num_args(), None);
assert!(!arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(<NUM>);
assert_eq!(arg.get_id(), "NUM");
@ -237,7 +237,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
assert_eq!(arg.get_num_args(), None);
assert!(arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(foo: <NUM>);
assert_eq!(arg.get_id(), "foo");
@ -245,7 +245,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
assert_eq!(arg.get_num_args(), None);
assert!(arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(<NUM> ...);
assert_eq!(arg.get_id(), "NUM");
@ -253,7 +253,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Append));
assert_eq!(arg.get_num_args(), Some((1..).into()));
assert!(arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), None);
assert_eq!(arg.get_help(), None);
let arg = clap::arg!(<NUM> "How to use it");
assert_eq!(arg.get_id(), "NUM");
@ -261,7 +261,7 @@ mod arg {
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
assert_eq!(arg.get_num_args(), None);
assert!(arg.is_required_set());
assert_eq!(arg.get_help().map(|s| s.as_str()), Some("How to use it"));
assert_eq!(arg.get_help(), Some("How to use it"));
}
#[test]