Skip to content

Commit

Permalink
feat: Support hiding subcommands by default
Browse files Browse the repository at this point in the history
Signed-off-by: Natsuki Ikeguchi <[email protected]>
  • Loading branch information
siketyan committed Jan 4, 2024
1 parent 6d6ce25 commit a4fbacb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions clap_complete/examples/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ use clap::Subcommand;

fn command() -> clap::Command {
let cmd = clap::Command::new("dynamic")
.subcommand(
clap::Command::new("hidden")
.about("Hidden subcommand")
.hide(true),
)
.arg(
clap::Arg::new("input")
.long("input")
Expand Down
8 changes: 4 additions & 4 deletions clap_complete/src/dynamic/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ fn complete_subcommand(value: &str, cmd: &clap::Command) -> Vec<(OsString, Optio

let mut scs = subcommands(cmd)
.into_iter()
.filter(|x| x.0.starts_with(value))
.map(|x| (OsString::from(&x.0), x.1))
.filter(|(n, sc)| n.starts_with(value) && (value != "" || !sc.is_hide_set()))
.map(|(n, sc)| (OsString::from(&n), sc.get_about().cloned()))
.collect::<Vec<_>>();
scs.sort();
scs.dedup();
Expand Down Expand Up @@ -338,11 +338,11 @@ fn possible_values(a: &clap::Arg) -> Option<Vec<clap::builder::PossibleValue>> {
///
/// Subcommand `rustup toolchain install` would be converted to
/// `("install", "rustup toolchain install")`.
fn subcommands(p: &clap::Command) -> Vec<(String, Option<StyledStr>)> {
fn subcommands(p: &clap::Command) -> Vec<(String, &clap::Command)> {
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()))
.map(|sc| (sc.get_name().to_string(), sc))
.collect()
}
11 changes: 10 additions & 1 deletion clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ macro_rules! complete {
fn suggest_subcommand_subset() {
let mut cmd = Command::new("exhaustive")
.subcommand(Command::new("hello-world"))
.subcommand(Command::new("hello-moon"))
.subcommand(Command::new("hello-moon").hide(true))
.subcommand(Command::new("goodbye-world"));

snapbox::assert_eq(
Expand All @@ -28,6 +28,15 @@ hello-world
help\tPrint this message or the help of the given subcommand(s)",
complete!(cmd, "he"),
);

snapbox::assert_eq(
"--help\tPrint help
-h\tPrint help
goodbye-world
hello-world
help\tPrint this message or the help of the given subcommand(s)",
complete!(cmd, " "),
);
}

#[test]
Expand Down

0 comments on commit a4fbacb

Please sign in to comment.