Module command_registry

Module command_registry 

Source
Expand description

Command registry implementation

This module provides the central registry for storing and retrieving command definitions and their associated handlers.

§Architecture

The registry maintains two main data structures:

  • A map of command names to their definitions and handlers
  • A map of aliases to canonical command names

This design allows O(1) lookup by both command name and alias.

§Example

use dynamic_cli::registry::CommandRegistry;
use dynamic_cli::config::schema::CommandDefinition;
use dynamic_cli::executor::CommandHandler;
use std::collections::HashMap;

// Create a registry
let mut registry = CommandRegistry::new();

// Define a command
let definition = CommandDefinition {
    name: "hello".to_string(),
    aliases: vec!["hi".to_string(), "greet".to_string()],
    description: "Say hello".to_string(),
    required: false,
    arguments: vec![],
    options: vec![],
    implementation: "hello_handler".to_string(),
};

// Create a handler
struct HelloCommand;
impl CommandHandler for HelloCommand {
    fn execute(
        &self,
        _ctx: &mut dyn dynamic_cli::context::ExecutionContext,
        _args: &HashMap<String, String>,
    ) -> dynamic_cli::Result<()> {
        println!("Hello!");
        Ok(())
    }
}

// Register the command
registry.register(definition, Box::new(HelloCommand))?;

// Retrieve by name
assert!(registry.get_handler("hello").is_some());

// Retrieve by alias
assert_eq!(registry.resolve_name("hi"), Some("hello"));

Structs§

CommandRegistry
Central registry for commands and their handlers