pub enum Arg {
Positional {
spec: ArgSpec,
metadata: Metadata,
index: usize,
value: String,
},
Default {
spec: ArgSpec,
metadata: Metadata,
},
Example {
spec: ArgSpec,
metadata: Metadata,
},
None {
spec: ArgSpec,
},
}Expand description
A positional argument.
Variants§
Implementations§
Source§impl Arg
impl Arg
Sourcepub fn is_present(&self) -> bool
pub fn is_present(&self) -> bool
Returns true if this argument has a value.
Sourcepub fn then<F, T, E>(self, f: F) -> Result<T, Error>
pub fn then<F, T, E>(self, f: F) -> Result<T, Error>
Applies additional conversion or validation to the argument.
This method allows for chaining transformations and validations when an argument is present. It first checks if the argument has a value and then applies the provided function.
§Examples
let mut args = noargs::RawArgs::new(["example", "42"].iter().map(|a| a.to_string()));
let arg = noargs::arg("<NUMBER>").take(&mut args);
// Parse as number and ensure it's positive
let num = arg.then(|arg| -> Result<_, Box<dyn std::error::Error>> {
let n: i32 = arg.value().parse()?;
if n <= 0 {
return Err("number must be positive".into());
}
Ok(n)
})?;§Errors
- Returns
Error::MissingArgifself.is_present()isfalse(argument is missing) - Returns
Error::InvalidArgiff(self)returnsErr(_)(validation or conversion failed)
Sourcepub fn present_and_then<F, T, E>(self, f: F) -> Result<Option<T>, Error>
pub fn present_and_then<F, T, E>(self, f: F) -> Result<Option<T>, Error>
Shorthand for self.present().map(|arg| arg.then(f)).transpose().
Trait Implementations§
impl Eq for Arg
impl StructuralPartialEq for Arg
Auto Trait Implementations§
impl Freeze for Arg
impl RefUnwindSafe for Arg
impl Send for Arg
impl Sync for Arg
impl Unpin for Arg
impl UnwindSafe for Arg
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