fix(derive): Allow defaulted value parser for '()' fields

Inspired by #4458

This is a compatible change because it turns a compiler error into a
working case.
This commit is contained in:
Ed Page 2022-11-07 06:07:48 -06:00
parent 520145ee67
commit 79225d3a2d
2 changed files with 52 additions and 1 deletions

View file

@ -233,9 +233,9 @@ pub fn gen_augment(
let implicit_methods = match **ty {
Ty::Unit => {
// Leaving out `value_parser` as it will always fail
quote_spanned! { ty.span()=>
.value_name(#value_name)
#value_parser
#action
}
}

View file

@ -281,3 +281,54 @@ fn override_implicit_from_flag_positional() {
Opt::try_parse_from(&["test", "true"]).unwrap()
);
}
#[test]
fn unit_for_negation() {
#[derive(Parser, PartialEq, Eq, Debug)]
struct Opt {
#[arg(long)]
arg: bool,
#[arg(long, action = ArgAction::SetTrue, overrides_with = "arg")]
no_arg: (),
}
assert_eq!(
Opt {
arg: false,
no_arg: ()
},
Opt::try_parse_from(&["test"]).unwrap()
);
assert_eq!(
Opt {
arg: true,
no_arg: ()
},
Opt::try_parse_from(&["test", "--arg"]).unwrap()
);
assert_eq!(
Opt {
arg: false,
no_arg: ()
},
Opt::try_parse_from(&["test", "--no-arg"]).unwrap()
);
assert_eq!(
Opt {
arg: true,
no_arg: ()
},
Opt::try_parse_from(&["test", "--no-arg", "--arg"]).unwrap()
);
assert_eq!(
Opt {
arg: false,
no_arg: ()
},
Opt::try_parse_from(&["test", "--arg", "--no-arg"]).unwrap()
);
}