Merge pull request #5025 from SUPERCILEX/resolve-alias-conflicts

fix: Resolve conflicting name inference if from aliases
This commit is contained in:
Ed Page 2023-09-25 15:56:28 -05:00 committed by GitHub
commit 3ac44040ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 30 deletions

View file

@ -544,19 +544,24 @@ impl<'cmd> Parser<'cmd> {
if self.cmd.is_infer_subcommands_set() { if self.cmd.is_infer_subcommands_set() {
// For subcommand `test`, we accepts it's prefix: `t`, `te`, // For subcommand `test`, we accepts it's prefix: `t`, `te`,
// `tes` and `test`. // `tes` and `test`.
let v = self let mut iter = self.cmd.get_subcommands().filter_map(|s| {
.cmd if s.get_name().starts_with(arg) {
.all_subcommand_names() return Some(s.get_name());
.filter(|s| s.starts_with(arg)) }
.collect::<Vec<_>>();
if v.len() == 1 { // Use find here instead of chaining the iterator because we want to accept
return Some(v[0]); // conflicts in aliases.
s.get_all_aliases().find(|s| s.starts_with(arg))
});
if let name @ Some(_) = iter.next() {
if iter.next().is_none() {
return name;
}
} }
// If there is any ambiguity, fallback to non-infer subcommand
// search.
} }
// 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) { if let Some(sc) = self.cmd.find_subcommand(arg) {
return Some(sc.get_name()); return Some(sc.get_name());
} }
@ -568,28 +573,29 @@ impl<'cmd> Parser<'cmd> {
fn possible_long_flag_subcommand(&self, arg: &str) -> Option<&str> { fn possible_long_flag_subcommand(&self, arg: &str) -> Option<&str> {
debug!("Parser::possible_long_flag_subcommand: arg={arg:?}"); debug!("Parser::possible_long_flag_subcommand: arg={arg:?}");
if self.cmd.is_infer_subcommands_set() { if self.cmd.is_infer_subcommands_set() {
let options = self let mut iter = self.cmd.get_subcommands().filter_map(|sc| {
.cmd sc.get_long_flag().and_then(|long| {
.get_subcommands() if long.starts_with(arg) {
.fold(Vec::new(), |mut options, sc| { Some(sc.get_name())
if let Some(long) = sc.get_long_flag() { } else {
if long.starts_with(arg) { sc.get_all_long_flag_aliases().find_map(|alias| {
options.push(long); if alias.starts_with(arg) {
} Some(sc.get_name())
options.extend(sc.get_all_aliases().filter(|alias| alias.starts_with(arg))) } else {
None
}
})
} }
options })
}); });
if options.len() == 1 {
return Some(options[0]);
}
for sc in options { if let name @ Some(_) = iter.next() {
if sc == arg { if iter.next().is_none() {
return Some(sc); 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); return Some(sc_name);
} }
None None

View file

@ -1,9 +1,9 @@
use std::ffi::OsString; use std::ffi::OsString;
use super::utils;
use clap::{arg, error::ErrorKind, Arg, ArgAction, Command}; use clap::{arg, error::ErrorKind, Arg, ArgAction, Command};
use super::utils;
static ALLOW_EXT_SC: &str = "\ static ALLOW_EXT_SC: &str = "\
Usage: clap-test [COMMAND] Usage: clap-test [COMMAND]
@ -253,6 +253,51 @@ fn infer_subcommands_pass_exact_match() {
assert_eq!(m.subcommand_name(), Some("test")); 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"));
}
#[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")] #[cfg(feature = "suggestions")]
#[test] #[test]
fn infer_subcommands_fail_suggestions() { fn infer_subcommands_fail_suggestions() {