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.
This commit is contained in:
Himadri Bhattacharjee 2023-10-19 16:54:57 +05:30 committed by GitHub
parent 27e6271402
commit b907939916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -474,53 +474,28 @@ fn parse_short_flags(
} }
} }
if found_short_flags.is_empty() { if found_short_flags.is_empty()
let arg_contents = working_set.get_span_contents(arg_span);
// check to see if we have a negative number // check to see if we have a negative number
if let Some(positional) = sig.get_positional(positional_idx) { && matches!(
if positional.shape == SyntaxShape::Int sig.get_positional(positional_idx),
|| positional.shape == SyntaxShape::Number Some(PositionalArg {
{ shape: SyntaxShape::Int | SyntaxShape::Number,
if String::from_utf8_lossy(arg_contents).parse::<f64>().is_ok() { ..
return None; })
} else if let Some(first) = unmatched_short_flags.first() { )
let contents = working_set.get_span_contents(*first); && String::from_utf8_lossy(working_set.get_span_contents(arg_span))
working_set.error(ParseError::UnknownFlag( .parse::<f64>()
sig.name.clone(), .is_ok()
format!("-{}", String::from_utf8_lossy(contents)), {
*first, return None;
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(
} else if let Some(first) = unmatched_short_flags.first() { sig.name.clone(),
let contents = working_set.get_span_contents(*first); format!("-{}", String::from_utf8_lossy(contents)),
working_set.error(ParseError::UnknownFlag( *first,
sig.name.clone(), sig.clone().formatted_flags(),
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(),
));
}
} }
Some(found_short_flags) Some(found_short_flags)