Macro app

Source
macro_rules! app {
    (=> $($rest:tt)*) => { ... };
    ($command_name:ident => $($rest:tt)*) => { ... };
    ($command_name:expr => $($rest:tt)*) => { ... };
    (@command ($builder:expr)) => { ... };
    (@command ($builder:expr) (description => $description:expr) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (usage => $usage:expr) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (help => $help:expr) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (version => $version:expr) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (hidden => $hidden:expr) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (handler ($options:ident, $arguments:ident) => $block:block) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (handler ($options:ident, $arguments:ident) => $expr:expr) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (handler (...$($arg_name:ident: $arg_type:ty),+) => $block:block) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (handler (...$($arg_name:ident: $arg_type:ty),+) => $expr:expr) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (handler ($($name:ident : $ty:ty),+ $(,...$($arg_name:ident: $arg_type:ty),+)?) => $block:block) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (handler ($($name:ident : $ty:ty),+ $(,...$($arg_name:ident: $arg_type:ty),+)?) => $expr:expr) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (handler () => $block:block) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (handler () => $expr:expr) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (handler => $block:block) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (handler => $expr:expr) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (@subcommand $command_name:ident $(=> $($rest:tt)+)?) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (@subcommand $command_name:expr $(=> $($rest:tt)+)?) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (@option $option_name:ident $(=> $($rest:tt)+)?) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (@option $option_name:expr $(=> $($rest:tt)+)?) $($tt:tt)*) => { ... };
    (@option ($option_builder:expr)) => { ... };
    (@option ($option_builder:expr) (@arg $arg_name:ident $(=> $($rest:tt)+)?) $($tt:tt)*) => { ... };
    (@option ($option_builder:expr) (@arg $arg_name:expr $(=> $($rest:tt)+)?) $($tt:tt)*) => { ... };
    (@option ($option_builder:expr) (@arg $(=> $($rest:tt)+)?) $($tt:tt)*) => { ... };
    (@option ($option_builder:expr) (description => $description:expr) $($tt:tt)*) => { ... };
    (@option ($option_builder:expr) (required => $required:expr) $($tt:tt)*) => { ... };
    (@option ($option_builder:expr) (multiple => $multiple:expr) $($tt:tt)*) => { ... };
    (@option ($option_builder:expr) (hidden => $hidden:expr) $($tt:tt)*) => { ... };
    (@option ($option_builder:expr) (requires_assign => $requires_assign:expr) $($tt:tt)*) => { ... };
    (@option ($option_builder:expr) (alias => $($alias:expr),+) $($tt:tt)*) => { ... };
    (@option ($option_builder:expr) (alias => $($literal:expr),+) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (@arg $arg_name:ident $(=> $($rest:tt)+)?) $($tt:tt)*) => { ... };
    (@command ($builder:expr) (@arg $arg_name:expr $(=> $($rest:tt)+)?) $($tt:tt)*) => { ... };
    (@arg ($arg_builder:expr)) => { ... };
    (@arg ($arg_builder:expr) (count => $count:expr) $($tt:tt)*) => { ... };
    (@arg ($arg_builder:expr) (description => $description:expr) $($tt:tt)*) => { ... };
    (@arg ($arg_builder:expr) (values => $($valid_values:expr),+) $($tt:tt)*) => { ... };
    (@arg ($arg_builder:expr) (default => $($default_values:expr),+) $($tt:tt)*) => { ... };
    (@arg ($arg_builder:expr) (validator => $validator:expr) $($tt:tt)*) => { ... };
    (@arg ($arg_builder:expr) (type => $ty:ty) $($tt:tt)*) => { ... };
    (@arg ($arg_builder:expr) (error => $error:expr) $($tt:tt)*) => { ... };
    (@@command => $($rest:tt)+) => { ... };
    (@@command $command_name:ident => $($rest:tt)+) => { ... };
    (@@command $command_name:expr => $($rest:tt)+) => { ... };
}
Expand description

Constructs a CommandLine app.

You use the @subcommand, @option and @arg tags to create subcommand, option and args respectively. A list of the tags and its properties:

  • @subcommand : description, usage, help, handler, hidden, @subcommand, @option and @arg.
  • @option : description, alias, required, multiple, requires_assign and @arg.
  • @arg : description, values, default, count, validator and type,

§Usage

To create the app start with:

  • clapi::app! { => ... }
  • clapi::app! { AppName => ... }
  • clapi::app! { "AppName" => ... }

This is the root of the app where all the tags and properties are declared, these are declared as (property => value).

  • For example:
clapi::app! { MyApp =>
    (description => "This is an app")
};

And the tags like @subcommand, @option and @arg, must contain a name either as an identifier or string literal for example:

  • (@subcommand version => ...)
  • (@option "enable" => ...)

Each tag contains its own properties, check the list above.

§Example

clapi::app!{ MyApp =>
    (description => "App to sum values")
    (usage => "USAGE: \n command [--times] <values...>")
    (@arg values =>
        (count => 1..)
        (type => i64)
    )
    (@option times =>
        (description => "Number of times to sum the values")
        (@arg =>
            (type => u64)
            (default => 1)
        )
    )
    (handler (times: u64, ...values: Vec<i64>) => {
        let times = times as i64;
        let total : i64 = values.iter().sum();
        println!("{}", total * times);
    })
};