Expand description
Command handler trait and related types
This module defines the core trait that all command implementations must implement.
The trait is designed to be object-safe, meaning it can be used as a trait object
(&dyn CommandHandler), which is critical for dynamic command registration.
§Design Principles
§Object Safety
The CommandHandler trait is intentionally kept simple and object-safe:
- No generic methods (would prevent trait object usage)
- No associated types with type parameters
- All methods use concrete types or trait objects
This allows the registry to store handlers as Box<dyn CommandHandler>,
enabling dynamic command registration at runtime.
§Simple Type Signatures
Arguments are passed as crate::parser::ParsedArgs rather than generic
types. This design choice:
- Maintains object safety
- Represents both scalar and repeatable-option values (DD-024)
- Delegates type parsing to the parser module
§Thread Safety
All handlers must be Send + Sync to support:
- Shared access across threads
- Potential async execution in the future
- Safe usage in multi-threaded contexts
§Example
use dynamic_cli::executor::{CommandHandler, ParsedArgs};
use dynamic_cli::context::ExecutionContext;
use dynamic_cli::Result;
// Define a simple command handler
struct HelloCommand;
impl CommandHandler for HelloCommand {
fn execute(
&self,
_context: &mut dyn ExecutionContext,
args: &ParsedArgs,
) -> Result<()> {
let name = args.get_scalar("name").unwrap_or("World");
println!("Hello, {}!", name);
Ok(())
}
}Traits§
- Async
Command Handler - Async counterpart of
CommandHandler. - Command
Handler - Trait for command implementations