Merge pull request #5814 from epage/parse

refactor(parser): Remove redundant error check
This commit is contained in:
Ed Page 2024-11-12 11:53:55 -06:00 committed by GitHub
commit 2e5c74423e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 5 additions and 35 deletions

View file

@ -132,10 +132,6 @@ impl MatchedArg {
self.vals.last().map(|x| x.len()).unwrap_or(0)
}
pub(crate) fn all_val_groups_empty(&self) -> bool {
self.vals.iter().flatten().count() == 0
}
pub(crate) fn check_explicit(&self, predicate: &ArgPredicate) -> bool {
if self.source.map(|s| !s.is_explicit()).unwrap_or(false) {
return false;

View file

@ -12,8 +12,8 @@ pub(crate) mod features;
pub(crate) use self::arg_matcher::ArgMatcher;
pub(crate) use self::matches::{MatchedArg, SubCommand};
pub(crate) use self::parser::Identifier;
pub(crate) use self::parser::Parser;
pub(crate) use self::parser::PendingArg;
pub(crate) use self::parser::{ParseState, Parser};
pub(crate) use self::validator::get_possible_values_cli;
pub(crate) use self::validator::Validator;

View file

@ -440,7 +440,7 @@ impl<'cmd> Parser<'cmd> {
#[cfg(feature = "env")]
ok!(self.add_env(matcher));
ok!(self.add_defaults(matcher));
return Validator::new(self.cmd).validate(parse_state, matcher);
return Validator::new(self.cmd).validate(matcher);
} else {
// Start error processing
let _ = self.resolve_pending(matcher);
@ -478,7 +478,7 @@ impl<'cmd> Parser<'cmd> {
#[cfg(feature = "env")]
ok!(self.add_env(matcher));
ok!(self.add_defaults(matcher));
Validator::new(self.cmd).validate(parse_state, matcher)
Validator::new(self.cmd).validate(matcher)
}
fn match_arg_error(

View file

@ -3,7 +3,7 @@ use crate::builder::StyledStr;
use crate::builder::{Arg, ArgGroup, ArgPredicate, Command, PossibleValue};
use crate::error::{Error, Result as ClapResult};
use crate::output::Usage;
use crate::parser::{ArgMatcher, ParseState};
use crate::parser::ArgMatcher;
use crate::util::ChildGraph;
use crate::util::FlatMap;
use crate::util::FlatSet;
@ -21,37 +21,11 @@ impl<'cmd> Validator<'cmd> {
Validator { cmd, required }
}
pub(crate) fn validate(
&mut self,
parse_state: ParseState,
matcher: &mut ArgMatcher,
) -> ClapResult<()> {
pub(crate) fn validate(&mut self, matcher: &mut ArgMatcher) -> ClapResult<()> {
debug!("Validator::validate");
let conflicts = Conflicts::with_args(self.cmd, matcher);
let has_subcmd = matcher.subcommand_name().is_some();
if let ParseState::Opt(a) = parse_state {
debug!("Validator::validate: needs_val_of={a:?}");
let o = &self.cmd[&a];
let should_err = if let Some(v) = matcher.args.get(o.get_id()) {
v.all_val_groups_empty() && o.get_min_vals() != 0
} else {
true
};
if should_err {
return Err(Error::empty_value(
self.cmd,
&get_possible_values_cli(o)
.iter()
.filter(|pv| !pv.is_hide_set())
.map(|n| n.get_name().to_owned())
.collect::<Vec<_>>(),
o.to_string(),
));
}
}
if !has_subcmd && self.cmd.is_arg_required_else_help_set() {
let num_user_values = matcher
.args()