CliBuilder

Struct CliBuilder 

Source
pub struct CliBuilder { /* private fields */ }
Expand description

Fluent builder for creating CLI/REPL applications

Provides a chainable API for configuring and building applications. Automatically loads configuration, registers handlers, and creates the appropriate interface (CLI or REPL).

§Builder Pattern

The builder follows the standard Rust builder pattern:

  • Methods consume self and return Self
  • Final build() method consumes the builder and returns the app

§Example

use dynamic_cli::prelude::*;

let app = CliBuilder::new()
    .config_file("commands.yaml")
    .context(Box::new(MyContext::default()))
    .register_handler("my_handler", Box::new(MyHandler))
    .prompt("myapp")
    .build()?;

Implementations§

Source§

impl CliBuilder

Source

pub fn new() -> Self

Create a new builder

§Example
use dynamic_cli::CliBuilder;

let builder = CliBuilder::new();
Source

pub fn config_file<P: Into<PathBuf>>(self, path: P) -> Self

Specify the configuration file

The file will be loaded during build(). Supports YAML and JSON formats.

§Arguments
  • path - Path to the configuration file (.yaml, .yml, or .json)
§Example
use dynamic_cli::CliBuilder;

let builder = CliBuilder::new()
    .config_file("commands.yaml");
Source

pub fn config(self, config: CommandsConfig) -> Self

Provide a pre-loaded configuration

Use this instead of config_file() if you want to load and potentially modify the configuration before building.

§Arguments
  • config - Loaded and validated configuration
§Example
use dynamic_cli::{CliBuilder, config::loader::load_config};

let mut config = load_config("commands.yaml")?;
// Modify config if needed...

let builder = CliBuilder::new()
    .config(config);
Source

pub fn context(self, context: Box<dyn ExecutionContext>) -> Self

Set the execution context

The context will be passed to all command handlers and can store application state.

§Arguments
  • context - Boxed execution context implementing ExecutionContext
§Example
use dynamic_cli::prelude::*;

#[derive(Default)]
struct MyContext {
    count: u32,
}

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 }
}

let builder = CliBuilder::new()
    .context(Box::new(MyContext::default()));
Source

pub fn register_handler( self, name: impl Into<String>, handler: Box<dyn CommandHandler>, ) -> Self

Register a command handler

Associates a handler with the command’s implementation name from the config. The name must match the implementation field in the command definition.

§Arguments
  • name - Implementation name from the configuration
  • handler - Boxed command handler implementing CommandHandler
§Example
use dynamic_cli::prelude::*;
use std::collections::HashMap;

struct MyCommand;

impl CommandHandler for MyCommand {
    fn execute(
        &self,
        _ctx: &mut dyn ExecutionContext,
        _args: &HashMap<String, String>,
    ) -> dynamic_cli::Result<()> {
        println!("Executed!");
        Ok(())
    }
}

let builder = CliBuilder::new()
    .register_handler("my_command", Box::new(MyCommand));
Source

pub fn prompt(self, prompt: impl Into<String>) -> Self

Set the REPL prompt

Only used in REPL mode. If not specified, uses the prompt from the configuration or defaults to “cli”.

§Arguments
  • prompt - Prompt prefix (e.g., “myapp” displays as “myapp > “)
§Example
use dynamic_cli::CliBuilder;

let builder = CliBuilder::new()
    .prompt("myapp");
Source

pub fn build(self) -> Result<CliApp>

Build the application

Performs the following steps:

  1. Load configuration (if config_file() was used)
  2. Validate that a context was provided
  3. Create the command registry
  4. Register all command handlers
  5. Verify that all required commands have handlers
  6. Create the CliApp
§Returns

A configured CliApp ready to run

§Errors
  • Configuration errors (file not found, invalid format, etc.)
  • Missing context
  • Missing required handlers
  • Registry errors
§Example
use dynamic_cli::prelude::*;

let app = CliBuilder::new()
    .config_file("commands.yaml")
    .context(Box::new(MyContext::default()))
    .register_handler("handler", Box::new(MyHandler))
    .build()?;

// Now app is ready to run

Trait Implementations§

Source§

impl Default for CliBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.