@@ -115,6 +115,16 @@ fn flag_named(token: &str) -> &str { |
115 | 115 | } |
116 | 116 | } |
117 | 117 |
|
| 118 | +/// Every long spelling a flag answers to, its negation included. |
| 119 | +/// |
| 120 | +/// The parser takes `--no-color` through `find_negation`, and the completions offer it, so a |
| 121 | +/// suggestion that leaves it out is the odd one — a near miss of a name that works gets silence. |
| 122 | +/// clap has no separate notion of a negation, so the two forms are two arguments there and it |
| 123 | +/// suggests either; matching that is the point. |
| 124 | +fn long_spellings<'a>(meta: &'a crate::spec::FlagMeta<'a>) -> impl Iterator<Item = &'a str> { |
| 125 | + meta.flag.longs.iter().copied().chain(meta.flag.negate) |
| 126 | +} |
| 127 | + |
118 | 128 | /// Every flag a word at this command could have named: its own, then any ancestor's globals. |
119 | 129 | /// |
120 | 130 | /// The same set the parser would have accepted, which is what makes a suggestion one that works. |
@@ -410,9 +420,7 @@ pub fn render( |
410 | 420 | // `--fore` came out similar to `--quiet`, which it is not. clap compares the bare |
411 | 421 | // names for the same reason. |
412 | 422 | let bare = typed.trim_start_matches('-'); |
413 | | - let names: Vec<&str> = flags_in_scope(chain) |
414 | | - .flat_map(|meta| meta.flag.longs.iter().copied()) |
415 | | - .collect(); |
| 423 | + let names: Vec<&str> = flags_in_scope(chain).flat_map(long_spellings).collect(); |
416 | 424 | let near: Vec<String> = nearest(bare, names.into_iter()) |
417 | 425 | .into_iter() |
418 | 426 | .map(|name| format!("--{name}")) |
@@ -442,9 +450,7 @@ pub fn render( |
442 | 450 | style.invalid(named) |
443 | 451 | ); |
444 | 452 | let bare = named.trim_start_matches('-'); |
445 | | - let names: Vec<&str> = flags_in_scope(chain) |
446 | | - .flat_map(|meta| meta.flag.longs.iter().copied()) |
447 | | - .collect(); |
| 453 | + let names: Vec<&str> = flags_in_scope(chain).flat_map(long_spellings).collect(); |
448 | 454 | let near: Vec<String> = nearest(bare, names.into_iter()) |
449 | 455 | .into_iter() |
450 | 456 | .map(|name| format!("--{name}")) |
@@ -645,6 +651,9 @@ mod tests { |
645 | 651 | name: "force", |
646 | 652 | longs: &["force"], |
647 | 653 | shorts: b"f", |
| 654 | + // A negation, because a flag's spellings are not only its `longs` and the parser takes |
| 655 | + // this one — so a suggestion that cannot offer it is offering less than the CLI accepts. |
| 656 | + negate: Some("no-force"), |
648 | 657 | ..Flag::BOOL |
649 | 658 | }; |
650 | 659 | static JOBS: Flag = Flag { |
@@ -995,6 +1004,34 @@ mod tests { |
995 | 1004 | ); |
996 | 1005 | } |
997 | 1006 |
|
| 1007 | + #[test] |
| 1008 | + fn a_negation_is_suggested_like_any_other_spelling() { |
| 1009 | + // `--no-force` is a name the parser accepts, through `find_negation`, and one the |
| 1010 | + // completions already offer. Scoring only `longs` left it out, so a near miss of a name |
| 1011 | + // that works got silence — and clap, which has no separate notion of a negation and |
| 1012 | + // sees two arguments, suggests it. Measured: |
| 1013 | + // |
| 1014 | + // error: unexpected argument '--no-colr' found |
| 1015 | + // tip: a similar argument exists: '--no-color' |
| 1016 | + let message = rendered( |
| 1017 | + &["use"], |
| 1018 | + Error::UnknownFlag { |
| 1019 | + token: b"--no-forc", |
| 1020 | + }, |
| 1021 | + ); |
| 1022 | + assert!( |
| 1023 | + message.contains("tip: a similar argument exists: '--no-force'"), |
| 1024 | + "{message}" |
| 1025 | + ); |
| 1026 | + |
| 1027 | + // And the plain form is still found, which is the half that already worked. |
| 1028 | + let message = rendered(&["use"], Error::UnknownFlag { token: b"--fore" }); |
| 1029 | + assert!( |
| 1030 | + message.contains("tip: a similar argument exists: '--force'"), |
| 1031 | + "{message}" |
| 1032 | + ); |
| 1033 | + } |
| 1034 | + |
998 | 1035 | #[test] |
999 | 1036 | fn nothing_is_suggested_when_nothing_is_close() { |
1000 | 1037 | // Offering `--force` for `--zzz` is worse than offering nothing: a user reads a tip as |
|