GeezerVison is showing github.com — fix(argv): four from review, and a build that only breaks for adopters · jdx/usage@54fd029
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: argv/src/diagnostic.rs +29-4Lines changed: 29 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -120,7 +120,7 @@ fn flag_named(token: &str) -> &str { | 120 | 120 | /// The same set the parser would have accepted, which is what makes a suggestion one that works. | 121 | 121 | fn flags_in_scope<'a, 'c>( | 122 | 122 | chain: &'c [&'a CommandMeta<'a>], | 123 | | -) -> impl Iterator<Item = &'a crate::spec::FlagMeta<'a>> + use<'a, 'c> { | | 123 | +) -> impl Iterator<Item = &'a crate::spec::FlagMeta<'a>> + 'c { | 124 | 124 | // The command's own flags, and from each ancestor only what it declared global — the rule | 125 | 125 | // the parser follows on the way down. The chain and not the tree: an earlier version | 126 | 126 | // collected globals from every branch it walked through, so a global declared on one command | @@ -569,12 +569,14 @@ pub fn render( | 569 | 569 | ); | 570 | 570 | } | 571 | 571 | Error::ConflictingFlags { name, other } => { | | 572 | + // Spelled by `help`, like every other name in this module — and like clap, which | | 573 | + // writes `the argument '--force' cannot be used with '--jobs <JOBS>'`. | 572 | 574 | let _ = writeln!( | 573 | 575 | out, | 574 | 576 | "{} the argument '{}' cannot be used with '{}'", | 575 | 577 | style.error("error:"), | 576 | | - style.invalid(name), | 577 | | - style.invalid(other) | | 578 | + style.invalid(&shown(here, name)), | | 579 | + style.invalid(&shown(here, other)) | 578 | 580 | ); | 579 | 581 | with_usage = true; | 580 | 582 | } | @@ -600,7 +602,7 @@ pub fn render( | 600 | 602 | out, | 601 | 603 | "{} '{}' can only be given after '{}'", | 602 | 604 | style.error("error:"), | 603 | | - style.literal(arg.name), | | 605 | + style.literal(&shown(here, arg.name)), | 604 | 606 | style.literal("--") | 605 | 607 | ); | 606 | 608 | } | @@ -891,6 +893,29 @@ mod tests { | 891 | 893 | // `--jobs`. | 892 | 894 | let message = rendered(&["use"], Error::MissingRequired { name: "jobs" }); | 893 | 895 | assert!(message.contains(" --jobs"), "{message}"); | | 896 | + | | 897 | + // Every variant, not most of them. These two printed the spec's name while the ones | | 898 | + // directly above and below them did not, so one argument could appear two ways in two | | 899 | + // messages from the same command — and clap writes the dashes here too: | | 900 | + // | | 901 | + // error: the argument '--force' cannot be used with '--jobs <JOBS>' | | 902 | + let message = rendered( | | 903 | + &["use"], | | 904 | + Error::ConflictingFlags { | | 905 | + name: "force", | | 906 | + other: "jobs", | | 907 | + }, | | 908 | + ); | | 909 | + assert!( | | 910 | + message.contains("the argument '--force' cannot be used with '--jobs'"), | | 911 | + "{message}" | | 912 | + ); | | 913 | + | | 914 | + let message = rendered(&["use"], Error::ArgRequiresDoubleDash { arg: &SHELLS }); | | 915 | + assert!( | | 916 | + message.contains("'[SHELLS]…' can only be given"), | | 917 | + "{message}" | | 918 | + ); | 894 | 919 | } | 895 | 920 |
| 896 | 921 | #[test] | |
Collapse file Copy file name to clipboardExpand all lines: argv/src/help.rs +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -378,10 +378,11 @@ fn annotations(out: &mut String, choices: &[&str], env: Option<&str>, default: & | 378 | 378 | out.push('\n'); | 379 | 379 | } | 380 | 380 |
| 381 | | -/// A flag as the flags section lists it, which includes its negation. | 382 | 381 | /// How a usage line writes a flag: its first long form, or its short if that is all it has. | 383 | 382 | /// | 384 | | -/// Shared with the diagnostics for the same reason as [`arg_usage`]. | | 383 | +/// Shared with the diagnostics for the same reason as [`arg_usage`], and gated with them: under | | 384 | +/// `spec` alone nothing calls it, and a `dead_code` warning is an error in this workspace. | | 385 | +#[cfg(feature = "diagnostics")] | 385 | 386 | pub(crate) fn flag_spelling(meta: &FlagMeta<'_>) -> String { | 386 | 387 | meta.flag | 387 | 388 | .longs | |
Collapse file Copy file name to clipboardExpand all lines: benches/gate/tests/errors.rs +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -117,8 +117,9 @@ fn the_usage_line_is_the_one_the_help_prints() { | 117 | 117 |
| 118 | 118 | #[test] | 119 | 119 | fn the_suggestion_is_the_one_clap_would_make() { | 120 | | - // Jaro-Winkler above 0.7, which is clap's rule — so the two suggest in the same cases and | 121 | | - // suggest the same thing. Checked against mise's real flags rather than a fixture, because | | 120 | + // Jaro above 0.7, which is clap's rule — plain Jaro, because clap says in its own source | | 121 | + // that `strsim`'s `jaro_winkler` is wrong (GH #4660). So the two suggest in the same cases | | 122 | + // and suggest the same thing. Checked against mise's real flags rather than a fixture, because | 122 | 123 | // what makes a suggestion good is the size of the set it was chosen from. | 123 | 124 | let cases = [ | 124 | 125 | (vec!["activate", "zsx"], "a similar value exists: 'zsh'"), | |
|