mirror of
https://github.com/clap-rs/clap
synced 2024-11-15 09:07:10 +00:00
fix(builder): Don't double-has arg id in default_value_ifs_os
(#3815)
This commit is contained in:
parent
8bb3853eb5
commit
3eacf5b8b0
2 changed files with 43 additions and 2 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue