pub struct CommandRegistry { /* private fields */ }Expand description
Central registry for commands and their handlers
The registry stores all registered commands along with their definitions and handlers. It provides efficient lookup by both command name and alias.
§Thread Safety
The registry is designed to be constructed once during application startup
and then shared immutably across the application. For multi-threaded access,
wrap it in Arc<CommandRegistry>.
§Example
use dynamic_cli::registry::CommandRegistry;
use dynamic_cli::config::schema::CommandDefinition;
use dynamic_cli::executor::CommandHandler;
use std::collections::HashMap;
let mut registry = CommandRegistry::new();
// Register commands during initialization
registry.register(definition, Box::new(TestCommand))?;
// Use throughout the application
if let Some(handler) = registry.get_handler("test") {
// Execute the command
}Implementations§
Source§impl CommandRegistry
impl CommandRegistry
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty registry
§Example
use dynamic_cli::registry::CommandRegistry;
let registry = CommandRegistry::new();
assert_eq!(registry.list_commands().len(), 0);Sourcepub fn register(
&mut self,
definition: CommandDefinition,
handler: Box<dyn CommandHandler>,
) -> Result<()>
pub fn register( &mut self, definition: CommandDefinition, handler: Box<dyn CommandHandler>, ) -> Result<()>
Register a command with its handler
This method registers a command definition along with its handler. It also registers all aliases for the command.
§Arguments
definition- The command definition from the configurationhandler- The handler implementation for this command
§Returns
Ok(())if registration succeedsErr(RegistryError)if:- A command with the same name is already registered
- An alias conflicts with an existing command or alias
§Errors
RegistryError::DuplicateRegistrationif the command name already existsRegistryError::DuplicateAliasif an alias is already in use
§Example
use dynamic_cli::registry::CommandRegistry;
use dynamic_cli::config::schema::CommandDefinition;
use dynamic_cli::executor::CommandHandler;
use std::collections::HashMap;
let mut registry = CommandRegistry::new();
let definition = CommandDefinition {
name: "simulate".to_string(),
aliases: vec!["sim".to_string(), "run".to_string()],
description: "Run simulation".to_string(),
required: false,
arguments: vec![],
options: vec![],
implementation: "sim_handler".to_string(),
};
struct SimCommand;
impl CommandHandler for SimCommand {
fn execute(
&self,
_: &mut dyn dynamic_cli::context::ExecutionContext,
_: &HashMap<String, String>,
) -> dynamic_cli::Result<()> {
Ok(())
}
}
// Register the command
registry.register(definition, Box::new(SimCommand))?;
// Can now access by name or alias
assert!(registry.get_handler("simulate").is_some());
assert_eq!(registry.resolve_name("sim"), Some("simulate"));Sourcepub fn resolve_name(&self, name: &str) -> Option<&str>
pub fn resolve_name(&self, name: &str) -> Option<&str>
Resolve a name (command or alias) to the canonical command name
This method checks if the given name is either:
- A registered command name (returns the name itself)
- An alias (returns the canonical command name)
§Arguments
name- The name or alias to resolve
§Returns
Some(&str)- The canonical command nameNone- If the name is not registered
§Example
use dynamic_cli::registry::CommandRegistry;
let mut registry = CommandRegistry::new();
// Resolve command name
assert_eq!(registry.resolve_name("hello"), Some("hello"));
// Resolve alias
assert_eq!(registry.resolve_name("hi"), Some("hello"));
// Unknown name
assert_eq!(registry.resolve_name("unknown"), None);Sourcepub fn get_definition(&self, name: &str) -> Option<&CommandDefinition>
pub fn get_definition(&self, name: &str) -> Option<&CommandDefinition>
Get the definition of a command by name or alias
§Arguments
name- The command name or alias
§Returns
Some(&CommandDefinition)if the command existsNoneif the command is not registered
§Example
// Get by name
if let Some(def) = registry.get_definition("test") {
assert_eq!(def.name, "test");
assert_eq!(def.description, "Test command");
}
// Get by alias
if let Some(def) = registry.get_definition("t") {
assert_eq!(def.name, "test");
}Sourcepub fn get_handler(&self, name: &str) -> Option<&Box<dyn CommandHandler>>
pub fn get_handler(&self, name: &str) -> Option<&Box<dyn CommandHandler>>
Get the handler of a command by name or alias
This is the primary method used during command execution to retrieve the handler that will execute the command.
§Arguments
name- The command name or alias
§Returns
Some(&Box<dyn CommandHandler>)if the command existsNoneif the command is not registered
§Example
// Get handler by name
if let Some(handler) = registry.get_handler("exec") {
// Use handler for execution
}
// Get handler by alias
if let Some(handler) = registry.get_handler("x") {
// Same handler
}Sourcepub fn list_commands(&self) -> Vec<&CommandDefinition>
pub fn list_commands(&self) -> Vec<&CommandDefinition>
List all registered command definitions
Returns a vector of references to all command definitions in the registry. The order is not guaranteed.
§Returns
Vector of command definition references
§Example
let commands = registry.list_commands();
assert_eq!(commands.len(), 2);
// Use for help text, command completion, etc.
for cmd in commands {
println!("{}: {}", cmd.name, cmd.description);
}Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of registered commands
§Example
use dynamic_cli::registry::CommandRegistry;
let registry = CommandRegistry::new();
assert_eq!(registry.len(), 0);