Skip to content

Commit

Permalink
fix(parser): SetFalse should also not allow self-override
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 28, 2022
1 parent bf42ff0 commit 2d78749
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
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
2 changes: 1 addition & 1 deletion src/parser/parser.rs
Expand Up @@ -1240,7 +1240,7 @@ 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() {
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

0 comments on commit 2d78749

Please sign in to comment.