mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 14:22:34 +00:00
Merge pull request #3541 from plaflamme/fix-argenum-defaults
fix: multiple default values combined with `ArgEnum` / `PossibleValues`
This commit is contained in:
commit
8170754287
2 changed files with 59 additions and 9 deletions
|
@ -675,6 +675,19 @@ fn assert_defaults<'d>(
|
|||
for default_os in defaults {
|
||||
if let Some(default_s) = default_os.to_str() {
|
||||
if !arg.possible_vals.is_empty() {
|
||||
if let Some(delim) = arg.get_value_delimiter() {
|
||||
for part in default_s.split(delim) {
|
||||
assert!(
|
||||
arg.possible_vals.iter().any(|possible_val| {
|
||||
possible_val.matches(part, arg.is_ignore_case_set())
|
||||
}),
|
||||
"Argument `{}`'s {}={} doesn't match possible values",
|
||||
arg.name,
|
||||
field,
|
||||
part
|
||||
)
|
||||
}
|
||||
} else {
|
||||
assert!(
|
||||
arg.possible_vals.iter().any(|possible_val| {
|
||||
possible_val.matches(default_s, arg.is_ignore_case_set())
|
||||
|
@ -685,6 +698,7 @@ fn assert_defaults<'d>(
|
|||
default_s
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(validator) = arg.validator.as_ref() {
|
||||
let mut validator = validator.lock().unwrap();
|
||||
|
|
|
@ -457,3 +457,39 @@ fn option_vec_type() {
|
|||
);
|
||||
assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vec_type_default_value() {
|
||||
#[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
|
||||
enum ArgChoice {
|
||||
Foo,
|
||||
Bar,
|
||||
Baz,
|
||||
}
|
||||
|
||||
#[derive(Parser, PartialEq, Debug)]
|
||||
struct Opt {
|
||||
#[clap(
|
||||
arg_enum,
|
||||
short,
|
||||
long,
|
||||
default_value = "foo,bar",
|
||||
value_delimiter = ','
|
||||
)]
|
||||
arg: Vec<ArgChoice>,
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
Opt {
|
||||
arg: vec![ArgChoice::Foo, ArgChoice::Bar]
|
||||
},
|
||||
Opt::try_parse_from(&[""]).unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Opt {
|
||||
arg: vec![ArgChoice::Foo, ArgChoice::Baz]
|
||||
},
|
||||
Opt::try_parse_from(&["", "-a", "foo,baz"]).unwrap()
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue