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());Sourcepub fn run_script(
self,
path: impl AsRef<Path>,
policy: ScriptErrorPolicy,
) -> Result<ScriptOutcome>
pub fn run_script( self, path: impl AsRef<Path>, policy: ScriptErrorPolicy, ) -> Result<ScriptOutcome>
Run a batch of command lines read from a file (#41).
Each non-blank, non-comment (#-prefixed) line is tokenized the
same quote-aware way as a typed REPL line (via
ReplParser::tokenize), then dispatched through the exact same
resolve → parse → execute path as run — no
duplicate parsing logic, and repeatable options (DD-024) are fully
preserved since dispatch goes through parse_typed() either way.
§Error policy
policy decides what happens when a line fails:
ScriptErrorPolicy::Abort: stop immediately, returningErrfor the failing line. Lines before it have already run.ScriptErrorPolicy::Continue: record the failure and proceed to the next line. The method still returnsOk, with every failure listed in the returnedScriptOutcome.
Every failure — whether it aborts the run or not — is reported
with its 1-based line number, wrapped in
ExecutionError::CommandFailed
(reusing the existing error hierarchy; no new enum variant, so no
breaking change to ExecutionError’s non-#[non_exhaustive]
shape).
§Example
use dynamic_cli::interface::{CliInterface, ScriptErrorPolicy};
use dynamic_cli::prelude::*;
let registry = CommandRegistry::new();
let context = Box::new(MyContext::default());
let cli = CliInterface::new(registry, context);
let outcome = cli.run_script("commands.txt", ScriptErrorPolicy::Continue)?;
println!("{}/{} lines succeeded", outcome.lines_succeeded, outcome.lines_executed);Auto Trait Implementations§
impl !RefUnwindSafe for CliInterface
impl !UnwindSafe for CliInterface
impl Freeze for CliInterface
impl Send for CliInterface
impl Sync for CliInterface
impl Unpin for CliInterface
impl UnsafeUnpin 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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more