CliRequest

Struct CliRequest 

Source
pub struct CliRequest {
    pub cmd_alias: String,
    pub is_help: bool,
    pub args: Vec<String>,
    pub flags: Vec<String>,
    pub flag_values: HashMap<String, String>,
    pub shortcuts: Vec<String>,
}
Expand description

Represents a parsed CLI command request.

This struct contains all the parsed information from a command line invocation, including the command name, arguments, flags, and their values.

Fields§

§cmd_alias: String

The primary alias of the command that was invoked.

§is_help: bool

Whether help was requested for this command.

§args: Vec<String>

Positional arguments passed to the command.

§flags: Vec<String>

Boolean flags that were provided (e.g., -v, --verbose).

§flag_values: HashMap<String, String>

Flags with associated values (e.g., --output file.txt).

§shortcuts: Vec<String>

List of shortcut aliases for this command.

Implementations§

Source§

impl CliRequest

Source

pub fn require_params(&self, num: usize) -> Result<(), CliError>

Ensures that at least the specified number of parameters were provided.

§Arguments
  • num - The minimum number of required parameters
§Returns

Returns Ok(()) if enough parameters were provided, or CliError::MissingParams otherwise.

§Example
fn process(&self, req: &CliRequest) -> anyhow::Result<()> {
    req.require_params(2)?;  // Require at least 2 parameters
    let source = &req.args[0];
    let dest = &req.args[1];
    // ... process command
    Ok(())
}
Source

pub fn require_flag(&self, flag: &str) -> Result<(), CliError>

Ensures that the specified flag was provided.

§Arguments
  • flag - The name of the required flag
§Returns

Returns Ok(()) if the flag is present, or CliError::MissingFlag otherwise.

§Example
fn process(&self, req: &CliRequest) -> anyhow::Result<()> {
    req.require_flag("--output")?;  // Require --output flag
    let output = req.get_flag("--output").unwrap();
    // ... process command
    Ok(())
}
Source

pub fn get_flag(&self, flag: &str) -> Option<String>

Gets the value of a flag if it was provided.

§Arguments
  • flag - The name of the flag
§Returns

Returns Some(String) with the flag’s value, or None if the flag wasn’t provided.

§Example
if let Some(output) = req.get_flag("--output") {
    println!("Output file: {}", output);
}
Source

pub fn validate_flag( &self, flag: &str, format: CliFormat, ) -> Result<(), CliError>

Validates that a flag’s value conforms to the specified format.

§Arguments
  • flag - The name of the flag to validate
  • format - The format validator to apply
§Returns

Returns Ok(()) if the flag value is valid, or a CliError describing the issue.

§Example
fn process(&self, req: &CliRequest) -> anyhow::Result<()> {
    req.validate_flag("--port", CliFormat::Integer)?;
    // Now we know --port contains a valid integer
    Ok(())
}
Source

pub fn has_flag(&self, flag: &str) -> bool

Checks if a flag was provided.

§Arguments
  • flag - The name of the flag to check
§Returns

Returns true if the flag is present, false otherwise.

§Example
if req.has_flag("--verbose") {
    println!("Verbose mode enabled");
}
Source

pub fn validate_params(&self, formats: Vec<CliFormat>) -> Result<(), CliError>

Validates that all parameters conform to the specified formats.

§Arguments
  • formats - A vector of format validators, one for each parameter position
§Returns

Returns Ok(()) if all parameters are valid, or a CliError for the first invalid parameter.

§Example
fn process(&self, req: &CliRequest) -> anyhow::Result<()> {
    req.validate_params(vec![
        CliFormat::File,           // First arg must be a file
        CliFormat::IntegerRange(1..100),  // Second arg: 1-99
    ])?;
    // Now we know the parameters are valid
    Ok(())
}

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,