Skip to content

Commit

Permalink
Merge pull request #5814 from epage/parse
Browse files Browse the repository at this point in the history
refactor(parser): Remove redundant error check
  • Loading branch information
epage authored Nov 12, 2024
2 parents 424a243 + 551e052 commit 2e5c744
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 35 deletions.
4 changes: 0 additions & 4 deletions clap_builder/src/parser/matches/matched_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clap_builder/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions clap_builder/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
30 changes: 2 additions & 28 deletions clap_builder/src/parser/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down

0 comments on commit 2e5c744

Please sign in to comment.