Merge pull request #5549 from shannmu/hidden_aliases

feat(clap_complete): Add support for visible subcommand aliases
This commit is contained in:
Ed Page 2024-07-17 16:20:17 -05:00 committed by GitHub
commit 4a8d6806d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 2 deletions

View file

@ -334,8 +334,11 @@ fn possible_values(a: &clap::Arg) -> Option<Vec<clap::builder::PossibleValue>> {
fn subcommands(p: &clap::Command) -> Vec<(String, Option<StyledStr>)> {
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()
}

View file

@ -30,6 +30,36 @@ help Print 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-moon-foo
hello-world
hello-world-foo"
],
);
}
#[test]
fn suggest_long_flag_subset() {
let mut cmd = Command::new("exhaustive")