Trait parkour::FromInput [−][src]
Trait for extracting information from the command-line input. This is implemented for flags, positional and named arguments, subcommands, etc.
Implementation
// The struct we want to crate from a positional number argument struct Foo(usize); // Information that is available while parsing. When `even` is true, // we only accept even numbers. Otherwise we only accept odd numbers. struct FooCtx { even: bool, } impl FromInput for Foo { type Context = FooCtx; fn from_input<P: Parse>(input: &mut P, context: &FooCtx) -> Result<Self, parkour::Error> { let num: usize = input.parse_value(&Default::default())?; if context.even && num % 2 != 0 { Err(parkour::Error::unexpected_value(num, "even number")) } else if !context.even && num % 2 == 0 { Err(parkour::Error::unexpected_value(num, "odd number")) } else { Ok(Foo(num)) } } }
Associated Types
Loading content...Required methods
fn from_input<P: Parse>(
input: &mut P,
context: &Self::Context
) -> Result<Self, Error>
[src]
input: &mut P,
context: &Self::Context
) -> Result<Self, Error>
Extract information from the command-line input.
Provided methods
fn try_from_input<P: Parse>(
input: &mut P,
context: &Self::Context
) -> Result<Option<Self>, Error>
[src]
input: &mut P,
context: &Self::Context
) -> Result<Option<Self>, Error>
Extract information from the command-line input, but convert
Error::no_value
to Option::None
. This is useful when you want to
bubble up all errors except for Error::no_value
:
if let Some(value) = bool::try_from_input(input, &Flag::Short("b").into())? { // do something with value }