From 18d988d4c8ac58341324d05e9ebede80be3d395a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Tue, 18 Feb 2020 01:58:30 -0500 Subject: [PATCH] Restrict short-hand flag detection to exact match. (#1406) --- crates/nu-parser/src/parse/token_tree.rs | 9 +++++++-- tests/shell/pipeline/commands/internal.rs | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) 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");