diff --git a/argv/src/diagnostic.rs b/argv/src/diagnostic.rs index 8fb024c1a..a6709fd2a 100644 --- a/argv/src/diagnostic.rs +++ b/argv/src/diagnostic.rs @@ -508,6 +508,13 @@ pub fn render( ); } Error::MissingSubcommand => { + // A bare command that can do nothing on its own is a request for orientation. + // clap prints the command's help page here, including the available subcommands, + // while keeping exit 2; an error plus only `` tells the reader what is + // missing and withholds the list they need to fix it. + if let Some(help) = crate::help::render_at(spec, &taken, false) { + return help; + } with_usage = true; let _ = writeln!( out, @@ -811,6 +818,15 @@ mod tests { assert_eq!(line, crate::help::usage_line(&["ex", "use"], &USE_META)); } + #[test] + fn a_missing_subcommand_prints_the_choices() { + let message = rendered(&[], Error::MissingSubcommand); + assert!(message.contains("Commands:"), "{message}"); + assert!(message.contains("use"), "{message}"); + assert!(message.contains("user"), "{message}"); + assert!(!message.contains("requires a subcommand"), "{message}"); + } + #[test] fn a_missing_value_names_what_it_wanted() { // No usage block: the shape of the command line was right, one value was missing — which