diff --git a/crates/nu-parser/src/parse/token_tree.rs b/crates/nu-parser/src/parse/token_tree.rs index c49c7394f6..536c616697 100644 --- a/crates/nu-parser/src/parse/token_tree.rs +++ b/crates/nu-parser/src/parse/token_tree.rs @@ -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, } diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index 4b04cb197b..d58c70d393 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -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");