Skip to content

Commit

Permalink
Merge pull request #5584 from shannmu/hidden_subcommands_and_aliases
Browse files Browse the repository at this point in the history
feat(clap_complete): Support hiding subcommands and their aliases
  • Loading branch information
epage authored Jul 19, 2024
2 parents 8710057 + d68c91a commit 93f0c48
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 17 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_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_aliases().into_iter().map(|s| {
CompletionCandidate::new(s.to_string())
.help(sc.get_about().cloned())
.visible(false)
}))
})
.collect()
}
Expand Down
38 changes: 38 additions & 0 deletions clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,44 @@ fn suggest_hidden_long_flags() {
)
}

#[test]
fn suggest_hidden_subcommand_and_aliases() {
let mut cmd = Command::new("exhaustive")
.subcommand(
Command::new("test_visible")
.visible_alias("test_visible-alias_visible")
.alias("test_visible-alias_hidden"),
)
.subcommand(
Command::new("test_hidden")
.visible_alias("test_hidden-alias_visible")
.alias("test_hidden-alias_hidden")
.hide(true),
);

assert_data_eq!(
complete!(cmd, "test"),
snapbox::str![
"test_visible
test_visible-alias_visible"
]
);

assert_data_eq!(
complete!(cmd, "test_h"),
snapbox::str![
"test_hidden
test_hidden-alias_hidden
test_hidden-alias_visible"
]
);

assert_data_eq!(
complete!(cmd, "test_hidden-alias_h"),
snapbox::str!["test_hidden-alias_hidden"]
)
}

#[test]
fn suggest_subcommand_aliases() {
let mut cmd = Command::new("exhaustive")
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 93f0c48

Please sign in to comment.