pub struct CliInterface { /* private fields */ }Expand description
CLI (Command-Line Interface) handler
Provides a simple interface for executing commands from command-line arguments. The CLI parses arguments, executes the command, and exits.
§Architecture
Command-line args → CliParser → CommandExecutor → Handler
↓
ExecutionContext§Error Handling
Errors are displayed to stderr with colored formatting (if enabled) and the process exits with appropriate exit codes:
0: Success1: Execution error2: Argument parsing error3: Other errors
Implementations§
Source§impl CliInterface
impl CliInterface
Sourcepub fn new(
registry: CommandRegistry,
context: Box<dyn ExecutionContext>,
) -> Self
pub fn new( registry: CommandRegistry, context: Box<dyn ExecutionContext>, ) -> Self
Create a new CLI interface
§Arguments
registry- Command registry with all registered commandscontext- Execution context (will be consumed by the interface)
§Example
use dynamic_cli::interface::CliInterface;
use dynamic_cli::prelude::*;
let registry = CommandRegistry::new();
let context = Box::new(MyContext::default());
let cli = CliInterface::new(registry, context);Sourcepub fn run(self, args: Vec<String>) -> Result<()>
pub fn run(self, args: Vec<String>) -> Result<()>
Run the CLI with provided arguments
Parses the arguments, executes the corresponding command, and handles errors.
This method consumes self as the CLI typically runs once and exits.
§Arguments
args- Command-line arguments (typically fromenv::args().skip(1))
§Returns
Ok(())on successErr(DynamicCliError)on any error (parsing, validation, execution)
§Exit Codes
The caller should handle errors and exit with appropriate codes:
- Parse errors → exit code 2
- Execution errors → exit code 1
- Other errors → exit code 3
§Example
use dynamic_cli::interface::CliInterface;
use dynamic_cli::prelude::*;
use std::process;
let registry = CommandRegistry::new();
let context = Box::new(MyContext::default());
let cli = CliInterface::new(registry, context);
if let Err(e) = cli.run(std::env::args().skip(1).collect()) {
eprintln!("Error: {}", e);
process::exit(1);
}Sourcepub fn run_and_exit(self, args: Vec<String>) -> !
pub fn run_and_exit(self, args: Vec<String>) -> !
Run the CLI with automatic error handling and exit
This is a convenience method that:
- Runs the CLI with provided arguments
- Handles errors by displaying them to stderr
- Exits the process with appropriate exit code
This method never returns.
§Arguments
args- Command-line arguments
§Example
use dynamic_cli::interface::CliInterface;
use dynamic_cli::prelude::*;
let registry = CommandRegistry::new();
let context = Box::new(MyContext::default());
let cli = CliInterface::new(registry, context);
// This will handle errors and exit automatically
cli.run_and_exit(std::env::args().skip(1).collect());Auto Trait Implementations§
impl Freeze for CliInterface
impl !RefUnwindSafe for CliInterface
impl Send for CliInterface
impl Sync for CliInterface
impl Unpin for CliInterface
impl !UnwindSafe for CliInterface
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more