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: Resolve conflicting name inference if from aliases #5025

Merged
merged 3 commits into from Sep 25, 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
62 changes: 34 additions & 28 deletions clap_builder/src/parser/parser.rs
Expand Up @@ -544,19 +544,24 @@ impl<'cmd> Parser<'cmd> {
if self.cmd.is_infer_subcommands_set() {
// For subcommand `test`, we accepts it's prefix: `t`, `te`,
// `tes` and `test`.
let v = self
.cmd
.all_subcommand_names()
.filter(|s| s.starts_with(arg))
.collect::<Vec<_>>();
let mut iter = self.cmd.get_subcommands().filter_map(|s| {
if s.get_name().starts_with(arg) {
return Some(s.get_name());
}

if v.len() == 1 {
return Some(v[0]);
}
// Use find here instead of chaining the iterator because we want to accept
// conflicts in aliases.
s.get_all_aliases().find(|s| s.starts_with(arg))
});

// If there is any ambiguity, fallback to non-infer subcommand
// search.
if let name @ Some(_) = iter.next() {
if iter.next().is_none() {
return name;
}
}
}
// Don't use an else here because we want inference to support exact matching even if
// there are conflicts.
if let Some(sc) = self.cmd.find_subcommand(arg) {
return Some(sc.get_name());
}
Expand All @@ -568,28 +573,29 @@ impl<'cmd> Parser<'cmd> {
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
.cmd
.get_subcommands()
.fold(Vec::new(), |mut options, sc| {
if let Some(long) = sc.get_long_flag() {
if long.starts_with(arg) {
options.push(long);
}
options.extend(sc.get_all_aliases().filter(|alias| alias.starts_with(arg)))
let mut iter = self.cmd.get_subcommands().filter_map(|sc| {
sc.get_long_flag().and_then(|long| {
if long.starts_with(arg) {
Some(sc.get_name())
} else {
sc.get_all_long_flag_aliases().find_map(|alias| {
if alias.starts_with(arg) {
Some(sc.get_name())
} else {
None
}
})
}
options
});
if options.len() == 1 {
return Some(options[0]);
}
})
});

for sc in options {
if sc == arg {
return Some(sc);
if let name @ Some(_) = iter.next() {
if iter.next().is_none() {
return name;
}
}
} else if let Some(sc_name) = self.cmd.find_long_subcmd(arg) {
}
if let Some(sc_name) = self.cmd.find_long_subcmd(arg) {
return Some(sc_name);
}
None
Expand Down
49 changes: 47 additions & 2 deletions tests/builder/app_settings.rs
@@ -1,9 +1,9 @@
use std::ffi::OsString;

use super::utils;

use clap::{arg, error::ErrorKind, Arg, ArgAction, Command};

use super::utils;

static ALLOW_EXT_SC: &str = "\
Usage: clap-test [COMMAND]

Expand Down Expand Up @@ -253,6 +253,51 @@ fn infer_subcommands_pass_exact_match() {
assert_eq!(m.subcommand_name(), Some("test"));
}

#[test]
fn infer_subcommands_pass_conflicting_aliases() {
let m = Command::new("prog")
.infer_subcommands(true)
.subcommand(Command::new("test").aliases(["testa", "t", "testb"]))
.try_get_matches_from(vec!["prog", "te"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}

SUPERCILEX marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn infer_long_flag_pass_conflicting_aliases() {
let m = Command::new("prog")
.infer_subcommands(true)
.subcommand(
Command::new("c")
.long_flag("test")
.long_flag_aliases(["testa", "t", "testb"]),
)
.try_get_matches_from(vec!["prog", "--te"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("c"));
}

#[test]
fn infer_long_flag() {
let m = Command::new("prog")
.infer_subcommands(true)
.subcommand(Command::new("test").long_flag("testa"))
.try_get_matches_from(vec!["prog", "--te"])
.unwrap();
assert_eq!(m.subcommand_name(), Some("test"));
}

#[test]
fn infer_subcommands_long_flag_fail_with_args2() {
let m = Command::new("prog")
.infer_subcommands(true)
.subcommand(Command::new("a").long_flag("test"))
.subcommand(Command::new("b").long_flag("temp"))
.try_get_matches_from(vec!["prog", "--te"]);
assert!(m.is_err(), "{:#?}", m.unwrap());
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnknownArgument);
}

#[cfg(feature = "suggestions")]
#[test]
fn infer_subcommands_fail_suggestions() {
Expand Down