diff --git a/clap_complete/examples/dynamic.rs b/clap_complete/examples/dynamic.rs index 24d9682c108..0813b7fe6a0 100644 --- a/clap_complete/examples/dynamic.rs +++ b/clap_complete/examples/dynamic.rs @@ -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") diff --git a/clap_complete/src/dynamic/completer.rs b/clap_complete/src/dynamic/completer.rs index b1b1f0803ed..7267d178603 100644 --- a/clap_complete/src/dynamic/completer.rs +++ b/clap_complete/src/dynamic/completer.rs @@ -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::>(); scs.sort(); scs.dedup(); @@ -338,11 +338,11 @@ fn possible_values(a: &clap::Arg) -> Option> { /// /// Subcommand `rustup toolchain install` would be converted to /// `("install", "rustup toolchain install")`. -fn subcommands(p: &clap::Command) -> Vec<(String, Option)> { +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() } diff --git a/clap_complete/tests/testsuite/dynamic.rs b/clap_complete/tests/testsuite/dynamic.rs index f6e971d1899..8f8b3786319 100644 --- a/clap_complete/tests/testsuite/dynamic.rs +++ b/clap_complete/tests/testsuite/dynamic.rs @@ -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( @@ -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]