Skip to content

Commit

Permalink
fix: Clarify short rejection in debug
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 4, 2022
1 parent bcbe126 commit 44bea94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// HACK https://github.com/rust-lang/rust-clippy/issues/7290
#![allow(clippy::single_component_path_imports)]
#![allow(clippy::branches_sharing_code)]
// Doesn't allow for debug statements, etc to be unique
#![allow(clippy::if_same_then_else)]

#[cfg(not(feature = "std"))]
compile_error!("`std` feature is currently required to build `clap`");
Expand Down
30 changes: 21 additions & 9 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,17 +1181,29 @@ impl<'help, 'app> Parser<'help, 'app> {
debug!("Parser::parse_short_arg: short_arg={:?}", short_arg);
let arg = short_arg.to_str_lossy();

if (self.is_set(AS::AllowNegativeNumbers) && arg.parse::<f64>().is_ok())
|| (self.is_set(AS::AllowHyphenValues)
&& arg.chars().any(|c| !self.app.contains_short(c)))
|| matches!(parse_state, ParseState::Opt(opt) | ParseState::Pos(opt)
if self.is_set(AS::AllowNegativeNumbers) && arg.parse::<f64>().is_ok() {
debug!("Parser::parse_short_arg: negative number");
return ParseResult::MaybeHyphenValue;
} else if self.is_set(AS::AllowHyphenValues)
&& arg.chars().any(|c| !self.app.contains_short(c))
{
debug!("Parser::parse_short_args: contains non-short flag");
return ParseResult::MaybeHyphenValue;
} else if matches!(parse_state, ParseState::Opt(opt) | ParseState::Pos(opt)
if self.app[opt].is_set(ArgSettings::AllowHyphenValues))
|| self
.app
.args
.get(&pos_counter)
.map_or(false, |arg| arg.is_set(ArgSettings::AllowHyphenValues))
{
debug!("Parser::parse_short_args: prior arg accepts hyphenated values",);
return ParseResult::MaybeHyphenValue;
} else if self
.app
.args
.get(&pos_counter)
.map_or(false, |arg| arg.is_set(ArgSettings::AllowHyphenValues))
{
debug!(
"Parser::parse_short_args: positional at {} allows hyphens",
pos_counter
);
return ParseResult::MaybeHyphenValue;
}

Expand Down

0 comments on commit 44bea94

Please sign in to comment.