Struct bpaf::Parser

source · []
pub struct Parser<T> { /* private fields */ }
Expand description

Simple or composed argument parser

Implementations

Wrap a value into a Parser

Parser will produce T without consuming anything from the command line, can be useful with construct!.

let a = long("flag-a").switch();
let b = Parser::pure(42u32);
let t: Parser<(bool, u32)> = construct!(a, b);

If first parser fails - try the second one

let a = short('a').switch();
let b = short('b').switch();

// Parser will accept either `-a` or `-b` on a command line but not both at once.
let a_or_b: Parser<bool> = a.or_else(b);
Performance

If first parser succeeds - second one will be called anyway to produce a better error message for combinations of mutually exclusive parsers:

Suppose program accepts one of two mutually exclusive switches -a and -b and both are present error message should point at the second flag

Fail with a fixed error message

let a = short('a').switch();
let no_a = Parser::fail("Custom error message for missing -a");

// Parser will produce a custom error message if `-a` is not specified
let a_: Parser<bool> = a.or_else(no_a);

Consume zero or more items from a command line

// parser will accept multiple `-n` arguments:
// `-n 1, -n 2, -n 3`
// and return all of them as a vector which can be empty if no `-n` specified
let n: Parser<Vec<u32>> = short('n').argument("NUM").from_str::<u32>().many();
Panics

Panics if parser succeeds without consuming any input: any parser modified with many must consume something,

Validate or fail with a message

let n = short('n').argument("NUM").from_str::<u32>();
// Parser will reject values greater than 10
let n = n.guard(|v| *v <= 10, "Values greater than 10 are only available in the DLC pack!");

Consume one or more items from a command line

// parser will accept multiple `-n` arguments:
// `-n 1, -n 2, -n 3`
// and return all of them as a vector. At least one `-n` argument is required.
let n: Parser<Vec<u32>> = short('n').argument("NUM").from_str::<u32>().some();

Turn a required parser into optional

let n: Parser<u32> = short('n').argument("NUM").from_str();
// if `-n` is not specified - parser will return `None`
let n: Parser<Option<u32>> = n.optional();

Apply a pure transformation to a contained value

let n: Parser<u32> = short('n').argument("NUM").from_str();
// produced value is now twice as large
let n = n.map(|v| v * 2);

Apply a failing transformation

See also from_str

let s: Parser<String> = short('n').argument("NUM");
// Try to parse String into u32 or fail during the parsing
use std::str::FromStr;
let n = s.map(|s| u32::from_str(&s));

Use this value as default if value is not present on a command line

Would still fail if value is present but failure comes from some transformation

let n = short('n').argument("NUM").from_str::<u32>().fallback(42);

Use value produced by this function as default if value is not present

Would still fail if value is present but failure comes from some transformation

let n = short('n').argument("NUM").from_str::<u32>();
let n = n.fallback_with(|| Result::<u32, String>::Ok(42));

Parse T or fallback to T::default()

let n = short('n').argument("NUM").from_str::<u32>().default();

Attach help message to a complex parser

let width = short('w').argument("PX").from_str::<u32>();
let height = short('h').argument("PX").from_str::<u32>();
let rect = construct!(width, height).group_help("take a rectangle");

See examples/rectangle.rs for a complete example

Parse stored String using FromStr instance

let speed = short('s').argument("SPEED").from_str::<f64>();
// at this point program would accept things like "-s 3.1415"
// but reject "-s pi"

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.