Skip to content

Commit

Permalink
Merge pull request #5549 from shannmu/hidden_aliases
Browse files Browse the repository at this point in the history
feat(clap_complete): Add support for visible subcommand aliases
  • Loading branch information
epage authored Jul 17, 2024
2 parents beeffe8 + 152b2e8 commit 4a8d680
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
7 changes: 5 additions & 2 deletions clap_complete/src/dynamic/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
30 changes: 30 additions & 0 deletions clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 4a8d680

Please sign in to comment.