Expand description
Command dispatch and orchestration for clap-based CLIs.
standout-dispatch provides command routing, handler execution, and a hook
system for CLI applications. It orchestrates the execution flow while remaining
agnostic to rendering implementation.
§Architecture
Dispatch is an orchestration layer that manages this execution flow:
parsed CLI args
→ pre-dispatch hook (validation, setup)
→ logic handler (business logic → serializable data)
→ post-dispatch hook (data transformation)
→ render handler (view + data → string output)
→ post-output hook (output transformation)§Design Rationale
Dispatch deliberately does not own rendering or output format logic:
-
Logic handlers have a strict input signature (
&ArgMatches,&CommandContext) and return serializable data. They focus purely on business logic. -
Render handlers are pluggable callbacks provided by the consuming framework. They receive (view name, data) and return a formatted string. All rendering decisions (format, theme, template engine) live in the render handler.
This separation allows:
- Using dispatch without any rendering (just return data)
- Using dispatch with custom renderers (not just standout-render)
- Keeping format/theme/template logic out of the dispatch layer
§Render Handler Pattern
The render handler is a closure that captures rendering context:
// Framework (e.g., standout) creates the render handler at runtime
// after parsing CLI args to determine format
let format = extract_output_mode(&matches); // --output=json
let theme = &config.theme;
let render_handler = move |view: &str, data: &Value| {
// All format/theme knowledge lives here, not in dispatch
my_renderer::render(view, data, theme, format)
};
dispatcher.run_with_renderer(matches, render_handler);This pattern means dispatch calls render_handler(view, data) without knowing
what format, theme, or template engine is being used.
§Features
- Command routing: Extract command paths from clap
ArgMatches - Handler traits: Thread-safe (
Handler) and local (LocalHandler) variants - Hook system: Pre/post dispatch and post-output hooks for cross-cutting concerns
- Render abstraction: Pluggable render handlers via
RenderFn/LocalRenderFn
§Usage
§Standalone (no rendering framework)
use standout_dispatch::{Handler, Output, from_fn};
// Simple render handler that just serializes to JSON
let render = from_fn(|data, _| Ok(serde_json::to_string_pretty(data)?));
Dispatcher::builder()
.command("list", list_handler, render)
.build()?
.run(cmd, args);§With standout framework
The standout crate provides full integration with templates and themes:
use standout::{App, embed_templates};
App::builder()
.templates(embed_templates!("src/templates"))
.command("list", list_handler, "list") // template name
.build()?
.run(cmd, args);In this case, standout creates the render handler internally, injecting
the template registry, theme, and output format from CLI args.
Structs§
- Command
Context - Context passed to command handlers.
- FnHandler
- A wrapper that implements Handler for closures.
- Hook
Error - Error returned by a hook.
- Hooks
- Per-command hook configuration.
- Local
FnHandler - A wrapper that implements LocalHandler for FnMut closures.
Enums§
- Hook
Phase - The phase at which a hook error occurred.
- Output
- What a handler produces.
- Render
Error - Errors that can occur during rendering.
- Rendered
Output - Output from a command, used in post-output hooks.
- RunResult
- Result of running the CLI dispatcher.
Traits§
- Handler
- Trait for thread-safe command handlers.
- Local
Handler - Trait for local (single-threaded) command handlers.
Functions§
- extract_
command_ path - Extracts the command path from ArgMatches by following the subcommand chain.
- from_fn
- Creates a render function from a closure.
- from_
fn_ mut - Creates a local render function from a FnMut closure.
- get_
deepest_ matches - Gets the deepest subcommand matches.
- has_
subcommand - Returns true if the matches contain a subcommand (excluding “help”).
- insert_
default_ command - Inserts a command name at position 1 (after program name) in the argument list.
- path_
to_ string - Converts a command path vector to a dot-separated string.
- string_
to_ path - Parses a dot-separated command path string into a vector.
Type Aliases§
- Handler
Result - The result type for command handlers.
- Local
Render Fn - A local (non-Send) render function for single-threaded use.
- Post
Dispatch Fn - Type alias for post-dispatch hook functions.
- Post
Output Fn - Type alias for post-output hook functions.
- PreDispatch
Fn - Type alias for pre-dispatch hook functions.
- Render
Fn - The render function signature.