mirror of
https://github.com/clap-rs/clap
synced 2025-03-05 07:47:40 +00:00
Refactor: remove a persistent parser state ValidNegNumFound
This commit is contained in:
parent
3c2f60c9f3
commit
407cdac8e0
3 changed files with 13 additions and 55 deletions
|
@ -2554,11 +2554,6 @@ impl<'help> App<'help> {
|
||||||
self.settings.set(s)
|
self.settings.set(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn unset(&mut self, s: AppSettings) {
|
|
||||||
self.settings.unset(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn has_args(&self) -> bool {
|
pub(crate) fn has_args(&self) -> bool {
|
||||||
!self.args.is_empty()
|
!self.args.is_empty()
|
||||||
|
|
|
@ -40,7 +40,6 @@ bitflags! {
|
||||||
const PROPAGATE_VALS_DOWN = 1 << 31;
|
const PROPAGATE_VALS_DOWN = 1 << 31;
|
||||||
const ALLOW_MISSING_POS = 1 << 32;
|
const ALLOW_MISSING_POS = 1 << 32;
|
||||||
const TRAILING_VALUES = 1 << 33;
|
const TRAILING_VALUES = 1 << 33;
|
||||||
const VALID_NEG_NUM_FOUND = 1 << 34;
|
|
||||||
const BUILT = 1 << 35;
|
const BUILT = 1 << 35;
|
||||||
const VALID_ARG_FOUND = 1 << 36;
|
const VALID_ARG_FOUND = 1 << 36;
|
||||||
const INFER_SUBCOMMANDS = 1 << 37;
|
const INFER_SUBCOMMANDS = 1 << 37;
|
||||||
|
@ -141,8 +140,6 @@ impl_settings! { AppSettings, AppFlags,
|
||||||
=> Flags::WAIT_ON_ERROR,
|
=> Flags::WAIT_ON_ERROR,
|
||||||
TrailingValues("trailingvalues")
|
TrailingValues("trailingvalues")
|
||||||
=> Flags::TRAILING_VALUES,
|
=> Flags::TRAILING_VALUES,
|
||||||
ValidNegNumFound("validnegnumfound")
|
|
||||||
=> Flags::VALID_NEG_NUM_FOUND,
|
|
||||||
Built("built")
|
Built("built")
|
||||||
=> Flags::BUILT,
|
=> Flags::BUILT,
|
||||||
ValidArgFound("validargfound")
|
ValidArgFound("validargfound")
|
||||||
|
@ -1051,9 +1048,6 @@ pub enum AppSettings {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
TrailingValues,
|
TrailingValues,
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
ValidNegNumFound,
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
Built,
|
Built,
|
||||||
|
|
||||||
|
@ -1203,10 +1197,6 @@ mod test {
|
||||||
"waitonerror".parse::<AppSettings>().unwrap(),
|
"waitonerror".parse::<AppSettings>().unwrap(),
|
||||||
AppSettings::WaitOnError
|
AppSettings::WaitOnError
|
||||||
);
|
);
|
||||||
assert_eq!(
|
|
||||||
"validnegnumfound".parse::<AppSettings>().unwrap(),
|
|
||||||
AppSettings::ValidNegNumFound
|
|
||||||
);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"validargfound".parse::<AppSettings>().unwrap(),
|
"validargfound".parse::<AppSettings>().unwrap(),
|
||||||
AppSettings::ValidArgFound
|
AppSettings::ValidArgFound
|
||||||
|
|
|
@ -30,7 +30,6 @@ pub(crate) enum ParseResult {
|
||||||
Opt(Id),
|
Opt(Id),
|
||||||
Pos(Id),
|
Pos(Id),
|
||||||
MaybeHyphenValue,
|
MaybeHyphenValue,
|
||||||
MaybeNegNum,
|
|
||||||
NotFound,
|
NotFound,
|
||||||
ValuesDone(Id),
|
ValuesDone(Id),
|
||||||
}
|
}
|
||||||
|
@ -373,14 +372,6 @@ impl<'help, 'app> Parser<'help, 'app> {
|
||||||
// Is this a new argument, or values from a previous option?
|
// Is this a new argument, or values from a previous option?
|
||||||
let starts_new_arg = self.is_new_arg(&arg_os, &needs_val_of);
|
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::<f64>().is_ok()
|
|
||||||
{
|
|
||||||
self.set(AS::ValidNegNumFound);
|
|
||||||
} else {
|
|
||||||
self.unset(AS::ValidNegNumFound);
|
|
||||||
}
|
|
||||||
|
|
||||||
if !self.is_set(AS::TrailingValues) && arg_os == "--" && starts_new_arg {
|
if !self.is_set(AS::TrailingValues) && arg_os == "--" && starts_new_arg {
|
||||||
debug!("Parser::get_matches_with: setting TrailingVals=true");
|
debug!("Parser::get_matches_with: setting TrailingVals=true");
|
||||||
self.set(AS::TrailingValues);
|
self.set(AS::TrailingValues);
|
||||||
|
@ -434,7 +425,13 @@ impl<'help, 'app> Parser<'help, 'app> {
|
||||||
ParseResult::FlagSubCommandShort(_, _) => unreachable!(),
|
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::<f64>().is_ok())
|
||||||
|
{
|
||||||
|
// Arg looks like a short flag, and not a possible number
|
||||||
|
|
||||||
// Try to parse short args like normal, if AllowLeadingHyphen or
|
// Try to parse short args like normal, if AllowLeadingHyphen or
|
||||||
// AllowNegativeNumbers is set, parse_short_arg will *not* throw
|
// AllowNegativeNumbers is set, parse_short_arg will *not* throw
|
||||||
// an error, and instead return Ok(None)
|
// an error, and instead return Ok(None)
|
||||||
|
@ -445,19 +442,6 @@ impl<'help, 'app> Parser<'help, 'app> {
|
||||||
needs_val_of
|
needs_val_of
|
||||||
);
|
);
|
||||||
match needs_val_of {
|
match needs_val_of {
|
||||||
ParseResult::MaybeNegNum => {
|
|
||||||
let lossy_arg = arg_os.to_string_lossy();
|
|
||||||
if !(lossy_arg.parse::<i64>().is_ok()
|
|
||||||
|| lossy_arg.parse::<f64>().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::Opt(ref id)
|
||||||
| ParseResult::Flag(ref id)
|
| ParseResult::Flag(ref id)
|
||||||
| ParseResult::ValuesDone(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
|
// 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.
|
// `-v` `-a` `-l` assuming `v` `a` and `l` are all, or mostly, valid shorts.
|
||||||
if self.is_set(AS::AllowLeadingHyphen) {
|
if self.is_set(AS::AllowLeadingHyphen) && arg.chars().any(|c| !self.contains_short(c)) {
|
||||||
if arg.chars().any(|c| !self.contains_short(c)) {
|
debug!(
|
||||||
debug!(
|
"Parser::parse_short_arg: LeadingHyphenAllowed yet -{} isn't valid",
|
||||||
"Parser::parse_short_arg: LeadingHyphenAllowed yet -{} isn't valid",
|
arg
|
||||||
arg
|
);
|
||||||
);
|
return Ok(ParseResult::MaybeHyphenValue);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut ret = ParseResult::NotFound;
|
let mut ret = ParseResult::NotFound;
|
||||||
|
@ -1782,8 +1759,4 @@ impl<'help, 'app> Parser<'help, 'app> {
|
||||||
pub(crate) fn set(&mut self, s: AS) {
|
pub(crate) fn set(&mut self, s: AS) {
|
||||||
self.app.set(s)
|
self.app.set(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn unset(&mut self, s: AS) {
|
|
||||||
self.app.unset(s)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue