Skip to content
Merged
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
16 changes: 16 additions & 0 deletions argv/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<SUBCOMMAND>` 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,
Expand Down Expand Up @@ -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
Expand Down