Skip to content

Commit

Permalink
feat(clap_app!): Support --("some-arg-name") syntax for defining lo…
Browse files Browse the repository at this point in the history
…ng arg names

Used for arg names that aren't idents.

Ref #321
  • Loading branch information
Arnavion committed Dec 13, 2016
1 parent 9895b67 commit f41ec96
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/18_builder_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn main() {
(author: "Alice")
(about: "Does awesome things")
(@arg config: -c --config <conf> #{1, 2} {file_exists} "Sets a custom config file")
(@arg proxyHostname: --("proxy-hostname") +takes_value "Sets the hostname of the proxy to use")
(@arg input: * "Input file")
(@group test =>
(@attributes +required)
Expand Down
3 changes: 3 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ macro_rules! clap_app {
// No more tokens to munch
(@arg ($arg:expr) $modes:tt) => { $arg };
// Shorthand tokens influenced by the usage_string
(@arg ($arg:expr) $modes:tt --($long:expr) $($tail:tt)*) => {
clap_app!{ @arg ($arg.long($long)) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt --$long:ident $($tail:tt)*) => {
clap_app!{ @arg ($arg.long(stringify!($long))) $modes $($tail)* }
};
Expand Down
38 changes: 38 additions & 0 deletions tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,41 @@ fn quoted_app_name() {
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"));
}

#[test]
fn quoted_arg_long_name() {
let app = clap_app!(claptests =>
(version: "0.1")
(about: "tests clap library")
(author: "Kevin K. <[email protected]>")
(@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. <[email protected]>")
(@arg scoption: -o --option ... +takes_value "tests options")
(@arg scpositional: index(1) "tests positionals"))
);

assert_eq!(app.p.long_list[2], "long-option-2");

let matches = app.get_matches_from_safe(vec!["bin_name", "value1", "value2", "--long-option-2"]).expect("Expected to successfully match the given args.");
assert!(matches.is_present("option2"));
}

0 comments on commit f41ec96

Please sign in to comment.