From 407cdac8e06d89d96ee77f8d8e5c04196b011afc Mon Sep 17 00:00:00 2001 From: Donough Liu Date: Sun, 8 Nov 2020 15:31:52 +0800 Subject: [PATCH] Refactor: remove a persistent parser state `ValidNegNumFound` --- src/build/app/mod.rs | 5 ---- src/build/app/settings.rs | 10 -------- src/parse/parser.rs | 53 ++++++++++----------------------------- 3 files changed, 13 insertions(+), 55 deletions(-) diff --git a/src/build/app/mod.rs b/src/build/app/mod.rs index 1da76ffe..81bbb954 100644 --- a/src/build/app/mod.rs +++ b/src/build/app/mod.rs @@ -2554,11 +2554,6 @@ impl<'help> App<'help> { self.settings.set(s) } - #[inline] - pub(crate) fn unset(&mut self, s: AppSettings) { - self.settings.unset(s) - } - #[inline] pub(crate) fn has_args(&self) -> bool { !self.args.is_empty() diff --git a/src/build/app/settings.rs b/src/build/app/settings.rs index 4770a214..207064fc 100644 --- a/src/build/app/settings.rs +++ b/src/build/app/settings.rs @@ -40,7 +40,6 @@ bitflags! { const PROPAGATE_VALS_DOWN = 1 << 31; const ALLOW_MISSING_POS = 1 << 32; const TRAILING_VALUES = 1 << 33; - const VALID_NEG_NUM_FOUND = 1 << 34; const BUILT = 1 << 35; const VALID_ARG_FOUND = 1 << 36; const INFER_SUBCOMMANDS = 1 << 37; @@ -141,8 +140,6 @@ impl_settings! { AppSettings, AppFlags, => Flags::WAIT_ON_ERROR, TrailingValues("trailingvalues") => Flags::TRAILING_VALUES, - ValidNegNumFound("validnegnumfound") - => Flags::VALID_NEG_NUM_FOUND, Built("built") => Flags::BUILT, ValidArgFound("validargfound") @@ -1051,9 +1048,6 @@ pub enum AppSettings { #[doc(hidden)] TrailingValues, - #[doc(hidden)] - ValidNegNumFound, - #[doc(hidden)] Built, @@ -1203,10 +1197,6 @@ mod test { "waitonerror".parse::().unwrap(), AppSettings::WaitOnError ); - assert_eq!( - "validnegnumfound".parse::().unwrap(), - AppSettings::ValidNegNumFound - ); assert_eq!( "validargfound".parse::().unwrap(), AppSettings::ValidArgFound diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 8e4605c9..4553cb3b 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -30,7 +30,6 @@ pub(crate) enum ParseResult { Opt(Id), Pos(Id), MaybeHyphenValue, - MaybeNegNum, NotFound, ValuesDone(Id), } @@ -373,14 +372,6 @@ impl<'help, 'app> Parser<'help, 'app> { // Is this a new argument, or values from a previous option? let starts_new_arg = self.is_new_arg(&arg_os, &needs_val_of); - if self.is_set(AS::AllowNegativeNumbers) - && arg_os.to_string_lossy().parse::().is_ok() - { - self.set(AS::ValidNegNumFound); - } else { - self.unset(AS::ValidNegNumFound); - } - if !self.is_set(AS::TrailingValues) && arg_os == "--" && starts_new_arg { debug!("Parser::get_matches_with: setting TrailingVals=true"); self.set(AS::TrailingValues); @@ -434,7 +425,13 @@ impl<'help, 'app> Parser<'help, 'app> { ParseResult::FlagSubCommandShort(_, _) => unreachable!(), _ => (), } - } else if arg_os.starts_with("-") && arg_os.len() != 1 { + } else if arg_os.starts_with("-") + && arg_os.len() != 1 + && !(self.is_set(AS::AllowNegativeNumbers) + && arg_os.to_string_lossy().parse::().is_ok()) + { + // Arg looks like a short flag, and not a possible number + // Try to parse short args like normal, if AllowLeadingHyphen or // AllowNegativeNumbers is set, parse_short_arg will *not* throw // an error, and instead return Ok(None) @@ -445,19 +442,6 @@ impl<'help, 'app> Parser<'help, 'app> { needs_val_of ); match needs_val_of { - ParseResult::MaybeNegNum => { - let lossy_arg = arg_os.to_string_lossy(); - if !(lossy_arg.parse::().is_ok() - || lossy_arg.parse::().is_ok()) - { - return Err(ClapError::unknown_argument( - lossy_arg.to_string(), - None, - Usage::new(self).create_usage_with_title(&[]), - self.app.color(), - )); - } - } ParseResult::Opt(ref id) | ParseResult::Flag(ref id) | ParseResult::ValuesDone(ref id) => { @@ -1200,19 +1184,12 @@ impl<'help, 'app> Parser<'help, 'app> { // If AllowLeadingHyphen is set, we want to ensure `-val` gets parsed as `-val` and not // `-v` `-a` `-l` assuming `v` `a` and `l` are all, or mostly, valid shorts. - if self.is_set(AS::AllowLeadingHyphen) { - if arg.chars().any(|c| !self.contains_short(c)) { - debug!( - "Parser::parse_short_arg: LeadingHyphenAllowed yet -{} isn't valid", - arg - ); - return Ok(ParseResult::MaybeHyphenValue); - } - } else if self.is_set(AS::ValidNegNumFound) { - // TODO: Add docs about having AllowNegativeNumbers and `-2` as a valid short - // May be better to move this to *after* not finding a valid flag/opt? - debug!("Parser::parse_short_arg: Valid negative num..."); - return Ok(ParseResult::MaybeNegNum); + if self.is_set(AS::AllowLeadingHyphen) && arg.chars().any(|c| !self.contains_short(c)) { + debug!( + "Parser::parse_short_arg: LeadingHyphenAllowed yet -{} isn't valid", + arg + ); + return Ok(ParseResult::MaybeHyphenValue); } let mut ret = ParseResult::NotFound; @@ -1782,8 +1759,4 @@ impl<'help, 'app> Parser<'help, 'app> { pub(crate) fn set(&mut self, s: AS) { self.app.set(s) } - - pub(crate) fn unset(&mut self, s: AS) { - self.app.unset(s) - } }