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): suggest a negation, which is a name that works · jdx/usage@db1ac64

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • argv/src
    • diagnostic.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
+43-6Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ fn flag_named(token: &str) -> &str {
115115
}
116116
}
117117

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+
118128
/// Every flag a word at this command could have named: its own, then any ancestor's globals.
119129
///
120130
/// The same set the parser would have accepted, which is what makes a suggestion one that works.
@@ -410,9 +420,7 @@ pub fn render(
410420
// `--fore` came out similar to `--quiet`, which it is not. clap compares the bare
411421
// names for the same reason.
412422
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();
416424
let near: Vec<String> = nearest(bare, names.into_iter())
417425
.into_iter()
418426
.map(|name| format!("--{name}"))
@@ -442,9 +450,7 @@ pub fn render(
442450
style.invalid(named)
443451
);
444452
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();
448454
let near: Vec<String> = nearest(bare, names.into_iter())
449455
.into_iter()
450456
.map(|name| format!("--{name}"))
@@ -645,6 +651,9 @@ mod tests {
645651
name: "force",
646652
longs: &["force"],
647653
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"),
648657
..Flag::BOOL
649658
};
650659
static JOBS: Flag = Flag {
@@ -995,6 +1004,34 @@ mod tests {
9951004
);
9961005
}
9971006

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+
9981035
#[test]
9991036
fn nothing_is_suggested_when_nothing_is_close() {
10001037
// Offering `--force` for `--zzz` is worse than offering nothing: a user reads a tip as

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.