Merge pull request #40 from epage/derive_vec

fix(derive): Define multiple policy for Special Types
This commit is contained in:
Ed Page 2021-11-29 13:33:05 -06:00 committed by GitHub
commit c7aa471deb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 14 deletions

View file

@ -302,8 +302,7 @@ pub fn gen_augment(
Ty::OptionVec => quote_spanned! { ty.span()=> Ty::OptionVec => quote_spanned! { ty.span()=>
.takes_value(true) .takes_value(true)
.value_name(#value_name) .value_name(#value_name)
.multiple_values(true) .multiple_occurrences(true)
.min_values(0)
#possible_values #possible_values
#validator #validator
#allow_invalid_utf8 #allow_invalid_utf8
@ -313,7 +312,7 @@ pub fn gen_augment(
quote_spanned! { ty.span()=> quote_spanned! { ty.span()=>
.takes_value(true) .takes_value(true)
.value_name(#value_name) .value_name(#value_name)
.multiple_values(true) .multiple_occurrences(true)
#possible_values #possible_values
#validator #validator
#allow_invalid_utf8 #allow_invalid_utf8
@ -326,7 +325,6 @@ pub fn gen_augment(
Ty::Other if flag => quote_spanned! { ty.span()=> Ty::Other if flag => quote_spanned! { ty.span()=>
.takes_value(false) .takes_value(false)
.multiple_values(false)
}, },
Ty::Other => { Ty::Other => {

View file

@ -419,7 +419,7 @@ fn vec_type() {
Opt { Opt {
arg: vec![ArgChoice::Foo, ArgChoice::Bar] arg: vec![ArgChoice::Foo, ArgChoice::Bar]
}, },
Opt::try_parse_from(&["", "-a", "foo", "bar"]).unwrap() Opt::try_parse_from(&["", "-a", "foo", "-a", "bar"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err()); assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err());
} }
@ -439,10 +439,6 @@ fn option_vec_type() {
} }
assert_eq!(Opt { arg: None }, Opt::try_parse_from(&[""]).unwrap()); assert_eq!(Opt { arg: None }, Opt::try_parse_from(&[""]).unwrap());
assert_eq!(
Opt { arg: Some(vec![]) },
Opt::try_parse_from(&["", "-a"]).unwrap()
);
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(vec![ArgChoice::Foo]) arg: Some(vec![ArgChoice::Foo])
@ -453,7 +449,7 @@ fn option_vec_type() {
Opt { Opt {
arg: Some(vec![ArgChoice::Foo, ArgChoice::Bar]) arg: Some(vec![ArgChoice::Foo, ArgChoice::Bar])
}, },
Opt::try_parse_from(&["", "-a", "foo", "bar"]).unwrap() Opt::try_parse_from(&["", "-a", "foo", "-a", "bar"]).unwrap()
); );
assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err()); assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err());
} }

View file

@ -259,7 +259,7 @@ fn two_option_option_types() {
} }
#[test] #[test]
fn vec_type_is_multiple_values() { fn vec_type_is_multiple_occurrences() {
#[derive(Parser, PartialEq, Debug)] #[derive(Parser, PartialEq, Debug)]
struct Opt { struct Opt {
#[clap(short, long)] #[clap(short, long)]
@ -270,6 +270,42 @@ fn vec_type_is_multiple_values() {
Opt::try_parse_from(&["test", "-a24"]).unwrap() Opt::try_parse_from(&["test", "-a24"]).unwrap()
); );
assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from(&["test"]).unwrap()); assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!(
Opt { arg: vec![24, 42] },
Opt::try_parse_from(&["test", "-a", "24", "-a", "42"]).unwrap()
);
}
#[test]
fn vec_type_with_required() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(short, long, required = true)]
arg: Vec<i32>,
}
assert_eq!(
Opt { arg: vec![24] },
Opt::try_parse_from(&["test", "-a24"]).unwrap()
);
assert!(Opt::try_parse_from(&["test"]).is_err());
assert_eq!(
Opt { arg: vec![24, 42] },
Opt::try_parse_from(&["test", "-a", "24", "-a", "42"]).unwrap()
);
}
#[test]
fn vec_type_with_multiple_values_only() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(short, long, multiple_values(true), multiple_occurrences(false))]
arg: Vec<i32>,
}
assert_eq!(
Opt { arg: vec![24] },
Opt::try_parse_from(&["test", "-a24"]).unwrap()
);
assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from(&["test"]).unwrap());
assert_eq!( assert_eq!(
Opt { arg: vec![24, 42] }, Opt { arg: vec![24, 42] },
Opt::try_parse_from(&["test", "-a", "24", "42"]).unwrap() Opt::try_parse_from(&["test", "-a", "24", "42"]).unwrap()
@ -308,6 +344,28 @@ fn option_vec_type() {
Opt::try_parse_from(&["test", "-a", "1"]).unwrap() Opt::try_parse_from(&["test", "-a", "1"]).unwrap()
); );
assert_eq!(
Opt {
arg: Some(vec![1, 2])
},
Opt::try_parse_from(&["test", "-a", "1", "-a", "2"]).unwrap()
);
assert_eq!(Opt { arg: None }, Opt::try_parse_from(&["test"]).unwrap());
}
#[test]
fn option_vec_type_structopt_behavior() {
#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[clap(short, long, multiple_values(true), min_values(0))]
arg: Option<Vec<i32>>,
}
assert_eq!(
Opt { arg: Some(vec![1]) },
Opt::try_parse_from(&["test", "-a", "1"]).unwrap()
);
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(vec![1, 2]) arg: Some(vec![1, 2])
@ -337,9 +395,9 @@ fn two_option_vec_types() {
assert_eq!( assert_eq!(
Opt { Opt {
arg: Some(vec![1]), arg: Some(vec![1]),
b: Some(vec![]) b: None,
}, },
Opt::try_parse_from(&["test", "-a", "1", "-b"]).unwrap() Opt::try_parse_from(&["test", "-a", "1"]).unwrap()
); );
assert_eq!( assert_eq!(
@ -355,7 +413,7 @@ fn two_option_vec_types() {
arg: Some(vec![1, 2]), arg: Some(vec![1, 2]),
b: Some(vec![1, 2]) b: Some(vec![1, 2])
}, },
Opt::try_parse_from(&["test", "-a", "1", "2", "-b", "1", "2"]).unwrap() Opt::try_parse_from(&["test", "-a", "1", "-a", "2", "-b", "1", "-b", "2"]).unwrap()
); );
assert_eq!( assert_eq!(