Crate dynamic_cli

Crate dynamic_cli 

Source
Expand description

§dynamic-cli

A framework for creating configurable CLI and REPL applications via YAML/JSON.

§Overview

dynamic-cli allows you to define your application’s command-line interface in a configuration file rather than coding it manually. The framework automatically generates:

  • Argument parsing
  • Input validation
  • Contextual help
  • Interactive mode (REPL)
  • Error handling with suggestions

§Quick Start

use dynamic_cli::prelude::*;
use std::collections::HashMap;

// 1. Define the execution context
#[derive(Default)]
struct MyContext;

impl ExecutionContext for MyContext {
    fn as_any(&self) -> &dyn std::any::Any { self }
    fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
}

// 2. Implement a command handler
struct HelloCommand;

impl CommandHandler for HelloCommand {
    fn execute(
        &self,
        _context: &mut dyn ExecutionContext,
        args: &HashMap<String, String>,
    ) -> dynamic_cli::Result<()> {
        let default_name = "World".to_string();
        let name = args.get("name").unwrap_or(&default_name);
        println!("Hello, {}!", name);
        Ok(())
    }
}

// 3. Load configuration and register commands
use dynamic_cli::config::loader::load_config;

let config = load_config("commands.yaml")?;
let mut registry = CommandRegistry::new();
registry.register(config.commands[0].clone(), Box::new(HelloCommand))?;

// 4. Parse and execute
let parser = ReplParser::new(&registry);
let parsed = parser.parse_line("hello World")?;

let mut context = MyContext::default();
let handler = registry.get_handler(&parsed.command_name).unwrap();
handler.execute(&mut context, &parsed.arguments)?;

§Architecture

The framework is organized into modules:

  • error: Error types and handling
  • config: Configuration file loading and validation
  • context: Execution context trait
  • executor: Command execution
  • registry: Command and handler registry
  • parser: CLI and REPL argument parsing
  • validator: Argument validation

§Module Status

  • ✅ Complete: error, config, context, executor, registry, parser, validator, interface, builder
  • 📋 Planned: utils, examples

§Examples

See the documentation for each module for detailed examples.

Re-exports§

pub use context::downcast_mut;
pub use context::downcast_ref;
pub use context::ExecutionContext;
pub use executor::CommandHandler;
pub use error::DynamicCliError;
pub use error::Result;
pub use config::schema::ArgumentDefinition;
pub use config::schema::ArgumentType;
pub use config::schema::CommandDefinition;
pub use config::schema::CommandsConfig;
pub use config::schema::Metadata;
pub use config::schema::OptionDefinition;
pub use config::schema::ValidationRule;
pub use registry::CommandRegistry;
pub use parser::CliParser;
pub use parser::ParsedCommand;
pub use parser::ReplParser;
pub use validator::validate_file_exists;
pub use validator::validate_file_extension;
pub use validator::validate_range;
pub use interface::CliInterface;
pub use interface::ReplInterface;
pub use builder::CliApp;
pub use builder::CliBuilder;
pub use utils::detect_type;
pub use utils::format_bytes;
pub use utils::format_duration;
pub use utils::get_extension;
pub use utils::has_extension;
pub use utils::is_blank;
pub use utils::normalize;
pub use utils::normalize_path;
pub use utils::parse_bool;
pub use utils::parse_float;
pub use utils::parse_int;
pub use utils::truncate;

Modules§

builder
Fluent builder API for creating CLI/REPL applications
config
Configuration module
context
Execution context module
error
Error handling for dynamic-cli
executor
Command execution module
interface
User interface module
parser
Command-line and REPL parsing
prelude
Prelude module for quickly importing essential types
registry
Command registry module
utils
Utility functions for dynamic-cli
validator
Argument validation module