From b907939916082e57c499169c42e562035eacfe8c Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee Date: Thu, 19 Oct 2023 16:54:57 +0530 Subject: [PATCH] Extract common logic for setting error in `parse_short_flags` (#10709) # Description Since the `else` clause for the nested branches check for the first unmatched argument, this PR brings together all the conditions where the positional argument shape is numeric using the `matches!` keyword. This also allows us to and (`&&`) the condition with when no short flags are found unlike the `if let ...` statements. Finally, we can handle any `unmatched_short_flags` at one place. # User-Facing Changes No user facing changes. --- crates/nu-parser/src/parser.rs | 67 +++++++++++----------------------- 1 file changed, 21 insertions(+), 46 deletions(-) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 6a06a4f90..010db7201 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -474,53 +474,28 @@ fn parse_short_flags( } } - if found_short_flags.is_empty() { - let arg_contents = working_set.get_span_contents(arg_span); - + if found_short_flags.is_empty() // check to see if we have a negative number - if let Some(positional) = sig.get_positional(positional_idx) { - if positional.shape == SyntaxShape::Int - || positional.shape == SyntaxShape::Number - { - if String::from_utf8_lossy(arg_contents).parse::().is_ok() { - return None; - } else if let Some(first) = unmatched_short_flags.first() { - let contents = working_set.get_span_contents(*first); - working_set.error(ParseError::UnknownFlag( - sig.name.clone(), - format!("-{}", String::from_utf8_lossy(contents)), - *first, - sig.clone().formatted_flags(), - )); - } - } else if let Some(first) = unmatched_short_flags.first() { - let contents = working_set.get_span_contents(*first); - working_set.error(ParseError::UnknownFlag( - sig.name.clone(), - format!("-{}", String::from_utf8_lossy(contents)), - *first, - sig.clone().formatted_flags(), - )); - } - } else if let Some(first) = unmatched_short_flags.first() { - let contents = working_set.get_span_contents(*first); - working_set.error(ParseError::UnknownFlag( - sig.name.clone(), - format!("-{}", String::from_utf8_lossy(contents)), - *first, - sig.clone().formatted_flags(), - )); - } - } else if !unmatched_short_flags.is_empty() { - if let Some(first) = unmatched_short_flags.first() { - let contents = working_set.get_span_contents(*first); - working_set.error(ParseError::UnknownFlag( - sig.name.clone(), - format!("-{}", String::from_utf8_lossy(contents)), - *first, - sig.clone().formatted_flags(), - )); - } + && matches!( + sig.get_positional(positional_idx), + Some(PositionalArg { + shape: SyntaxShape::Int | SyntaxShape::Number, + .. + }) + ) + && String::from_utf8_lossy(working_set.get_span_contents(arg_span)) + .parse::() + .is_ok() + { + return None; + } else if let Some(first) = unmatched_short_flags.first() { + let contents = working_set.get_span_contents(*first); + working_set.error(ParseError::UnknownFlag( + sig.name.clone(), + format!("-{}", String::from_utf8_lossy(contents)), + *first, + sig.clone().formatted_flags(), + )); } Some(found_short_flags)