Skip to content

Commit

Permalink
feat(clap_complete): Support hiding subcommands and their aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
shannmu committed Jul 19, 2024
1 parent 6a64c24 commit 1116f7a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
9 changes: 9 additions & 0 deletions clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3553,6 +3553,15 @@ impl Command {
self.long_flag_aliases.iter().map(|a| a.0.as_str())
}

/// Iterate through the *hidden* aliases for this subcommand.
#[inline]
pub fn get_hidden_aliases(&self) -> impl Iterator<Item = &str> + '_ {
self.aliases
.iter()
.filter(|(_, vis)| !*vis)
.map(|a| a.0.as_str())
}

#[inline]
pub(crate) fn is_set(&self, s: AppSettings) -> bool {
self.settings.is_set(s) || self.g_settings.is_set(s)
Expand Down
17 changes: 12 additions & 5 deletions clap_complete/src/dynamic/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,18 @@ fn subcommands(p: &clap::Command) -> Vec<CompletionCandidate> {
debug!("subcommands: Has subcommands...{:?}", p.has_subcommands());
p.get_subcommands()
.flat_map(|sc| {
sc.get_name_and_visible_aliases().into_iter().map(|s| {
CompletionCandidate::new(s.to_string())
.help(sc.get_about().cloned())
.visible(true)
})
sc.get_name_and_visible_aliases()
.into_iter()
.map(|s| {
CompletionCandidate::new(s.to_string())
.help(sc.get_about().cloned())
.visible(!sc.is_hide_set())
})
.chain(sc.get_hidden_aliases().into_iter().map(|s| {
CompletionCandidate::new(s.to_string())
.help(sc.get_about().cloned())
.visible(false)
}))
})
.collect()
}
Expand Down
7 changes: 3 additions & 4 deletions clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ fn suggest_hidden_subcommand_and_aliases() {
assert_data_eq!(
complete!(cmd, "hello_"),
snapbox::str![
"hello_world_hidden
hello_world_hidden-alias
hello_world_visible
"hello_world_visible
hello_world_visible-alias
"
]
Expand All @@ -84,7 +82,8 @@ hello_world_visible-alias
complete!(cmd, "hello_world_h"),
snapbox::str![
"hello_world_hidden
hello_world_hidden-alias"
hello_world_hidden-alias
hello_world_hidden-hidden-alias"
]
);
}
Expand Down
6 changes: 3 additions & 3 deletions clap_complete/tests/testsuite/elvish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ fn complete_dynamic() {
let expected = snapbox::str![
r#"% exhaustive --generate
COMPLETING argument
--generate --help -V action complete hint pacman value
--global --version -h alias help last quote "#
--generate --help -V action help last quote
--global --version -h alias hint pacman value"#
];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand All @@ -213,4 +213,4 @@ fn complete_dynamic() {
];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
}
}
10 changes: 5 additions & 5 deletions clap_complete/tests/testsuite/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ fn complete_dynamic() {
let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
% exhaustive action
action last -V (Print version)
alias pacman --generate (generate)
complete (Register shell completions for this program) quote --global (everywhere)
help (Print this message or the help of the given subcommand(s)) value --help (Print help)
hint -h (Print help) --version (Print version)
action pacman --generate (generate)
alias quote --global (everywhere)
help (Print this message or the help of the given subcommand(s)) value --help (Print help)
hint -h (Print help) --version (Print version)
last -V (Print version)
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand Down
7 changes: 3 additions & 4 deletions clap_complete/tests/testsuite/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ pacman action alias value quote hint last --
assert_data_eq!(actual, expected);
}


#[cfg(all(unix, feature = "unstable-dynamic"))]
#[test]
fn register_dynamic() {
Expand All @@ -184,8 +183,8 @@ fn complete_dynamic() {
let input = "exhaustive \t\t";
let expected = snapbox::str![
r#"% exhaustive
--generate --help -V action complete hint pacman value
--global --version -h alias help last quote "#
--generate --help -V action help last quote
--global --version -h alias hint pacman value "#
];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand All @@ -200,4 +199,4 @@ fn complete_dynamic() {
];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
}
}

0 comments on commit 1116f7a

Please sign in to comment.