Function bpaf::pure_with

source ·
pub fn pure_with<T, F, E>(val: F) -> ParsePureWith<T, F, E>where
    F: Fn() -> Result<T, E>,
    E: ToString,
Expand description

Wrap a calculated value into a Parser

This parser represents a possibly failing equivalent to pure. It produces T by invoking the provided callback without consuming anything from the command line, can be useful with construct!. As with any parsers T should be Clone and Debug.

Both pure and pure_with are designed to put values into structures, to generate fallback you should be using fallback and fallback_with.

See also pure for a pure computation that can’t fail.

Combinatoric usage

fn pair() -> impl Parser<bool> {
    let a = long("flag-a").switch();
    let b = pure_with::<_, _, String>(|| {
        // search for history file and try to fish out the last used value ...
        // if this computation fails - user will see it
        Ok(false)
    });
    construct!([a, b])
}