Restrict short-hand flag detection to exact match. (#1406)

This commit is contained in:
Andrés N. Robalino 2020-02-18 01:58:30 -05:00 committed by GitHub
parent 0f7c723672
commit 18d988d4c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View file

@ -368,8 +368,13 @@ impl SpannedToken {
match flag.kind {
FlagKind::Longhand if value == name => Some(*flag),
FlagKind::Shorthand if short.is_some() && short == name.chars().next() => {
Some(*flag)
FlagKind::Shorthand => {
if let Some(short_hand) = short {
if short_hand.to_string() == name {
return Some(*flag);
}
}
None
}
_ => None,
}

View file

@ -56,6 +56,29 @@ mod parse {
-r, --raw: Prints the raw value representation.
*/
#[test]
fn errors_if_flag_passed_is_not_exact() {
let actual = nu_error!(cwd: ".", "debug -ra");
assert!(
actual.contains("unexpected flag"),
format!(
"error message '{}' should contain 'unexpected flag'",
actual
)
);
let actual = nu_error!(cwd: ".", "debug --rawx");
assert!(
actual.contains("unexpected flag"),
format!(
"error message '{}' should contain 'unexpected flag'",
actual
)
);
}
#[test]
fn errors_if_flag_is_not_supported() {
let actual = nu_error!(cwd: ".", "debug --ferris");