Skip to content

Commit

Permalink
refactor(parser): Remove redundant error check
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 12, 2024
1 parent 424a243 commit fb5f179
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 31 deletions.
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 fb5f179

Please sign in to comment.