fix(builder): Don't double-has arg id in default_value_ifs_os (#3815)

This commit is contained in:
梦想实现家 2022-06-13 20:39:50 +08:00 committed by GitHub
parent 8bb3853eb5
commit 3eacf5b8b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View file

@ -3307,7 +3307,7 @@ impl<'help> Arg<'help> {
ifs: &[(T, Option<&'help OsStr>, Option<&'help OsStr>)],
) -> Self {
for (arg, val, default) in ifs {
self = self.default_value_if_os(arg.key(), *val, *default);
self = self.default_value_if_os(arg, *val, *default);
}
self
}

View file

@ -1,5 +1,7 @@
#![allow(deprecated)]
use clap::{Arg, ArgSettings};
use clap::{Arg, ArgSettings, Command};
use std::ffi::OsStr;
#[test]
fn setting() {
@ -42,3 +44,42 @@ fn unset_setting_bitor() {
assert!(!m.is_hide_set(), "{:#?}", m);
assert!(!m.is_last_set(), "{:#?}", m);
}
#[test]
fn default_value_ifs_os() {
let cmd = Command::new("my_cargo")
.arg(
Arg::new("flag")
.long("flag")
.allow_invalid_utf8(true)
.takes_value(true),
)
.arg(
Arg::new("other")
.long("other")
.allow_invalid_utf8(true)
.default_value_ifs_os(&[(
"flag",
Some("标记2").map(OsStr::new),
Some("flag=标记2").map(OsStr::new),
)]),
);
let result = cmd.try_get_matches_from([
OsStr::new("my_cargo"),
OsStr::new("--flag"),
OsStr::new("标记2"),
]);
assert!(result.is_ok());
match result {
Ok(arg_matches) => {
assert_eq!(arg_matches.value_of_os("flag"), Some(OsStr::new("标记2")));
assert_eq!(
arg_matches.value_of_os("other"),
Some(OsStr::new("flag=标记2")),
);
}
Err(e) => {
println!("{}", e.to_string());
}
}
}