Struct bpaf::OptionParser
source · [−]pub struct OptionParser<T> { /* private fields */ }
Expand description
Parser with atteched meta information
Implementations
sourceimpl<T> OptionParser<T>
impl<T> OptionParser<T>
sourcepub fn render_help(&self) -> Result<String, Error>
pub fn render_help(&self) -> Result<String, Error>
Return current help message for outer parser as a string
sourceimpl<T> OptionParser<T>
impl<T> OptionParser<T>
sourcepub fn run(self) -> T
pub fn run(self) -> T
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
sourcepub fn run_inner(self, args: Args) -> Result<T, ParseFailure>
pub fn run_inner(self, args: Args) -> Result<T, ParseFailure>
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
sourceimpl<T: Clone> Clone for OptionParser<T>
impl<T: Clone> Clone for OptionParser<T>
sourcefn clone(&self) -> OptionParser<T>
fn clone(&self) -> OptionParser<T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl<T> !RefUnwindSafe for OptionParser<T>
impl<T> !Send for OptionParser<T>
impl<T> !Sync for OptionParser<T>
impl<T> Unpin for OptionParser<T>
impl<T> !UnwindSafe for OptionParser<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more