mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 14:54:15 +00:00
fix: Make long subcommand flag inference consistent
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
This commit is contained in:
parent
c2b8ec3bd3
commit
a76789eb8b
2 changed files with 22 additions and 22 deletions
|
@ -573,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
|
||||
|
|
|
@ -264,7 +264,6 @@ fn infer_subcommands_pass_conflicting_aliases() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "internal error")]
|
||||
fn infer_long_flag_pass_conflicting_aliases() {
|
||||
let m = Command::new("prog")
|
||||
.infer_subcommands(true)
|
||||
|
@ -273,12 +272,12 @@ fn infer_long_flag_pass_conflicting_aliases() {
|
|||
.long_flag("test")
|
||||
.long_flag_aliases(["testa", "t", "testb"]),
|
||||
)
|
||||
.try_get_matches_from(vec!["prog", "--te"]);
|
||||
assert!(m.is_err(), "{:#?}", m.unwrap());
|
||||
.try_get_matches_from(vec!["prog", "--te"])
|
||||
.unwrap();
|
||||
assert_eq!(m.subcommand_name(), Some("c"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "internal error")]
|
||||
fn infer_long_flag() {
|
||||
let m = Command::new("prog")
|
||||
.infer_subcommands(true)
|
||||
|
|
Loading…
Reference in a new issue