arg

Macro arg 

Source
macro_rules! arg {
    (@arg ($arg:expr) ) => { ... };
    (@arg ($arg:expr) --$long:ident $($tail:tt)*) => { ... };
    (@arg ($arg:expr) -($short:expr) $($tail:tt)*) => { ... };
    (@arg ($arg:expr) | ($alias:expr) $($tail:tt)*) => { ... };
    (@arg ($arg:expr) | $alias:ident $($tail:tt)*) => { ... };
    (@arg ($arg:expr) ... $($tail:tt)*) => { ... };
    (@arg ($arg:expr) =[$var:expr$(=>$default:expr)?] $($tail:tt)*) => { ... };
    (@arg ($arg:expr) !$ident:ident $($tail:tt)*) => { ... };
    (@arg ($arg:expr) $ident:ident $($tail:tt)*) => { ... };
    ($name:ident $($tail:tt)*) => { ... };
    (--($name:expr) $($tail:tt)*) => { ... };
    (--$name:ident $($tail:tt)*) => { ... };
}
Expand description

Makes declaring consistent arguments less verbose and less tedious.

The available syntax is:

  • --STRING or --("STRING-WITH-HYPHENS") will make an Arg where both the name and long are the same. Due to Rust syntax, if the argument should have hyphens, one must use --("foo-bar-baz")
  • -('f') sets the Short value. (Due to Rust syntax rules)
  • Visible aliases can be set with using | along with the similar Long value rules. I.e. |foo or |("foo-with-hyphens"). When combined the Long/name it actually looks good –foo|bar`, etc.
  • A value name can be set with =["STRING"] optionally also setting a default value =["STRING"=>"default"]
  • Setting multiple values can be done with ... Note that this sets multiple values/occurrences in a consistent manner for this application. If you need arguments with different semantics you’ll have to set those manually. ... is equivalent to setting Arg::new("foo").action(ArgAction::Append).number_of_values(1). value_delimiter(',')
  • Setting any boolean value to true can be done by just the function name i.e. required
  • Setting any boolean value to false can be done by prefixing the function with ! i.e. !required
arg!(--foo|foos =["NUM"=>"2"]... global !allow_hyphen_values);

// is equivalent to (with the macro syntax in the comment to the right)...
Arg::new("foo")                // --foo
  .long("foo")                 // --foo
  .visible_alias("foos")       // |foos
  .value_name("NUM")           // =["NUM"]
  .default_value("2")          // =[..=>"2"]
  .action(ArgAction::Append)   // ...
  .value_delimiter(',')        // ...
  .number_of_values(1)         // ...
  .global(true)                // global
  .allow_hyphen_values(false); // !allow_hyphen_values