CliInterface

Struct CliInterface 

Source
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: Success
  • 1: Execution error
  • 2: Argument parsing error
  • 3: Other errors

Implementations§

Source§

impl CliInterface

Source

pub fn new( registry: CommandRegistry, context: Box<dyn ExecutionContext>, ) -> Self

Create a new CLI interface

§Arguments
  • registry - Command registry with all registered commands
  • context - 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);
Source

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 from env::args().skip(1))
§Returns
  • Ok(()) on success
  • Err(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);
}
Source

pub fn run_and_exit(self, args: Vec<String>) -> !

Run the CLI with automatic error handling and exit

This is a convenience method that:

  1. Runs the CLI with provided arguments
  2. Handles errors by displaying them to stderr
  3. 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§

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.