Skip to content

Commit

Permalink
fix(macros): fixes broken pattern which prevented calling multi-argum…
Browse files Browse the repository at this point in the history
…ent Arg methods
  • Loading branch information
Peter Corlett committed Dec 13, 2018
1 parent 3294d18 commit 9e7a352
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,13 @@ macro_rules! clap_app {
(@arg ($arg:expr) $modes:tt $ident:ident[$($target:ident)*] $($tail:tt)*) => {
clap_app!{ @arg ($arg $( .$ident(stringify!($target)) )*) $modes $($tail)* }
};
// Inherit builder's functions
(@arg ($arg:expr) $modes:tt $ident:ident($($expr:expr)*) $($tail:tt)*) => {
clap_app!{ @arg ($arg.$ident($($expr)*)) $modes $($tail)* }
// Inherit builder's functions, e.g. `index(2)`, `requires_if("val", "arg")`
(@arg ($arg:expr) $modes:tt $ident:ident($($expr:expr),*) $($tail:tt)*) => {
clap_app!{ @arg ($arg.$ident($($expr),*)) $modes $($tail)* }
};
// Inherit builder's functions with trailing comma, e.g. `index(2,)`, `requires_if("val", "arg",)`
(@arg ($arg:expr) $modes:tt $ident:ident($($expr:expr,)*) $($tail:tt)*) => {
clap_app!{ @arg ($arg.$ident($($expr),*)) $modes $($tail)* }
};

// Build a subcommand outside of an app.
Expand Down
26 changes: 26 additions & 0 deletions tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,32 @@ fn group_macro_set_not_required() {
assert!(!matches.is_present("difficulty"));
}

#[test]
fn multiarg() {
let app = || clap_app!(
claptests =>
(@arg flag: --flag "value")
(@arg multiarg: --multiarg
default_value("flag-unset") default_value_if("flag", None, "flag-set")
"multiarg")
(@arg multiarg2: --multiarg2
default_value("flag-unset") default_value_if("flag", None, "flag-set",)
"multiarg2")
);

let matches = app()
.get_matches_from_safe(vec!["bin_name"])
.expect("match failed");
assert_eq!(matches.value_of("multiarg"), Some("flag-unset"));
assert_eq!(matches.value_of("multiarg2"), Some("flag-unset"));

let matches = app()
.get_matches_from_safe(vec!["bin_name", "--flag"])
.expect("match failed");
assert_eq!(matches.value_of("multiarg"), Some("flag-set"));
assert_eq!(matches.value_of("multiarg2"), Some("flag-set"));
}

#[test]
fn arg_enum() {
// Helper macros to avoid repetition
Expand Down

0 comments on commit 9e7a352

Please sign in to comment.