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
selfand returnSelf - 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
impl CliBuilder
Sourcepub fn config_file<P: Into<PathBuf>>(self, path: P) -> Self
pub fn config_file<P: Into<PathBuf>>(self, path: P) -> Self
Sourcepub fn config(self, config: CommandsConfig) -> Self
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);Sourcepub fn context(self, context: Box<dyn ExecutionContext>) -> Self
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 implementingExecutionContext
§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()));Sourcepub fn register_handler(
self,
name: impl Into<String>,
handler: Box<dyn CommandHandler>,
) -> Self
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 configurationhandler- Boxed command handler implementingCommandHandler
§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));Sourcepub fn build(self) -> Result<CliApp>
pub fn build(self) -> Result<CliApp>
Build the application
Performs the following steps:
- Load configuration (if
config_file()was used) - Validate that a context was provided
- Create the command registry
- Register all command handlers
- Verify that all required commands have handlers
- 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