Struct bpaf::parsers::ParseFallbackWith
source · pub struct ParseFallbackWith<T, P, F, E> { /* private fields */ }
Expand description
Parser that substitutes missing value with a function results but not parser
failure, created with fallback_with
.
Implementations§
source§impl<P, T: Display, F, E> ParseFallbackWith<T, P, F, E>
impl<P, T: Display, F, E> ParseFallbackWith<T, P, F, E>
sourcepub fn display_fallback(self) -> Self
pub fn display_fallback(self) -> Self
Show fallback_with
value in --help
using Display
representation
If fallback function fails - no value will show up
Combinatoric example
fn try_to_get_version() -> Result<usize, &'static str> {
Ok(42)
}
#[derive(Debug, Clone)]
pub struct Options {
version: usize,
}
pub fn options() -> OptionParser<Options> {
let version = long("version")
.help("Specify protocol version")
.argument("VERS")
.fallback_with(try_to_get_version)
.display_fallback();
construct!(Options { version }).to_options()
}
fn main() {
println!("{:?}", options().run())
}
Derive example
fn try_to_get_version() -> Result<usize, &'static str> {
Ok(42)
}
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub struct Options {
#[bpaf(argument("VERS"), fallback_with(try_to_get_version), display_fallback)]
/// Specify protocol version
version: usize,
}
fn main() {
println!("{:?}", options().run())
}
Output
fallback_with
changes parser to fallback to a value that comes from a potentially failing
computation when argument is not specified
Options { version: 42 }
If value is present - fallback value is ignored
Options { version: 10 }
Parsing errors are preserved and preserved to user
Error: couldn't parse ten: invalid digit found in string
bpaf
encases parsers with fallback value of some sort in usage with []
Usage: app [--version=VERS]
- --version=VERS
- Specify protocol version
- [default: 42]
- -h, --help
- Prints help information
source§impl<P, T: Debug, F, E> ParseFallbackWith<T, P, F, E>
impl<P, T: Debug, F, E> ParseFallbackWith<T, P, F, E>
sourcepub fn debug_fallback(self) -> Self
pub fn debug_fallback(self) -> Self
Show fallback_with
value in --help
using Debug
representation
If fallback function fails - no value will show up
Combinatoric example
#[derive(Debug, Clone)]
pub struct Options {
jobs: usize,
}
pub fn options() -> OptionParser<Options> {
let jobs = long("jobs")
.help("Number of jobs")
.argument("JOBS")
.fallback(42)
.debug_fallback();
construct!(Options { jobs }).to_options()
}
fn main() {
println!("{:?}", options().run())
}
Derive example
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
#[allow(dead_code)]
pub struct Options {
/// Number of jobs
#[bpaf(argument("JOBS"), fallback(42), debug_fallback)]
jobs: usize,
}
fn main() {
println!("{:?}", options().run())
}
Output
fallback
changes parser to fallback to a default value used when argument is not specified
Options { jobs: 42 }
If value is present - fallback value is ignored
Options { jobs: 10 }
Parsing errors are preserved and preserved to user
Error: couldn't parse ten: invalid digit found in string
With display_fallback
and
debug_fallback
you can make it so default value
is visible in --help
output
Usage: app [--jobs=JOBS]
- --jobs=JOBS
- Number of jobs
- [default: 42]
- -h, --help
- Prints help information
Trait Implementations§
source§impl<T, P, F, E> Parser<T> for ParseFallbackWith<T, P, F, E>
impl<T, P, F, E> Parser<T> for ParseFallbackWith<T, P, F, E>
source§fn collect<C>(self) -> ParseCollect<Self, C, T>where
C: FromIterator<T>,
Self: Sized,
fn collect<C>(self) -> ParseCollect<Self, C, T>where
C: FromIterator<T>,
Self: Sized,
source§fn optional(self) -> ParseOptional<Self>
fn optional(self) -> ParseOptional<Self>
source§fn count(self) -> ParseCount<Self, T>
fn count(self) -> ParseCount<Self, T>
source§fn last(self) -> ParseLast<Self>
fn last(self) -> ParseLast<Self>
source§fn parse<F, R, E>(self, f: F) -> ParseWith<T, Self, F, E, R>
fn parse<F, R, E>(self, f: F) -> ParseWith<T, Self, F, E, R>
source§fn map<F, R>(self, map: F) -> ParseMap<T, Self, F, R>
fn map<F, R>(self, map: F) -> ParseMap<T, Self, F, R>
source§fn guard<F>(self, check: F, message: &'static str) -> ParseGuard<Self, F>
fn guard<F>(self, check: F, message: &'static str) -> ParseGuard<Self, F>
source§fn fallback(self, value: T) -> ParseFallback<Self, T>
fn fallback(self, value: T) -> ParseFallback<Self, T>
source§fn fallback_with<F, E>(self, fallback: F) -> ParseFallbackWith<T, Self, F, E>
fn fallback_with<F, E>(self, fallback: F) -> ParseFallbackWith<T, Self, F, E>
source§fn hide(self) -> ParseHide<Self>
fn hide(self) -> ParseHide<Self>
source§fn hide_usage(self) -> ParseUsage<Self>
fn hide_usage(self) -> ParseUsage<Self>
source§fn custom_usage<M>(self, usage: M) -> ParseUsage<Self>
fn custom_usage<M>(self, usage: M) -> ParseUsage<Self>
source§fn group_help<M: Into<Doc>>(self, message: M) -> ParseGroupHelp<Self>
fn group_help<M: Into<Doc>>(self, message: M) -> ParseGroupHelp<Self>
source§fn with_group_help<F>(self, f: F) -> ParseWithGroupHelp<Self, F>
fn with_group_help<F>(self, f: F) -> ParseWithGroupHelp<Self, F>
source§fn complete<M, F>(self, op: F) -> ParseComp<Self, F>
fn complete<M, F>(self, op: F) -> ParseComp<Self, F>
autocomplete
only.source§fn complete_shell(self, op: ShellComp) -> ParseCompShell<Self>
fn complete_shell(self, op: ShellComp) -> ParseCompShell<Self>
autocomplete
only.