mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
parent
82dfc7d5ea
commit
81092b5aed
2 changed files with 24 additions and 8 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue