Skip to main content

dispatch

Macro dispatch 

Source
macro_rules! dispatch {
    { $($tokens:tt)* } => { ... };
}
Expand description

Declarative macro for defining command dispatch tables.

The macro expands to a closure that configures a [GroupBuilder] with the specified commands and groups.

§Syntax

dispatch! {
    // Simple command (template from convention)
    command_name => handler_fn,

    // Command with options
    command_name => {
        handler: handler_fn,
        template: "template.j2",           // optional
        pre_dispatch: hook_fn,             // optional
        post_dispatch: hook_fn,            // optional
        post_output: hook_fn,              // optional
    },

    // Nested group
    group_name: {
        // commands and nested groups...
    },
}

§Example

use standout::cli::{dispatch, App, HandlerResult, Output};
use serde_json::json;

fn migrate_handler(_m: &clap::ArgMatches, _ctx: &CommandContext) -> HandlerResult<serde_json::Value> {
    Ok(Output::Render(json!({"migrated": true})))
}

fn backup_handler(_m: &clap::ArgMatches, _ctx: &CommandContext) -> HandlerResult<serde_json::Value> {
    Ok(Output::Render(json!({"backed_up": true})))
}

let builder = App::builder()
    .template_dir("templates")
    .commands(dispatch! {
        db: {
            migrate => migrate_handler,
            backup => {
                handler: backup_handler,
                template: "db/backup_custom.j2",
            },
        },
        version => |_m, _ctx| Ok(Output::Render(json!({"version": "1.0.0"}))),
    });