Trait parkour::FromInput[][src]

pub trait FromInput: Sized {
    type Context;
    fn from_input<P: Parse>(
        input: &mut P,
        context: &Self::Context
    ) -> Result<Self, Error>; fn try_from_input<P: Parse>(
        input: &mut P,
        context: &Self::Context
    ) -> Result<Option<Self>, Error> { ... } }

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

type Context[src]

Information that is available while parsing

Loading content...

Required methods

fn from_input<P: Parse>(
    input: &mut P,
    context: &Self::Context
) -> Result<Self, Error>
[src]

Extract information from the command-line input.

Loading content...

Provided methods

fn try_from_input<P: Parse>(
    input: &mut P,
    context: &Self::Context
) -> Result<Option<Self>, Error>
[src]

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
}
Loading content...

Implementors

impl<T: FromInputValue> FromInput for T[src]

type Context = ArgCtx<'static, T::Context>

Loading content...