Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): Allow one-off self-overrides #4281

Merged
merged 2 commits into from Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/builder/action.rs
Expand Up @@ -169,7 +169,7 @@ pub enum ArgAction {
/// .action(clap::ArgAction::SetFalse)
/// );
///
/// 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_eq!(
/// matches.get_one::<bool>("flag").copied(),
Expand Down
5 changes: 0 additions & 5 deletions src/builder/debug_asserts.rs
Expand Up @@ -686,11 +686,6 @@ fn assert_arg(arg: &Arg) {
"Argument '{}' cannot conflict with itself",
arg.get_id(),
);
assert!(
!arg.overrides.iter().any(|x| *x == arg.id),
"Argument '{}' cannot override itself, its the default",
arg.get_id(),
);

assert_eq!(
arg.get_action().takes_values(),
Expand Down
12 changes: 9 additions & 3 deletions src/parser/parser.rs
Expand Up @@ -1180,7 +1180,9 @@ impl<'cmd> Parser<'cmd> {
self.cur_idx.set(self.cur_idx.get() + 1);
debug!("Parser::react: cur_idx:={}", self.cur_idx.get());
}
if matcher.remove(&arg.id) && !self.cmd.is_args_override_self() {
if matcher.remove(&arg.id)
&& !(self.cmd.is_args_override_self() || arg.overrides.contains(arg.get_id()))
{
return Err(ClapError::argument_conflict(
self.cmd,
arg.to_string(),
Expand Down Expand Up @@ -1221,7 +1223,9 @@ impl<'cmd> Parser<'cmd> {
raw_vals
};

if matcher.remove(&arg.id) && !self.cmd.is_args_override_self() {
if matcher.remove(&arg.id)
&& !(self.cmd.is_args_override_self() || arg.overrides.contains(arg.get_id()))
{
return Err(ClapError::argument_conflict(
self.cmd,
arg.to_string(),
Expand All @@ -1240,7 +1244,9 @@ impl<'cmd> Parser<'cmd> {
raw_vals
};

if matcher.remove(&arg.id) && self.cmd.is_args_override_self() {
if matcher.remove(&arg.id)
&& !(self.cmd.is_args_override_self() || arg.overrides.contains(arg.get_id()))
{
return Err(ClapError::argument_conflict(
self.cmd,
arg.to_string(),
Expand Down
7 changes: 7 additions & 0 deletions tests/builder/action.rs
Expand Up @@ -240,8 +240,15 @@ fn set_false() {
assert_eq!(matches.contains_id("mammal"), true);
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
.clone()
.args_override_self(true)
.try_get_matches_from(["test", "--mammal", "--mammal"])
.unwrap();
assert_eq!(*matches.get_one::<bool>("mammal").unwrap(), false);
Expand Down
26 changes: 23 additions & 3 deletions tests/builder/posix_compatible.rs
@@ -1,16 +1,36 @@
use clap::{arg, error::ErrorKind, Arg, ArgAction, Command};

#[test]
#[should_panic = "Argument 'flag' cannot override itself"]
fn flag_overrides_itself() {
Command::new("posix")
let res = Command::new("posix")
.arg(
arg!(--flag "some flag"
)
.action(ArgAction::SetTrue)
.overrides_with("flag"),
)
.build();
.try_get_matches_from(vec!["", "--flag", "--flag"]);
assert!(res.is_ok(), "{}", res.unwrap_err());
let m = res.unwrap();
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
}

#[test]
fn option_overrides_itself() {
let res = Command::new("posix")
.arg(
arg!(--opt <val> "some option")
.required(false)
.overrides_with("opt"),
)
.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]
Expand Down