2172: Don't use default value when option is passed r=pksunkara a=davidhewitt



Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
This commit is contained in:
bors[bot] 2020-10-14 20:54:10 +00:00 committed by GitHub
commit d2dd54b555
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 18 deletions

View file

@ -1613,23 +1613,8 @@ impl<'help, 'app> Parser<'help, 'app> {
if !arg.default_vals.is_empty() {
debug!("Parser::add_value:iter:{}: has default vals", arg.name);
if matcher
.get(&arg.id)
.map(|ma| ma.vals.len())
.map(|len| len == 0)
.unwrap_or(false)
{
debug!(
"Parser::add_value:iter:{}: has no user defined vals",
arg.name
);
for val in &arg.default_vals {
self.add_val_to_arg(arg, &ArgStr::new(val), matcher, ty)?;
}
} else if matcher.get(&arg.id).is_some() {
debug!("Parser::add_value:iter:{}: has user defined vals", arg.name);
if matcher.get(&arg.id).is_some() {
debug!("Parser::add_value:iter:{}: was used", arg.name);
// do nothing
} else {
debug!("Parser::add_value:iter:{}: wasn't used", arg.name);
@ -1647,6 +1632,39 @@ impl<'help, 'app> Parser<'help, 'app> {
// do nothing
}
if !arg.default_missing_vals.is_empty() {
debug!(
"Parser::add_value:iter:{}: has default missing vals",
arg.name
);
match matcher.get(&arg.id) {
Some(ma) if ma.vals.is_empty() => {
debug!(
"Parser::add_value:iter:{}: has no user defined vals",
arg.name
);
for val in &arg.default_missing_vals {
self.add_val_to_arg(arg, &ArgStr::new(val), matcher, ty)?;
}
}
None => {
debug!("Parser::add_value:iter:{}: wasn't used", arg.name);
// do nothing
}
_ => {
debug!("Parser::add_value:iter:{}: has user defined vals", arg.name);
// do nothing
}
}
} else {
debug!(
"Parser::add_value:iter:{}: doesn't have default missing vals",
arg.name
);
// do nothing
}
Ok(())
}

View file

@ -11,6 +11,19 @@ fn opts() {
assert_eq!(m.value_of("o").unwrap(), "default");
}
#[test]
fn opt_without_value_fail() {
let r = App::new("df")
.arg(Arg::from("-o [opt] 'some opt'").default_value("default"))
.try_get_matches_from(vec!["", "-o"]);
assert!(r.is_err());
let err = r.unwrap_err();
assert_eq!(err.kind, ErrorKind::EmptyValue);
assert!(err
.to_string()
.contains("The argument '-o <opt>' requires a value but none was supplied"));
}
#[test]
fn opt_user_override() {
let r = App::new("df")

View file

@ -402,7 +402,7 @@ fn issue_1047_min_zero_vals_default_val() {
.long("del")
.setting(ArgSettings::RequireEquals)
.min_values(0)
.default_value("default"),
.default_missing_value("default"),
)
.get_matches_from(vec!["foo", "-d"]);
assert_eq!(m.occurrences_of("del"), 1);