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: Broken long arg inference on conflicts #4971

Merged
merged 1 commit into from Jun 23, 2023
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
6 changes: 4 additions & 2 deletions clap_builder/src/parser/parser.rs
Expand Up @@ -729,7 +729,7 @@ impl<'cmd> Parser<'cmd> {
debug!("Parser::parse_long_arg: Found valid arg or flag '{arg}'");
Some((long_arg, arg))
} else if self.cmd.is_infer_long_args_set() {
self.cmd.get_arguments().find_map(|a| {
let mut iter = self.cmd.get_arguments().filter_map(|a| {
if let Some(long) = a.get_long() {
if long.starts_with(long_arg) {
return Some((long, a));
Expand All @@ -738,7 +738,9 @@ impl<'cmd> Parser<'cmd> {
a.aliases
.iter()
.find_map(|(alias, _)| alias.starts_with(long_arg).then(|| (alias.as_str(), a)))
})
});

iter.next().filter(|_| iter.next().is_none())
} else {
None
};
Expand Down
56 changes: 55 additions & 1 deletion tests/builder/opts.rs
Expand Up @@ -659,7 +659,7 @@ fn issue_2279() {
}

#[test]
fn infer_long_arg() {
fn infer_long_arg_pass() {
let cmd = Command::new("test")
.infer_long_args(true)
.arg(
Expand Down Expand Up @@ -716,3 +716,57 @@ fn infer_long_arg() {
let matches = cmd.clone().try_get_matches_from(["test", "--a"]).unwrap();
assert!(*matches.get_one::<bool>("arg").expect("defaulted by clap"));
}

#[test]
fn infer_long_arg_pass_conflicts_exact_match() {
let cmd = Command::new("test")
.infer_long_args(true)
.arg(Arg::new("arg").long("arg").action(ArgAction::SetTrue))
.arg(Arg::new("arg2").long("arg2").action(ArgAction::SetTrue));

let matches = cmd.clone().try_get_matches_from(["test", "--arg"]).unwrap();
assert!(*matches.get_one::<bool>("arg").expect("defaulted by clap"));

let matches = cmd
.clone()
.try_get_matches_from(["test", "--arg2"])
.unwrap();
assert!(*matches.get_one::<bool>("arg2").expect("defaulted by clap"));
}

#[test]
fn infer_long_arg_pass_conflicting_aliases() {
let cmd = Command::new("test").infer_long_args(true).arg(
Arg::new("abc-123")
.long("abc-123")
.aliases(["a", "abc-xyz"])
.action(ArgAction::SetTrue),
);

let matches = cmd.clone().try_get_matches_from(["test", "--ab"]).unwrap();
assert!(*matches
.get_one::<bool>("abc-123")
.expect("defaulted by clap"));
}

#[test]
fn infer_long_arg_fail_conflicts() {
let cmd = Command::new("test")
.infer_long_args(true)
.arg(
Arg::new("abc-123")
.long("abc-123")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("abc-xyz")
.long("abc-xyz")
.action(ArgAction::SetTrue),
);

let error = cmd
.clone()
.try_get_matches_from(["test", "--abc"])
.unwrap_err();
assert_eq!(error.kind(), ErrorKind::UnknownArgument);
}