Skip to content

Commit

Permalink
fix: Make long subcommand flag inference consistent
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
  • Loading branch information
Alex Saveau committed Sep 25, 2023
1 parent c2b8ec3 commit a76789e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
37 changes: 19 additions & 18 deletions clap_builder/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions tests/builder/app_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit a76789e

Please sign in to comment.