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 parse<T>(&self) -> Result<T, Error>
👎Deprecated since 0.3.0: please use then()
instead
pub fn parse<T>(&self) -> Result<T, Error>
then()
insteadParse the value of this argument.
Sourcepub fn parse_if_present<T>(&self) -> Result<Option<T>, Error>
👎Deprecated since 0.3.0: please use present_and_then()
instead
pub fn parse_if_present<T>(&self) -> Result<Option<T>, Error>
present_and_then()
insteadParse the value of this argument if it is present.
Sourcepub fn parse_with<F, T, E>(&self, f: F) -> Result<T, Error>
👎Deprecated since 0.3.0: please use then()
instead
pub fn parse_with<F, T, E>(&self, f: F) -> Result<T, Error>
then()
insteadSimilar to Arg::parse()
, but more flexible as this method allows you to specify an arbitrary parsing function.
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::MissingArg
ifself.is_present()
isfalse
(argument is missing) - Returns
Error::InvalidArg
iff(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()
.
Sourcepub fn raw_value(&self) -> Option<&str>
👎Deprecated since 0.3.0: please use present()
and value()
instead
pub fn raw_value(&self) -> Option<&str>
present()
and value()
insteadReturns the raw value of this argument.
Sourcepub fn raw_value_or_empty(&self) -> &str
👎Deprecated since 0.3.0: please use value()
instead
pub fn raw_value_or_empty(&self) -> &str
value()
insteadReturns the raw value of this argument, or an empty string if not present.
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