pub struct OptionParser<T> { /* private fields */ }
Expand description

Parser with atteched meta information

Implementations

Return current help message for outer parser as a string

Execute the OptionParser, extract a parsed value or print some diagnostic and exit

let verbose = short('v').req_flag(()).many().map(|xs|xs.len());
let info = Info::default().descr("Takes verbosity flag and does nothing else");

let opt = info.for_parser(verbose).run();
// At this point `opt` contains number of repetitions of `-v` on a command line

Execute the OptionParser and produce a value that can be used in unit tests

#[test]
fn positional_argument() {
    let p = positional("FILE").help("File to process");
    let parser = Info::default().for_parser(p);

    let help = parser
        .run_inner(Args::from(&["--help"]))
        .unwrap_err()
        .unwrap_stdout();
    let expected_help = "\
Usage: <FILE>

Available options:
    -h, --help   Prints help information
";
    assert_eq!(expected_help, help);
}

See also Args and it’s From impls to produce input and ParseFailure::unwrap_stderr / ParseFailure::unwrap_stdout for processing results.

Errors

If parser can’t produce desired outcome run_inner will return ParseFailure which represents runtime behavior: one branch to print something to stdout and exit with success and the other branch to print something to stderr and exit with failure.

Parser is not really capturing anything. If parser detects --help or --version it will always produce something that can be consumed with ParseFailure::unwrap_stdout. Otherwise it will produce ParseFailure::unwrap_stderr generated either by the parser itself in case someone required field is missing or by user’s Parser::guard or Parser::parse functions.

API for those is constructed to only produce a String. If you try to print something inside Parser::map or Parser::parse - it will not be captured. Depending on a test case you’ll know what to use: unwrap_stdout if you want to test generated help or unwrap_stderr if you are testing parse / guard / missing parameters.

Exact string reperentations may change between versions including minor releases.

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

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.