From 9895b671cff784f35cf56abcd8270f7c2ba09699 Mon Sep 17 00:00:00 2001 From: Arnavion Date: Mon, 12 Dec 2016 22:42:11 -0800 Subject: [PATCH] feat(clap_app!): Support `("some app name")` syntax for defining app names Used for setting names that aren't Rust idents. Fixes #759 --- src/macros.rs | 4 ++++ tests/macros.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/macros.rs b/src/macros.rs index 9478857cb0a..55ef976917c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -535,6 +535,10 @@ macro_rules! clap_app { clap_app!{ @app ($crate::SubCommand::with_name(stringify!($name))) $($tail)* } }; // Start the magic + (($name:expr) => $($tail:tt)*) => {{ + clap_app!{ @app ($crate::App::new($name)) $($tail)*} + }}; + ($name:ident => $($tail:tt)*) => {{ clap_app!{ @app ($crate::App::new(stringify!($name))) $($tail)*} }}; diff --git a/tests/macros.rs b/tests/macros.rs index bd2bfdc9258..a68f6282705 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -33,3 +33,43 @@ fn basic() { (@arg scpositional: index(1) "tests positionals")) ); } + +#[test] +fn quoted_app_name() { + let app = clap_app!(("app name with spaces-and-hyphens") => + (version: "0.1") + (about: "tests clap library") + (author: "Kevin K. ") + (@arg opt: -o --option +takes_value ... "tests options") + (@arg positional: index(1) "tests positionals") + (@arg flag: -f --flag ... +global "tests flags") + (@arg flag2: -F conflicts_with[flag] requires[option2] + "tests flags with exclusions") + (@arg option2: --long_option_2 conflicts_with[option] requires[positional2] + "tests long options with exclusions") + (@arg positional2: index(2) "tests positionals with exclusions") + (@arg option3: -O --Option +takes_value possible_value[fast slow] + "tests options with specific value sets") + (@arg positional3: index(3) ... possible_value[vi emacs] + "tests positionals with specific values") + (@arg multvals: --multvals +takes_value value_name[one two] + "Tests mutliple values, not mult occs") + (@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two] + "Tests mutliple values, not mult occs") + (@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals") + (@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals") + (@subcommand subcmd => + (about: "tests subcommands") + (version: "0.1") + (author: "Kevin K. ") + (@arg scoption: -o --option ... +takes_value "tests options") + (@arg scpositional: index(1) "tests positionals")) + ); + + assert_eq!(app.p.meta.name, "app name with spaces-and-hyphens"); + + let mut help_text = vec![]; + app.write_help(&mut help_text).expect("Could not write help text."); + let help_text = String::from_utf8(help_text).expect("Help text is not valid utf-8"); + assert!(help_text.starts_with("app name with spaces-and-hyphens 0.1\n")); +}