pub enum Opt {
Long {
spec: OptSpec,
metadata: Metadata,
index: usize,
value: String,
},
Short {
spec: OptSpec,
metadata: Metadata,
index: usize,
value: String,
},
Env {
spec: OptSpec,
metadata: Metadata,
value: String,
},
Default {
spec: OptSpec,
metadata: Metadata,
},
Example {
spec: OptSpec,
metadata: Metadata,
},
MissingValue {
spec: OptSpec,
long: bool,
},
None {
spec: OptSpec,
},
}Expand description
A named argument with value.
Variants§
Implementations§
Source§impl Opt
impl Opt
Sourcepub fn is_present(&self) -> bool
pub fn is_present(&self) -> bool
Returns true if this option is present.
Sourcepub fn is_value_present(&self) -> bool
pub fn is_value_present(&self) -> bool
Returns true if this option is present and 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 option.
This method allows for chaining transformations and validations when an option is present. It first checks if the option has a value and then applies the provided function.
§Examples
let mut args = noargs::RawArgs::new(["example", "--num=42"].iter().map(|a| a.to_string()));
let opt = noargs::opt("num").take(&mut args);
// Parse as number and ensure it's positive
let num = opt.then(|opt| -> Result<_, Box<dyn std::error::Error>> {
let n: i32 = opt.value().parse()?;
if n <= 0 {
return Err("number must be positive".into());
}
Ok(n)
})?;§Errors
- Returns
Error::MissingOptifself.is_value_present()isfalse(option is missing) - Returns
Error::InvalidOptiff(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(|opt| opt.then(f)).transpose().
Trait Implementations§
impl Eq for Opt
impl StructuralPartialEq for Opt
Auto Trait Implementations§
impl Freeze for Opt
impl RefUnwindSafe for Opt
impl Send for Opt
impl Sync for Opt
impl Unpin for Opt
impl UnwindSafe for Opt
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