fix(parser): Conflict-with-self is back on by default

See #4261 for more details
This commit is contained in:
Ed Page 2022-09-23 15:18:14 -05:00
parent 18ed37ee67
commit 9bccded7ed
19 changed files with 142 additions and 67 deletions

View file

@ -183,6 +183,9 @@ Subtle changes (i.e. compiler won't catch):
- `ArgAction::Count`, requiring `ArgMatches::get_count` instead of `ArgMatches::occurrences_of` - `ArgAction::Count`, requiring `ArgMatches::get_count` instead of `ArgMatches::occurrences_of`
- `ArgAction::Set`, requiring `ArgMatches::get_one` instead of `ArgMatches::value_of` - `ArgAction::Set`, requiring `ArgMatches::get_one` instead of `ArgMatches::value_of`
- `ArgAction::Append`, requiring `ArgMatches::get_many` instead of `ArgMatches::values_of` - `ArgAction::Append`, requiring `ArgMatches::get_many` instead of `ArgMatches::values_of`
- `ArgAction::Set`, `ArgAction::SetTrue`, and `Arg::Action::SetFalse` now
conflict by default to be like `ArgAction::StoreValue` and
`ArgAction::IncOccurrences`, requiring `cmd.args_override_self(true)` to override instead (#4261)
- By default, an `Arg`s default action is `ArgAction::Set`, rather than `ArgAction::IncOccurrence` to reduce confusing magic through consistency (#2687, #4032, see also #3977) - By default, an `Arg`s default action is `ArgAction::Set`, rather than `ArgAction::IncOccurrence` to reduce confusing magic through consistency (#2687, #4032, see also #3977)
- `mut_arg` can no longer be used to customize help and version arguments, instead disable them (`Command::disable_help_flag`, `Command::disable_version_flag`) and provide your own (#4056) - `mut_arg` can no longer be used to customize help and version arguments, instead disable them (`Command::disable_help_flag`, `Command::disable_version_flag`) and provide your own (#4056)
- Removed lifetimes from `Command`, `Arg`, `ArgGroup`, and `PossibleValue`, assuming `'static`. `string` feature flag will enable support for `String`s (#1041, #2150, #4223) - Removed lifetimes from `Command`, `Arg`, `ArgGroup`, and `PossibleValue`, assuming `'static`. `string` feature flag will enable support for `String`s (#1041, #2150, #4223)

View file

@ -16,6 +16,11 @@ $ 03_01_flag_bool --verbose
verbose: true verbose: true
$ 03_01_flag_bool --verbose --verbose $ 03_01_flag_bool --verbose --verbose
verbose: true ? failed
error: The argument '--verbose' cannot be used with '--verbose'
Usage: 03_01_flag_bool[EXE] [OPTIONS]
For more information try '--help'
``` ```

View file

@ -16,6 +16,11 @@ $ 03_01_flag_bool_derive --verbose
verbose: true verbose: true
$ 03_01_flag_bool_derive --verbose --verbose $ 03_01_flag_bool_derive --verbose --verbose
verbose: true ? failed
error: The argument '--verbose' cannot be used with '--verbose'
Usage: 03_01_flag_bool_derive[EXE] [OPTIONS]
For more information try '--help'
``` ```

View file

@ -27,6 +27,10 @@
pub enum ArgAction { pub enum ArgAction {
/// When encountered, store the associated value(s) in [`ArgMatches`][crate::ArgMatches] /// When encountered, store the associated value(s) in [`ArgMatches`][crate::ArgMatches]
/// ///
/// **NOTE:** If the argument has previously been seen, it will result in a
/// [`ArgumentConflict`][crate::error::ErrorKind::ArgumentConflict] unless
/// [`Command::args_override_self(true)`][crate::Command::args_override_self] is set.
///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
@ -76,6 +80,10 @@ pub enum ArgAction {
/// No value is allowed. To optionally accept a value, see /// No value is allowed. To optionally accept a value, see
/// [`Arg::default_missing_value`][super::Arg::default_missing_value] /// [`Arg::default_missing_value`][super::Arg::default_missing_value]
/// ///
/// **NOTE:** If the argument has previously been seen, it will result in a
/// [`ArgumentConflict`][crate::error::ErrorKind::ArgumentConflict] unless
/// [`Command::args_override_self(true)`][crate::Command::args_override_self] is set.
///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
@ -88,7 +96,7 @@ pub enum ArgAction {
/// .action(clap::ArgAction::SetTrue) /// .action(clap::ArgAction::SetTrue)
/// ); /// );
/// ///
/// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag", "--flag"]).unwrap(); /// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag"]).unwrap();
/// assert!(matches.contains_id("flag")); /// assert!(matches.contains_id("flag"));
/// assert_eq!( /// assert_eq!(
/// matches.get_one::<bool>("flag").copied(), /// matches.get_one::<bool>("flag").copied(),
@ -123,7 +131,7 @@ pub enum ArgAction {
/// ) /// )
/// ); /// );
/// ///
/// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag", "--flag"]).unwrap(); /// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag"]).unwrap();
/// assert!(matches.contains_id("flag")); /// assert!(matches.contains_id("flag"));
/// assert_eq!( /// assert_eq!(
/// matches.get_one::<usize>("flag").copied(), /// matches.get_one::<usize>("flag").copied(),
@ -145,6 +153,10 @@ pub enum ArgAction {
/// No value is allowed. To optionally accept a value, see /// No value is allowed. To optionally accept a value, see
/// [`Arg::default_missing_value`][super::Arg::default_missing_value] /// [`Arg::default_missing_value`][super::Arg::default_missing_value]
/// ///
/// **NOTE:** If the argument has previously been seen, it will result in a
/// [`ArgumentConflict`][crate::error::ErrorKind::ArgumentConflict] unless
/// [`Command::args_override_self(true)`][crate::Command::args_override_self] is set.
///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust

View file

@ -31,6 +31,7 @@ pub(crate) enum AppSettings {
IgnoreErrors, IgnoreErrors,
AllowHyphenValues, AllowHyphenValues,
AllowNegativeNumbers, AllowNegativeNumbers,
AllArgsOverrideSelf,
AllowMissingPositional, AllowMissingPositional,
TrailingVarArg, TrailingVarArg,
DontDelimitTrailingValues, DontDelimitTrailingValues,
@ -93,6 +94,7 @@ bitflags! {
const VALID_ARG_FOUND = 1 << 35; const VALID_ARG_FOUND = 1 << 35;
const INFER_SUBCOMMANDS = 1 << 36; const INFER_SUBCOMMANDS = 1 << 36;
const CONTAINS_LAST = 1 << 37; const CONTAINS_LAST = 1 << 37;
const ARGS_OVERRIDE_SELF = 1 << 38;
const HELP_REQUIRED = 1 << 39; const HELP_REQUIRED = 1 << 39;
const SUBCOMMAND_PRECEDENCE_OVER_ARG = 1 << 40; const SUBCOMMAND_PRECEDENCE_OVER_ARG = 1 << 40;
const DISABLE_HELP_FLAG = 1 << 41; const DISABLE_HELP_FLAG = 1 << 41;
@ -163,6 +165,8 @@ impl_settings! { AppSettings, AppFlags,
=> Flags::BIN_NAME_BUILT, => Flags::BIN_NAME_BUILT,
InferSubcommands InferSubcommands
=> Flags::INFER_SUBCOMMANDS, => Flags::INFER_SUBCOMMANDS,
AllArgsOverrideSelf
=> Flags::ARGS_OVERRIDE_SELF,
InferLongArgs InferLongArgs
=> Flags::INFER_LONG_ARGS => Flags::INFER_LONG_ARGS
} }

View file

@ -978,6 +978,23 @@ impl Command {
} }
} }
/// Specifies that all arguments override themselves.
///
/// This is the equivalent to saying the `foo` arg using [`Arg::overrides_with("foo")`] for all
/// defined arguments.
///
/// **NOTE:** This choice is propagated to all child subcommands.
///
/// [`Arg::overrides_with("foo")`]: crate::Arg::overrides_with()
#[inline]
pub fn args_override_self(self, yes: bool) -> Self {
if yes {
self.global_setting(AppSettings::AllArgsOverrideSelf)
} else {
self.unset_global_setting(AppSettings::AllArgsOverrideSelf)
}
}
/// Disables the automatic delimiting of values after `--` or when [`Command::trailing_var_arg`] /// Disables the automatic delimiting of values after `--` or when [`Command::trailing_var_arg`]
/// was used. /// was used.
/// ///
@ -3671,6 +3688,11 @@ impl Command {
self.is_set(AppSettings::ArgsNegateSubcommands) self.is_set(AppSettings::ArgsNegateSubcommands)
} }
#[doc(hidden)]
pub fn is_args_override_self(&self) -> bool {
self.is_set(AppSettings::AllArgsOverrideSelf)
}
/// Report whether [`Command::subcommand_precedence_over_arg`] is set /// Report whether [`Command::subcommand_precedence_over_arg`] is set
pub fn is_subcommand_precedence_over_arg_set(&self) -> bool { pub fn is_subcommand_precedence_over_arg_set(&self) -> bool {
self.is_set(AppSettings::SubcommandPrecedenceOverArg) self.is_set(AppSettings::SubcommandPrecedenceOverArg)

View file

@ -657,7 +657,7 @@ pub trait TypedValueParser: Clone + Send + Sync + 'static {
/// ) /// )
/// ); /// );
/// ///
/// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag", "--flag"]).unwrap(); /// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag"]).unwrap();
/// assert!(matches.contains_id("flag")); /// assert!(matches.contains_id("flag"));
/// assert_eq!( /// assert_eq!(
/// matches.get_one::<usize>("flag").copied(), /// matches.get_one::<usize>("flag").copied(),

View file

@ -101,8 +101,8 @@ impl ArgMatcher {
self.matches.args.get_mut(arg) self.matches.args.get_mut(arg)
} }
pub(crate) fn remove(&mut self, arg: &Id) { pub(crate) fn remove(&mut self, arg: &Id) -> bool {
self.matches.args.remove(arg); self.matches.args.remove(arg).is_some()
} }
pub(crate) fn contains(&self, arg: &Id) -> bool { pub(crate) fn contains(&self, arg: &Id) -> bool {

View file

@ -162,7 +162,7 @@ impl ArgMatches {
/// .action(clap::ArgAction::SetTrue) /// .action(clap::ArgAction::SetTrue)
/// ); /// );
/// ///
/// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag", "--flag"]).unwrap(); /// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag"]).unwrap();
/// assert!(matches.contains_id("flag")); /// assert!(matches.contains_id("flag"));
/// assert_eq!( /// assert_eq!(
/// matches.get_flag("flag"), /// matches.get_flag("flag"),

View file

@ -1179,7 +1179,14 @@ impl<'cmd> Parser<'cmd> {
self.cur_idx.set(self.cur_idx.get() + 1); self.cur_idx.set(self.cur_idx.get() + 1);
debug!("Parser::react: cur_idx:={}", self.cur_idx.get()); debug!("Parser::react: cur_idx:={}", self.cur_idx.get());
} }
matcher.remove(&arg.id); if matcher.remove(&arg.id) && !self.cmd.is_args_override_self() {
return Err(ClapError::argument_conflict(
self.cmd,
arg.to_string(),
vec![arg.to_string()],
Usage::new(self.cmd).create_usage_with_title(&[]),
));
}
self.start_custom_arg(matcher, arg, source); self.start_custom_arg(matcher, arg, source);
self.push_arg_values(arg, raw_vals, matcher)?; self.push_arg_values(arg, raw_vals, matcher)?;
if cfg!(debug_assertions) && matcher.needs_more_vals(arg) { if cfg!(debug_assertions) && matcher.needs_more_vals(arg) {
@ -1213,7 +1220,14 @@ impl<'cmd> Parser<'cmd> {
raw_vals raw_vals
}; };
matcher.remove(&arg.id); if matcher.remove(&arg.id) && !self.cmd.is_args_override_self() {
return Err(ClapError::argument_conflict(
self.cmd,
arg.to_string(),
vec![arg.to_string()],
Usage::new(self.cmd).create_usage_with_title(&[]),
));
}
self.start_custom_arg(matcher, arg, source); self.start_custom_arg(matcher, arg, source);
self.push_arg_values(arg, raw_vals, matcher)?; self.push_arg_values(arg, raw_vals, matcher)?;
Ok(ParseResult::ValuesDone) Ok(ParseResult::ValuesDone)
@ -1225,7 +1239,14 @@ impl<'cmd> Parser<'cmd> {
raw_vals raw_vals
}; };
matcher.remove(&arg.id); if matcher.remove(&arg.id) && self.cmd.is_args_override_self() {
return Err(ClapError::argument_conflict(
self.cmd,
arg.to_string(),
vec![arg.to_string()],
Usage::new(self.cmd).create_usage_with_title(&[]),
));
}
self.start_custom_arg(matcher, arg, source); self.start_custom_arg(matcher, arg, source);
self.push_arg_values(arg, raw_vals, matcher)?; self.push_arg_values(arg, raw_vals, matcher)?;
Ok(ParseResult::ValuesDone) Ok(ParseResult::ValuesDone)

View file

@ -1,6 +1,7 @@
#![allow(clippy::bool_assert_comparison)] #![allow(clippy::bool_assert_comparison)]
use clap::builder::ArgPredicate; use clap::builder::ArgPredicate;
use clap::error::ErrorKind;
use clap::Arg; use clap::Arg;
use clap::ArgAction; use clap::ArgAction;
use clap::Command; use clap::Command;
@ -22,8 +23,15 @@ fn set() {
assert_eq!(matches.contains_id("mammal"), true); assert_eq!(matches.contains_id("mammal"), true);
assert_eq!(matches.index_of("mammal"), Some(2)); assert_eq!(matches.index_of("mammal"), Some(2));
let result = cmd
.clone()
.try_get_matches_from(["test", "--mammal", "dog", "--mammal", "cat"]);
let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
let matches = cmd let matches = cmd
.clone() .clone()
.args_override_self(true)
.try_get_matches_from(["test", "--mammal", "dog", "--mammal", "cat"]) .try_get_matches_from(["test", "--mammal", "dog", "--mammal", "cat"])
.unwrap(); .unwrap();
assert_eq!(matches.get_one::<String>("mammal").unwrap(), "cat"); assert_eq!(matches.get_one::<String>("mammal").unwrap(), "cat");
@ -88,8 +96,15 @@ fn set_true() {
assert_eq!(matches.contains_id("mammal"), true); assert_eq!(matches.contains_id("mammal"), true);
assert_eq!(matches.index_of("mammal"), Some(1)); assert_eq!(matches.index_of("mammal"), Some(1));
let result = cmd
.clone()
.try_get_matches_from(["test", "--mammal", "--mammal"]);
let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
let matches = cmd let matches = cmd
.clone() .clone()
.args_override_self(true)
.try_get_matches_from(["test", "--mammal", "--mammal"]) .try_get_matches_from(["test", "--mammal", "--mammal"])
.unwrap(); .unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true); assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), true);

View file

@ -986,65 +986,11 @@ fn built_in_subcommand_escaped() {
} }
} }
#[test]
fn aaos_flags() {
// flags
let cmd = Command::new("posix").arg(arg!(--flag "some flag").action(ArgAction::SetTrue));
let res = cmd.clone().try_get_matches_from(vec!["", "--flag"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("flag"));
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
let res = cmd.try_get_matches_from(vec!["", "--flag", "--flag", "--flag", "--flag"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("flag"));
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
}
#[test]
fn aaos_flags_mult() {
// flags with multiple
let cmd = Command::new("posix").arg(arg!(--flag "some flag").action(ArgAction::Count));
let res = cmd.clone().try_get_matches_from(vec!["", "--flag"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("flag"));
assert_eq!(*m.get_one::<u8>("flag").expect("defaulted by clap"), 1);
let res = cmd.try_get_matches_from(vec!["", "--flag", "--flag", "--flag", "--flag"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("flag"));
assert_eq!(*m.get_one::<u8>("flag").expect("defaulted by clap"), 4);
}
#[test]
fn aaos_opts() {
// opts
let res = Command::new("posix")
.arg(
arg!(--opt <val> "some option")
.required(true)
.action(ArgAction::Set),
)
.try_get_matches_from(vec!["", "--opt=some", "--opt=other"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(m.contains_id("opt"));
assert_eq!(
m.get_one::<String>("opt").map(|v| v.as_str()),
Some("other")
);
}
#[test] #[test]
fn aaos_opts_w_other_overrides() { fn aaos_opts_w_other_overrides() {
// opts with other overrides // opts with other overrides
let res = Command::new("posix") let res = Command::new("posix")
.args_override_self(true)
.arg(arg!(--opt <val> "some option").action(ArgAction::Set)) .arg(arg!(--opt <val> "some option").action(ArgAction::Set))
.arg( .arg(
arg!(--other <val> "some other option") arg!(--other <val> "some other option")
@ -1066,6 +1012,7 @@ fn aaos_opts_w_other_overrides() {
fn aaos_opts_w_other_overrides_rev() { fn aaos_opts_w_other_overrides_rev() {
// opts with other overrides, rev // opts with other overrides, rev
let res = Command::new("posix") let res = Command::new("posix")
.args_override_self(true)
.arg( .arg(
arg!(--opt <val> "some option") arg!(--opt <val> "some option")
.required(true) .required(true)
@ -1092,6 +1039,7 @@ fn aaos_opts_w_other_overrides_rev() {
fn aaos_opts_w_other_overrides_2() { fn aaos_opts_w_other_overrides_2() {
// opts with other overrides // opts with other overrides
let res = Command::new("posix") let res = Command::new("posix")
.args_override_self(true)
.arg( .arg(
arg!(--opt <val> "some option") arg!(--opt <val> "some option")
.overrides_with("other") .overrides_with("other")
@ -1113,6 +1061,7 @@ fn aaos_opts_w_other_overrides_2() {
fn aaos_opts_w_other_overrides_rev_2() { fn aaos_opts_w_other_overrides_rev_2() {
// opts with other overrides, rev // opts with other overrides, rev
let res = Command::new("posix") let res = Command::new("posix")
.args_override_self(true)
.arg( .arg(
arg!(--opt <val> "some option") arg!(--opt <val> "some option")
.required(true) .required(true)
@ -1259,6 +1208,7 @@ fn aaos_pos_mult() {
#[test] #[test]
fn aaos_option_use_delim_false() { fn aaos_option_use_delim_false() {
let m = Command::new("posix") let m = Command::new("posix")
.args_override_self(true)
.arg( .arg(
arg!(--opt <val> "some option") arg!(--opt <val> "some option")
.required(true) .required(true)

View file

@ -19,6 +19,7 @@ fn flag_using_short() {
#[test] #[test]
fn lots_o_flags_sep() { fn lots_o_flags_sep() {
let r = Command::new("opts") let r = Command::new("opts")
.args_override_self(true)
.arg(arg!(o: -o ... "some flag").action(ArgAction::SetTrue)) .arg(arg!(o: -o ... "some flag").action(ArgAction::SetTrue))
.try_get_matches_from(vec![ .try_get_matches_from(vec![
"", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
@ -53,6 +54,7 @@ fn lots_o_flags_sep() {
#[test] #[test]
fn lots_o_flags_combined() { fn lots_o_flags_combined() {
let r = Command::new("opts") let r = Command::new("opts")
.args_override_self(true)
.arg(arg!(o: -o ... "some flag").action(ArgAction::SetTrue)) .arg(arg!(o: -o ... "some flag").action(ArgAction::SetTrue))
.try_get_matches_from(vec![ .try_get_matches_from(vec![
"", "",

View file

@ -223,6 +223,7 @@ fn grouped_interleaved_positional_occurrences() {
#[test] #[test]
fn issue_2171() { fn issue_2171() {
let schema = Command::new("ripgrep#1701 reproducer") let schema = Command::new("ripgrep#1701 reproducer")
.args_override_self(true)
.arg( .arg(
Arg::new("pretty") Arg::new("pretty")
.short('p') .short('p')

View file

@ -3,6 +3,7 @@ use clap::{Arg, ArgAction, Command};
#[test] #[test]
fn indices_mult_opts() { fn indices_mult_opts() {
let m = Command::new("ind") let m = Command::new("ind")
.args_override_self(true)
.arg( .arg(
Arg::new("exclude") Arg::new("exclude")
.short('e') .short('e')
@ -32,6 +33,7 @@ fn indices_mult_opts() {
#[test] #[test]
fn index_mult_opts() { fn index_mult_opts() {
let m = Command::new("ind") let m = Command::new("ind")
.args_override_self(true)
.arg( .arg(
Arg::new("exclude") Arg::new("exclude")
.short('e') .short('e')
@ -55,6 +57,7 @@ fn index_mult_opts() {
#[test] #[test]
fn index_flag() { fn index_flag() {
let m = Command::new("ind") let m = Command::new("ind")
.args_override_self(true)
.arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue)) .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
.arg(Arg::new("include").short('i').action(ArgAction::SetTrue)) .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
.try_get_matches_from(vec!["ind", "-e", "-i"]) .try_get_matches_from(vec!["ind", "-e", "-i"])
@ -67,6 +70,7 @@ fn index_flag() {
#[test] #[test]
fn index_flags() { fn index_flags() {
let m = Command::new("ind") let m = Command::new("ind")
.args_override_self(true)
.arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue)) .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
.arg(Arg::new("include").short('i').action(ArgAction::SetTrue)) .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
.try_get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"]) .try_get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"])
@ -79,6 +83,7 @@ fn index_flags() {
#[test] #[test]
fn indices_mult_flags() { fn indices_mult_flags() {
let m = Command::new("ind") let m = Command::new("ind")
.args_override_self(true)
.arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue)) .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
.arg(Arg::new("include").short('i').action(ArgAction::SetTrue)) .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
.try_get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"]) .try_get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"])
@ -91,6 +96,7 @@ fn indices_mult_flags() {
#[test] #[test]
fn indices_mult_flags_combined() { fn indices_mult_flags_combined() {
let m = Command::new("ind") let m = Command::new("ind")
.args_override_self(true)
.arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue)) .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
.arg(Arg::new("include").short('i').action(ArgAction::SetTrue)) .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
.try_get_matches_from(vec!["ind", "-eieei"]) .try_get_matches_from(vec!["ind", "-eieei"])
@ -103,6 +109,7 @@ fn indices_mult_flags_combined() {
#[test] #[test]
fn indices_mult_flags_opt_combined() { fn indices_mult_flags_opt_combined() {
let m = Command::new("ind") let m = Command::new("ind")
.args_override_self(true)
.arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue)) .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
.arg(Arg::new("include").short('i').action(ArgAction::SetTrue)) .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
.arg(Arg::new("option").short('o').action(ArgAction::Set)) .arg(Arg::new("option").short('o').action(ArgAction::Set))
@ -117,6 +124,7 @@ fn indices_mult_flags_opt_combined() {
#[test] #[test]
fn indices_mult_flags_opt_combined_eq() { fn indices_mult_flags_opt_combined_eq() {
let m = Command::new("ind") let m = Command::new("ind")
.args_override_self(true)
.arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue)) .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
.arg(Arg::new("include").short('i').action(ArgAction::SetTrue)) .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
.arg(Arg::new("option").short('o').action(ArgAction::Set)) .arg(Arg::new("option").short('o').action(ArgAction::Set))
@ -131,6 +139,7 @@ fn indices_mult_flags_opt_combined_eq() {
#[test] #[test]
fn indices_mult_opt_value_delim_eq() { fn indices_mult_opt_value_delim_eq() {
let m = Command::new("myapp") let m = Command::new("myapp")
.args_override_self(true)
.arg( .arg(
Arg::new("option") Arg::new("option")
.short('o') .short('o')
@ -149,6 +158,7 @@ fn indices_mult_opt_value_delim_eq() {
#[test] #[test]
fn indices_mult_opt_value_no_delim_eq() { fn indices_mult_opt_value_no_delim_eq() {
let m = Command::new("myapp") let m = Command::new("myapp")
.args_override_self(true)
.arg( .arg(
Arg::new("option") Arg::new("option")
.short('o') .short('o')
@ -163,6 +173,7 @@ fn indices_mult_opt_value_no_delim_eq() {
#[test] #[test]
fn indices_mult_opt_mult_flag() { fn indices_mult_opt_mult_flag() {
let m = Command::new("myapp") let m = Command::new("myapp")
.args_override_self(true)
.arg(Arg::new("option").short('o').action(ArgAction::Append)) .arg(Arg::new("option").short('o').action(ArgAction::Append))
.arg(Arg::new("flag").short('f').action(ArgAction::SetTrue)) .arg(Arg::new("flag").short('f').action(ArgAction::SetTrue))
.try_get_matches_from(vec!["myapp", "-o", "val1", "-f", "-o", "val2", "-f"]) .try_get_matches_from(vec!["myapp", "-o", "val1", "-f", "-o", "val2", "-f"])

View file

@ -3,6 +3,7 @@ use clap::{arg, Arg, ArgAction, Command};
#[test] #[test]
fn multiple_occurrences_of_flags_long() { fn multiple_occurrences_of_flags_long() {
let m = Command::new("mo_flags_long") let m = Command::new("mo_flags_long")
.args_override_self(true)
.arg(arg!(--multflag "allowed multiple flag").action(ArgAction::SetTrue)) .arg(arg!(--multflag "allowed multiple flag").action(ArgAction::SetTrue))
.arg(arg!(--flag "disallowed multiple flag").action(ArgAction::SetTrue)) .arg(arg!(--flag "disallowed multiple flag").action(ArgAction::SetTrue))
.try_get_matches_from(vec!["", "--multflag", "--flag", "--multflag"]) .try_get_matches_from(vec!["", "--multflag", "--flag", "--multflag"])
@ -16,6 +17,7 @@ fn multiple_occurrences_of_flags_long() {
#[test] #[test]
fn multiple_occurrences_of_flags_short() { fn multiple_occurrences_of_flags_short() {
let m = Command::new("mo_flags_short") let m = Command::new("mo_flags_short")
.args_override_self(true)
.arg(arg!(-m --multflag "allowed multiple flag").action(ArgAction::SetTrue)) .arg(arg!(-m --multflag "allowed multiple flag").action(ArgAction::SetTrue))
.arg(arg!(-f --flag "disallowed multiple flag").action(ArgAction::SetTrue)) .arg(arg!(-f --flag "disallowed multiple flag").action(ArgAction::SetTrue))
.try_get_matches_from(vec!["", "-m", "-f", "-m"]) .try_get_matches_from(vec!["", "-m", "-f", "-m"])

View file

@ -401,7 +401,7 @@ fn option_max_more() {
#[test] #[test]
fn optional_value() { fn optional_value() {
let mut cmd = Command::new("test").arg( let mut cmd = Command::new("test").args_override_self(true).arg(
Arg::new("port") Arg::new("port")
.short('p') .short('p')
.value_name("NUM") .value_name("NUM")

View file

@ -21,6 +21,7 @@ use clap::Parser;
#[test] #[test]
fn bool_type_is_flag() { fn bool_type_is_flag() {
#[derive(Parser, PartialEq, Eq, Debug)] #[derive(Parser, PartialEq, Eq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short, long)] #[arg(short, long)]
alice: bool, alice: bool,

View file

@ -21,6 +21,7 @@ use clap::{Parser, Subcommand};
#[test] #[test]
fn required_option() { fn required_option() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short, long)] #[arg(short, long)]
arg: i32, arg: i32,
@ -47,6 +48,7 @@ fn required_option() {
#[test] #[test]
fn option_with_default() { fn option_with_default() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short, default_value = "42")] #[arg(short, default_value = "42")]
arg: i32, arg: i32,
@ -65,6 +67,7 @@ fn option_with_default() {
#[test] #[test]
fn option_with_raw_default() { fn option_with_raw_default() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short, default_value = "42")] #[arg(short, default_value = "42")]
arg: i32, arg: i32,
@ -94,6 +97,7 @@ fn option_from_str() {
} }
#[derive(Debug, Parser, PartialEq)] #[derive(Debug, Parser, PartialEq)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
a: Option<A>, a: Option<A>,
} }
@ -119,6 +123,7 @@ fn vec_from_str() {
} }
#[derive(Debug, Parser, PartialEq)] #[derive(Debug, Parser, PartialEq)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
a: Vec<A>, a: Vec<A>,
} }
@ -147,6 +152,7 @@ fn option_vec_from_str() {
} }
#[derive(Debug, Parser, PartialEq)] #[derive(Debug, Parser, PartialEq)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short)] #[arg(short)]
a: Option<Vec<A>>, a: Option<Vec<A>>,
@ -162,6 +168,7 @@ fn option_vec_from_str() {
#[test] #[test]
fn option_type_is_optional() { fn option_type_is_optional() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short)] #[arg(short)]
arg: Option<i32>, arg: Option<i32>,
@ -181,6 +188,7 @@ fn option_type_is_optional() {
fn required_with_option_type() { fn required_with_option_type() {
#[derive(Debug, PartialEq, Eq, Parser)] #[derive(Debug, PartialEq, Eq, Parser)]
#[command(subcommand_negates_reqs = true)] #[command(subcommand_negates_reqs = true)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(required = true)] #[arg(required = true)]
req_str: Option<String>, req_str: Option<String>,
@ -223,6 +231,7 @@ fn ignore_qualified_option_type() {
} }
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(value_parser = parser)] #[arg(value_parser = parser)]
arg: ::std::option::Option<String>, arg: ::std::option::Option<String>,
@ -239,6 +248,7 @@ fn ignore_qualified_option_type() {
#[test] #[test]
fn option_option_type_is_optional_value() { fn option_option_type_is_optional_value() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short)] #[arg(short)]
#[allow(clippy::option_option)] #[allow(clippy::option_option)]
@ -266,6 +276,7 @@ fn option_option_type_is_optional_value() {
#[test] #[test]
fn option_option_type_help() { fn option_option_type_help() {
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(long, value_name = "val")] #[arg(long, value_name = "val")]
arg: Option<Option<i32>>, arg: Option<Option<i32>>,
@ -278,6 +289,7 @@ fn option_option_type_help() {
#[test] #[test]
fn two_option_option_types() { fn two_option_option_types() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short)] #[arg(short)]
arg: Option<Option<i32>>, arg: Option<Option<i32>>,
@ -332,6 +344,7 @@ fn two_option_option_types() {
#[test] #[test]
fn vec_type_is_multiple_occurrences() { fn vec_type_is_multiple_occurrences() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short, long)] #[arg(short, long)]
arg: Vec<i32>, arg: Vec<i32>,
@ -350,6 +363,7 @@ fn vec_type_is_multiple_occurrences() {
#[test] #[test]
fn vec_type_with_required() { fn vec_type_with_required() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short, long, required = true)] #[arg(short, long, required = true)]
arg: Vec<i32>, arg: Vec<i32>,
@ -368,6 +382,7 @@ fn vec_type_with_required() {
#[test] #[test]
fn vec_type_with_multiple_values_only() { fn vec_type_with_multiple_values_only() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short, long, num_args(1..))] #[arg(short, long, num_args(1..))]
arg: Vec<i32>, arg: Vec<i32>,
@ -390,6 +405,7 @@ fn ignore_qualified_vec_type() {
} }
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(value_parser = parser)] #[arg(value_parser = parser)]
arg: ::std::vec::Vec<String>, arg: ::std::vec::Vec<String>,
@ -406,6 +422,7 @@ fn ignore_qualified_vec_type() {
#[test] #[test]
fn option_vec_type() { fn option_vec_type() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short)] #[arg(short)]
arg: Option<Vec<i32>>, arg: Option<Vec<i32>>,
@ -428,6 +445,7 @@ fn option_vec_type() {
#[test] #[test]
fn option_vec_type_structopt_behavior() { fn option_vec_type_structopt_behavior() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short, long, num_args(0..))] #[arg(short, long, num_args(0..))]
arg: Option<Vec<i32>>, arg: Option<Vec<i32>>,
@ -455,6 +473,7 @@ fn option_vec_type_structopt_behavior() {
#[test] #[test]
fn two_option_vec_types() { fn two_option_vec_types() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(short)] #[arg(short)]
arg: Option<Vec<i32>>, arg: Option<Vec<i32>>,
@ -496,6 +515,7 @@ fn two_option_vec_types() {
#[test] #[test]
fn explicit_value_parser() { fn explicit_value_parser() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(long, value_parser = clap::value_parser!(i32))] #[arg(long, value_parser = clap::value_parser!(i32))]
arg: i32, arg: i32,
@ -509,6 +529,7 @@ fn explicit_value_parser() {
#[test] #[test]
fn implicit_value_parser() { fn implicit_value_parser() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
#[command(args_override_self = true)]
struct Opt { struct Opt {
#[arg(long)] #[arg(long)]
arg: i32, arg: i32,