clap_nested_commands/
async_commands.rs1#[macro_export]
2macro_rules! generate_async_commands {
3 ($($(#[$attr:meta])* $module:ident),*) => (
5 generate_async_commands!(return_type = (); $($(#[$attr])* $module),*);
6 );
7
8 (return_type = $return_type:ty; $($(#[$attr:meta])* $module:ident),*) => (
10 paste::paste! {
11 #[derive(Debug, Subcommand)]
12 pub enum Commands {
13 $(
14 $(#[$attr])*
15 [<$module:camel>]($module::Command),
16 )*
17 }
18
19 pub async fn execute(cli_context: &CliContext, cmd: Command) -> Result<$return_type, anyhow::Error> {
20 match cmd.command {
21 $(
22 Commands::[<$module:camel>](cmd) => $module::execute(cli_context, cmd).await,
23 )*
24 }
25 }
26 }
27 );
28
29 (default_command = $default:ident; $($(#[$attr:meta])* $module:ident),*) => (
31 generate_async_commands!(return_type = (), default_command = $default; $($(#[$attr])* $module),*);
32 );
33
34 (return_type = $return_type:ty, default_command = $default:ident; $($(#[$attr:meta])* $module:ident),*) => (
36 paste::paste! {
37 #[derive(Debug, Subcommand)]
38 pub enum Commands {
39 $(
40 $(#[$attr])*
41 [<$module:camel>]($module::Command),
42 )*
43 }
44
45 pub async fn execute(cli_context: &CliContext, cmd: Command) -> Result<$return_type, anyhow::Error> {
46 match cmd.command {
47 $(
48 Some(Commands::[<$module:camel>](cmd)) => $module::execute(cli_context, cmd).await,
49 )*
50 None => $default::execute(cli_context, $default::Command::default()).await,
51 }
52 }
53 }
54 );
55}