Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = &str> + 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<Id> {
Expand Down
27 changes: 25 additions & 2 deletions tests/builder/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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'
Expand All @@ -511,7 +532,9 @@ Usage: <COMMAND>

For more information, try 'help'.

"#]], true);
"#]],
true,
);
}

// Verify whatever we did to get the above to work didn't disable `--help` and `--version`.
Expand Down
Loading