Skip to content

Commit 93f0c48

Browse files
authored
Merge pull request #5584 from shannmu/hidden_subcommands_and_aliases
feat(clap_complete): Support hiding subcommands and their aliases
2 parents 8710057 + d68c91a commit 93f0c48

File tree

6 files changed

+70
-17
lines changed

6 files changed

+70
-17
lines changed

clap_builder/src/builder/command.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,6 +3553,15 @@ impl Command {
35533553
self.long_flag_aliases.iter().map(|a| a.0.as_str())
35543554
}
35553555

3556+
/// Iterate through the *hidden* aliases for this subcommand.
3557+
#[inline]
3558+
pub fn get_aliases(&self) -> impl Iterator<Item = &str> + '_ {
3559+
self.aliases
3560+
.iter()
3561+
.filter(|(_, vis)| !*vis)
3562+
.map(|a| a.0.as_str())
3563+
}
3564+
35563565
#[inline]
35573566
pub(crate) fn is_set(&self, s: AppSettings) -> bool {
35583567
self.settings.is_set(s) || self.g_settings.is_set(s)

clap_complete/src/dynamic/completer.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,18 @@ fn subcommands(p: &clap::Command) -> Vec<CompletionCandidate> {
386386
debug!("subcommands: Has subcommands...{:?}", p.has_subcommands());
387387
p.get_subcommands()
388388
.flat_map(|sc| {
389-
sc.get_name_and_visible_aliases().into_iter().map(|s| {
390-
CompletionCandidate::new(s.to_string())
391-
.help(sc.get_about().cloned())
392-
.visible(true)
393-
})
389+
sc.get_name_and_visible_aliases()
390+
.into_iter()
391+
.map(|s| {
392+
CompletionCandidate::new(s.to_string())
393+
.help(sc.get_about().cloned())
394+
.visible(!sc.is_hide_set())
395+
})
396+
.chain(sc.get_aliases().into_iter().map(|s| {
397+
CompletionCandidate::new(s.to_string())
398+
.help(sc.get_about().cloned())
399+
.visible(false)
400+
}))
394401
})
395402
.collect()
396403
}

clap_complete/tests/testsuite/dynamic.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,44 @@ fn suggest_hidden_long_flags() {
5454
)
5555
}
5656

57+
#[test]
58+
fn suggest_hidden_subcommand_and_aliases() {
59+
let mut cmd = Command::new("exhaustive")
60+
.subcommand(
61+
Command::new("test_visible")
62+
.visible_alias("test_visible-alias_visible")
63+
.alias("test_visible-alias_hidden"),
64+
)
65+
.subcommand(
66+
Command::new("test_hidden")
67+
.visible_alias("test_hidden-alias_visible")
68+
.alias("test_hidden-alias_hidden")
69+
.hide(true),
70+
);
71+
72+
assert_data_eq!(
73+
complete!(cmd, "test"),
74+
snapbox::str![
75+
"test_visible
76+
test_visible-alias_visible"
77+
]
78+
);
79+
80+
assert_data_eq!(
81+
complete!(cmd, "test_h"),
82+
snapbox::str![
83+
"test_hidden
84+
test_hidden-alias_hidden
85+
test_hidden-alias_visible"
86+
]
87+
);
88+
89+
assert_data_eq!(
90+
complete!(cmd, "test_hidden-alias_h"),
91+
snapbox::str!["test_hidden-alias_hidden"]
92+
)
93+
}
94+
5795
#[test]
5896
fn suggest_subcommand_aliases() {
5997
let mut cmd = Command::new("exhaustive")

clap_complete/tests/testsuite/elvish.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ fn complete_dynamic() {
196196
let expected = snapbox::str![
197197
r#"% exhaustive --generate
198198
COMPLETING argument
199-
--generate --help -V action complete hint pacman value
200-
--global --version -h alias help last quote "#
199+
--generate --help -V action help last quote
200+
--global --version -h alias hint pacman value"#
201201
];
202202
let actual = runtime.complete(input, &term).unwrap();
203203
assert_data_eq!(actual, expected);
@@ -213,4 +213,4 @@ fn complete_dynamic() {
213213
];
214214
let actual = runtime.complete(input, &term).unwrap();
215215
assert_data_eq!(actual, expected);
216-
}
216+
}

clap_complete/tests/testsuite/fish.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ fn complete_dynamic() {
185185
let input = "exhaustive \t\t";
186186
let expected = snapbox::str![[r#"
187187
% exhaustive action
188-
action last -V (Print version)
189-
alias pacman --generate (generate)
190-
complete (Register shell completions for this program) quote --global (everywhere)
191-
help (Print this message or the help of the given subcommand(s)) value --help (Print help)
192-
hint -h (Print help) --version (Print version)
188+
action pacman --generate (generate)
189+
alias quote --global (everywhere)
190+
help (Print this message or the help of the given subcommand(s)) value --help (Print help)
191+
hint -h (Print help) --version (Print version)
192+
last -V (Print version)
193193
"#]];
194194
let actual = runtime.complete(input, &term).unwrap();
195195
assert_data_eq!(actual, expected);

clap_complete/tests/testsuite/zsh.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ pacman action alias value quote hint last --
163163
assert_data_eq!(actual, expected);
164164
}
165165

166-
167166
#[cfg(all(unix, feature = "unstable-dynamic"))]
168167
#[test]
169168
fn register_dynamic() {
@@ -184,8 +183,8 @@ fn complete_dynamic() {
184183
let input = "exhaustive \t\t";
185184
let expected = snapbox::str![
186185
r#"% exhaustive
187-
--generate --help -V action complete hint pacman value
188-
--global --version -h alias help last quote "#
186+
--generate --help -V action help last quote
187+
--global --version -h alias hint pacman value "#
189188
];
190189
let actual = runtime.complete(input, &term).unwrap();
191190
assert_data_eq!(actual, expected);
@@ -200,4 +199,4 @@ fn complete_dynamic() {
200199
];
201200
let actual = runtime.complete(input, &term).unwrap();
202201
assert_data_eq!(actual, expected);
203-
}
202+
}

0 commit comments

Comments
 (0)