Revert "Fixes arg and group with default value having requires"

This reverts commit 130dcbfdd9.
This commit is contained in:
Pavan Kumar Sunkara 2021-11-03 18:22:19 +00:00
parent 8818327b25
commit bd8c36cf04
3 changed files with 5 additions and 75 deletions

View file

@ -27,7 +27,7 @@ use crate::{
build::{arg::ArgProvider, Arg, ArgGroup, ArgSettings},
mkeymap::MKeyMap,
output::{fmt::Colorizer, Help, HelpWriter, Usage},
parse::{ArgMatcher, ArgMatches, Input, Parser, ValueType},
parse::{ArgMatcher, ArgMatches, Input, Parser},
util::{color::ColorChoice, Id, Key},
Error, ErrorKind, Result as ClapResult, INTERNAL_ERROR_MSG,
};
@ -2961,7 +2961,6 @@ impl<'help> App<'help> {
if let Some(v) = val {
if matcher
.get(arg)
.filter(|ma| !matches!(ma.ty, ValueType::DefaultValue))
.map(|ma| ma.contains_val(v))
.unwrap_or(false)
{
@ -2969,10 +2968,8 @@ impl<'help> App<'help> {
} else {
None
}
} else if !matcher.is_default_value(arg) {
Some(req_arg.clone())
} else {
None
Some(req_arg.clone())
}
};

View file

@ -402,10 +402,8 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
}
} else if let Some(g) = self.p.app.groups.iter().find(|grp| grp.id == *name) {
debug!("Validator::gather_conflicts:iter:{:?}:group", name);
if !matcher.is_default_value(&g.id) {
for r in &g.requires {
self.p.required.insert(r.clone());
}
for r in &g.requires {
self.p.required.insert(r.clone());
}
}
}

View file

@ -238,7 +238,7 @@ fn required_unless_present() {
}
#[test]
fn required_unless_present_err() {
fn required_unless_err() {
let res = App::new("unlesstest")
.arg(
Arg::new("cfg")
@ -1141,68 +1141,3 @@ fn required_unless_invalid_arg() {
)
.try_get_matches_from(vec![""]);
}
#[test]
fn requires_with_default_value() {
let result = App::new("prog")
.arg(
Arg::new("opt")
.long("opt")
.default_value("default")
.requires("flag"),
)
.arg(Arg::new("flag").long("flag"))
.try_get_matches_from(vec!["myprog"]);
assert!(
result.is_ok(),
"requires should ignore default_value: {:?}",
result.unwrap_err()
);
let m = result.unwrap();
assert_eq!(m.value_of("opt"), Some("default"));
assert!(!m.is_present("flag"));
}
#[test]
fn requires_if_with_default_value() {
let result = App::new("prog")
.arg(
Arg::new("opt")
.long("opt")
.default_value("default")
.requires_if("default", "flag"),
)
.arg(Arg::new("flag").long("flag"))
.try_get_matches_from(vec!["myprog"]);
assert!(
result.is_ok(),
"requires_if should ignore default_value: {:?}",
result.unwrap_err()
);
let m = result.unwrap();
assert_eq!(m.value_of("opt"), Some("default"));
assert!(!m.is_present("flag"));
}
#[test]
fn group_requires_with_default_value() {
let result = App::new("prog")
.arg(Arg::new("opt").long("opt").default_value("default"))
.arg(Arg::new("flag").long("flag"))
.group(ArgGroup::new("one").arg("opt").requires("flag"))
.try_get_matches_from(vec!["myprog"]);
assert!(
result.is_ok(),
"arg group requires should ignore default_value: {:?}",
result.unwrap_err()
);
let m = result.unwrap();
assert_eq!(m.value_of("opt"), Some("default"));
assert!(!m.is_present("flag"));
}