@@ -365,10 +365,32 @@ pub fn render( |
365 | 365 | Error::UnexpectedArg { token } => { |
366 | 366 | with_usage = true; |
367 | 367 | let word = String::from_utf8_lossy(token); |
368 | | - // A word where a subcommand was expected reads better as one — which is the same |
369 | | - // distinction clap draws between an unexpected argument and an unrecognized |
370 | | - // subcommand. |
371 | | - if cmd.subcommands.is_empty() { |
| 368 | + // What the word *looks like* decides, before anything about the command does. A |
| 369 | + // dash-prefixed token is a flag the user got wrong — telling them `--forc` is an |
| 370 | + // unrecognized subcommand is answering a question they did not ask, and it happens |
| 371 | + // on exactly the commands where the mistake is easiest to make: the ones with |
| 372 | + // subcommands, where a bare word would have been one. |
| 373 | + if word.starts_with('-') && word != "-" { |
| 374 | + let _ = writeln!( |
| 375 | + out, |
| 376 | + "{} unexpected argument '{}' found", |
| 377 | + style.error("error:"), |
| 378 | + style.invalid(&word) |
| 379 | + ); |
| 380 | + let bare = word.trim_start_matches('-'); |
| 381 | + let names: Vec<&str> = flags_in_scope(spec, cmd) |
| 382 | + .flat_map(|meta| meta.flag.longs.iter().copied()) |
| 383 | + .collect(); |
| 384 | + let near: Vec<String> = nearest(bare, names.into_iter()) |
| 385 | + .into_iter() |
| 386 | + .map(|name| format!("--{name}")) |
| 387 | + .collect(); |
| 388 | + out.push_str(&tip( |
| 389 | + style, |
| 390 | + "argument", |
| 391 | + &near.iter().map(String::as_str).collect::<Vec<_>>(), |
| 392 | + )); |
| 393 | + } else if cmd.subcommands.is_empty() { |
372 | 394 | let _ = writeln!( |
373 | 395 | out, |
374 | 396 | "{} unexpected argument '{}' found", |
@@ -834,4 +856,35 @@ mod tests { |
834 | 856 | "{message}" |
835 | 857 | ); |
836 | 858 | } |
| 859 | + #[test] |
| 860 | + fn a_dash_prefixed_word_is_a_flag_even_where_subcommands_exist() { |
| 861 | + // The root has subcommands, so a bare word there is a subcommand — but `--forc` is not a |
| 862 | + // subcommand anybody could have meant, and saying "unrecognized subcommand" answers a |
| 863 | + // question the user did not ask. It happens on exactly the commands where the mistake is |
| 864 | + // easiest to make. |
| 865 | + let message = rendered(&[], Error::UnexpectedArg { token: b"--quie" }); |
| 866 | + assert!( |
| 867 | + message.starts_with("error: unexpected argument '--quie' found"), |
| 868 | + "{message}" |
| 869 | + ); |
| 870 | + assert!( |
| 871 | + message.contains("tip: a similar argument exists: '--quiet'"), |
| 872 | + "{message}" |
| 873 | + ); |
| 874 | + assert!(!message.contains("subcommand"), "{message}"); |
| 875 | + |
| 876 | + // A bare word is still a subcommand, which is the other half of the same rule. |
| 877 | + let message = rendered(&[], Error::UnexpectedArg { token: b"usse" }); |
| 878 | + assert!( |
| 879 | + message.starts_with("error: unrecognized subcommand 'usse'"), |
| 880 | + "{message}" |
| 881 | + ); |
| 882 | + |
| 883 | + // A lone `-` is a word, not a flag: it is what several tools spell "standard input". |
| 884 | + let message = rendered(&[], Error::UnexpectedArg { token: b"-" }); |
| 885 | + assert!( |
| 886 | + message.starts_with("error: unrecognized subcommand '-'"), |
| 887 | + "{message}" |
| 888 | + ); |
| 889 | + } |
837 | 890 | } |