GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(argv): call a dash-prefixed word a flag, not a subcommand · jdx/usage@390a9ec

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • argv/src
    • diagnostic.rs
  • benches/gate/tests
    • errors.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎argv/src/diagnostic.rs‎

Copy file name to clipboardExpand all lines: argv/src/diagnostic.rs
+57-4Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,32 @@ pub fn render(
365365
Error::UnexpectedArg { token } => {
366366
with_usage = true;
367367
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() {
372394
let _ = writeln!(
373395
out,
374396
"{} unexpected argument '{}' found",
@@ -834,4 +856,35 @@ mod tests {
834856
"{message}"
835857
);
836858
}
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+
}
837890
}
Collapse file

‎benches/gate/tests/errors.rs‎

Copy file name to clipboardExpand all lines: benches/gate/tests/errors.rs
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,15 @@ fn mise_accepts_an_unknown_flag_where_clap_refuses_one() {
183183
.any(|f| f.flag.longs.contains(&"global"));
184184
assert!(has_global, "mise use declares --global");
185185
}
186+
187+
#[test]
188+
fn a_flag_typo_on_a_command_with_subcommands_reads_as_a_flag() {
189+
// `mise doctor --forc` fails on both sides, and both should call it an unexpected *argument*
190+
// — `doctor` has subcommands, so the tempting answer is "unrecognized subcommand", which is
191+
// an answer to a question nobody asked.
192+
let words = ["doctor", "--forc"];
193+
let ours = our_error(&words).expect("a failure");
194+
let theirs = clap_error(&words).expect("a failure");
195+
assert_eq!(first_line(&ours), first_line(&theirs), "\n{ours}\n{theirs}");
196+
assert!(!ours.contains("subcommand"), "{ours}");
197+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.