fix: Detect deeply recursed global args

Fixes #3428
This commit is contained in:
Ed Page 2022-02-09 15:40:11 -06:00
parent 82dfc7d5ea
commit 81092b5aed
2 changed files with 24 additions and 8 deletions

View file

@ -2704,20 +2704,16 @@ impl<'help> App<'help> {
// Internally used only
impl<'help> App<'help> {
fn get_used_global_args(&self, matcher: &ArgMatcher, global_arg_vec: &mut Vec<Id>) {
fn get_used_global_args(&self, matches: &ArgMatches, global_arg_vec: &mut Vec<Id>) {
global_arg_vec.extend(
self.args
.args()
.filter(|a| a.get_global())
.map(|ga| ga.id.clone()),
);
if let Some(used_subcommand) = matcher.subcommand.as_ref() {
if let Some(used_subcommand) = self
.subcommands
.iter()
.find(|subcommand| subcommand.id == used_subcommand.id)
{
return used_subcommand.get_used_global_args(matcher, global_arg_vec);
if let Some((id, matches)) = matches.subcommand() {
if let Some(used_sub) = self.find_subcommand(id) {
used_sub.get_used_global_args(matches, global_arg_vec);
}
}
}

View file

@ -88,3 +88,23 @@ fn global_arg_available_in_subcommand() {
assert!(m.is_present("global"));
assert!(m.subcommand_matches("ping").unwrap().is_present("global"));
}
#[test]
fn deeply_nested_discovery() {
let app = App::new("a").arg(arg!(--"long-a").global(true)).subcommand(
App::new("b").arg(arg!(--"long-b").global(true)).subcommand(
App::new("c")
.arg(arg!(--"long-c").global(true))
.subcommand(App::new("d")),
),
);
let m = app
.try_get_matches_from(["a", "b", "c", "d", "--long-a", "--long-b", "--long-c"])
.unwrap();
assert!(m.is_present("long-a"));
let m = m.subcommand_matches("b").unwrap();
assert!(m.is_present("long-b"));
let m = m.subcommand_matches("c").unwrap();
assert!(m.is_present("long-c"));
}