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: StringThe primary alias of the command that was invoked.
is_help: boolWhether 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
impl CliRequest
Sourcepub fn require_params(&self, num: usize) -> Result<(), CliError>
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(())
}Sourcepub fn require_flag(&self, flag: &str) -> Result<(), CliError>
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(())
}Sourcepub fn validate_flag(
&self,
flag: &str,
format: CliFormat,
) -> Result<(), CliError>
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 validateformat- 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(())
}Sourcepub fn validate_params(&self, formats: Vec<CliFormat>) -> Result<(), CliError>
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§
impl Freeze for CliRequest
impl RefUnwindSafe for CliRequest
impl Send for CliRequest
impl Sync for CliRequest
impl Unpin for CliRequest
impl UnsafeUnpin for CliRequest
impl UnwindSafe for CliRequest
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