clap_nested_commands/
async_commands.rs#[macro_export]
macro_rules! generate_async_commands {
($($(#[$attr:meta])* $module:ident),*) => (
generate_async_commands!(return_type = (); $($(#[$attr])* $module),*);
);
(return_type = $return_type:ty; $($(#[$attr:meta])* $module:ident),*) => (
paste::paste! {
#[derive(Debug, Subcommand)]
pub enum Commands {
$(
$(#[$attr])*
[<$module:camel>]($module::Command),
)*
}
pub async fn execute(cli_context: &CliContext, cmd: Command) -> Result<$return_type, anyhow::Error> {
match cmd.command {
$(
Commands::[<$module:camel>](cmd) => $module::execute(cli_context, cmd).await,
)*
}
}
}
);
(default_command = $default:ident; $($(#[$attr:meta])* $module:ident),*) => (
generate_async_commands!(return_type = (), default_command = $default; $($(#[$attr])* $module),*);
);
(return_type = $return_type:ty, default_command = $default:ident; $($(#[$attr:meta])* $module:ident),*) => (
paste::paste! {
#[derive(Debug, Subcommand)]
pub enum Commands {
$(
$(#[$attr])*
[<$module:camel>]($module::Command),
)*
}
pub async fn execute(cli_context: &CliContext, cmd: Command) -> Result<$return_type, anyhow::Error> {
match cmd.command {
$(
Some(Commands::[<$module:camel>](cmd)) => $module::execute(cli_context, cmd).await,
)*
None => $default::execute(cli_context, $default::Command::default()).await,
}
}
}
);
}