From c41d41cbdc978e459ade139b6e0b57c13ec0719b Mon Sep 17 00:00:00 2001 From: shanmu Date: Mon, 1 Jul 2024 01:24:19 +0800 Subject: [PATCH 1/2] test(clap_complete): Add test case for visible subcommand aliases --- clap_complete/tests/testsuite/dynamic.rs | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/clap_complete/tests/testsuite/dynamic.rs b/clap_complete/tests/testsuite/dynamic.rs index 7cf9cf7d693..a938a658c2e 100644 --- a/clap_complete/tests/testsuite/dynamic.rs +++ b/clap_complete/tests/testsuite/dynamic.rs @@ -33,6 +33,34 @@ help\tPrint this message or the help of the given subcommand(s)" ); } +#[test] +fn suggest_subcommand_aliases() { + let mut cmd = Command::new("exhaustive") + .subcommand( + Command::new("hello-world") + .visible_alias("hello-world-foo") + .alias("hidden-world"), + ) + .subcommand( + Command::new("hello-moon") + .visible_alias("hello-moon-foo") + .alias("hidden-moon"), + ) + .subcommand( + Command::new("goodbye-world") + .visible_alias("goodbye-world-foo") + .alias("hidden-goodbye"), + ); + + assert_data_eq!( + complete!(cmd, "hello"), + snapbox::str![ + "hello-moon +hello-world" + ], + ); +} + #[test] fn suggest_long_flag_subset() { let mut cmd = Command::new("exhaustive") From 152b2e8a5532f5c8701927c973e736a8c5cb7c6b Mon Sep 17 00:00:00 2001 From: shanmu Date: Mon, 1 Jul 2024 00:50:32 +0800 Subject: [PATCH 2/2] feat(clap_complete): Add support for visible subcommand aliases --- clap_complete/src/dynamic/completer.rs | 7 +++++-- clap_complete/tests/testsuite/dynamic.rs | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/clap_complete/src/dynamic/completer.rs b/clap_complete/src/dynamic/completer.rs index 8bf121ea1f4..4c6d9c92fe9 100644 --- a/clap_complete/src/dynamic/completer.rs +++ b/clap_complete/src/dynamic/completer.rs @@ -334,8 +334,11 @@ fn possible_values(a: &clap::Arg) -> Option> { fn subcommands(p: &clap::Command) -> Vec<(String, Option)> { debug!("subcommands: name={}", p.get_name()); debug!("subcommands: Has subcommands...{:?}", p.has_subcommands()); - p.get_subcommands() - .map(|sc| (sc.get_name().to_string(), sc.get_about().cloned())) + .flat_map(|sc| { + sc.get_name_and_visible_aliases() + .into_iter() + .map(|s| (s.to_string(), sc.get_about().cloned())) + }) .collect() } diff --git a/clap_complete/tests/testsuite/dynamic.rs b/clap_complete/tests/testsuite/dynamic.rs index a938a658c2e..207e03ae775 100644 --- a/clap_complete/tests/testsuite/dynamic.rs +++ b/clap_complete/tests/testsuite/dynamic.rs @@ -56,7 +56,9 @@ fn suggest_subcommand_aliases() { complete!(cmd, "hello"), snapbox::str![ "hello-moon -hello-world" +hello-moon-foo +hello-world +hello-world-foo" ], ); }