diff --git a/clap_builder/src/builder/command.rs b/clap_builder/src/builder/command.rs index b0d5bebd838..946a7876236 100644 --- a/clap_builder/src/builder/command.rs +++ b/clap_builder/src/builder/command.rs @@ -5024,11 +5024,13 @@ impl Command { /// Iterate through all the names of all subcommands (not recursively), including aliases. /// Used for suggestions. pub(crate) fn all_subcommand_names(&self) -> impl Iterator + Captures<'_> { - self.get_subcommands().flat_map(|sc| { - let name = sc.get_name(); - let aliases = sc.get_all_aliases(); - std::iter::once(name).chain(aliases) - }) + self.get_subcommands() + .filter(|sc| !sc.is_hide_set()) + .flat_map(|sc| { + let name = sc.get_name(); + let aliases = sc.get_all_aliases(); + std::iter::once(name).chain(aliases) + }) } pub(crate) fn required_graph(&self) -> ChildGraph { diff --git a/tests/builder/subcommands.rs b/tests/builder/subcommands.rs index a3f6a205859..d4d31196bef 100644 --- a/tests/builder/subcommands.rs +++ b/tests/builder/subcommands.rs @@ -136,6 +136,24 @@ For more information, try '--help'. utils::assert_output(cmd, "dym te", DYM_SUBCMD_AMBIGUOUS, true); } +#[test] +#[cfg(feature = "suggestions")] +#[cfg(feature = "error-context")] +fn subcmd_did_you_mean_hidden_not_suggested() { + static DYM_SUBCMD_HIDDEN: &str = "\ +error: unrecognized subcommand 'tes' + +Usage: dym [COMMAND] + +For more information, try '--help'. +"; + + let cmd = Command::new("dym") + .subcommand(Command::new("test").hide(true)) + .subcommand(Command::new("other")); + utils::assert_output(cmd, "dym tes", DYM_SUBCMD_HIDDEN, true); +} + #[test] #[cfg(feature = "suggestions")] #[cfg(feature = "error-context")] @@ -502,7 +520,10 @@ For more information, try 'help'. #[cfg(feature = "suggestions")] { let err = cmd.clone().try_get_matches_from(["baz"]).unwrap_err(); - utils::assert_error(err, ErrorKind::InvalidSubcommand, str![[r#" + utils::assert_error( + err, + ErrorKind::InvalidSubcommand, + str![[r#" error: unrecognized subcommand 'baz' tip: a similar subcommand exists: 'bar' @@ -511,7 +532,9 @@ Usage: For more information, try 'help'. -"#]], true); +"#]], + true, + ); } // Verify whatever we did to get the above to work didn't disable `--help` and `--version`.