Skip to content

Commit

Permalink
Merge pull request #4459 from epage/unit
Browse files Browse the repository at this point in the history
fix(derive): Allow defaulted value parser for '()' fields
  • Loading branch information
epage committed Nov 7, 2022
2 parents 520145e + 79225d3 commit 816841f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clap_derive/src/derives/args.rs
Expand Up @@ -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
}
}
Expand Down
51 changes: 51 additions & 0 deletions tests/derive/flags.rs
Expand Up @@ -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()
);
}

0 comments on commit 816841f

Please sign in to comment.