From 79225d3a2d520456d458ed10ecef3d3a8bd2e06f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 7 Nov 2022 06:07:48 -0600 Subject: [PATCH] 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. --- clap_derive/src/derives/args.rs | 2 +- tests/derive/flags.rs | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/clap_derive/src/derives/args.rs b/clap_derive/src/derives/args.rs index 15f676be..ad2a6463 100644 --- a/clap_derive/src/derives/args.rs +++ b/clap_derive/src/derives/args.rs @@ -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 } } diff --git a/tests/derive/flags.rs b/tests/derive/flags.rs index 63fb216a..12eb7d51 100644 --- a/tests/derive/flags.rs +++ b/tests/derive/flags.rs @@ -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() + ); +}