pub struct ParseFallback<P, T> {
pub(crate) inner: P,
pub(crate) value: T,
pub(crate) value_str: String,
}Expand description
Parser that substitutes missing value but not parse failure, created with
fallback.
Fields§
§inner: P§value: T§value_str: StringImplementations§
Source§impl<P, T: Display> ParseFallback<P, T>
impl<P, T: Display> ParseFallback<P, T>
Sourcepub fn display_fallback(self) -> Self
pub fn display_fallback(self) -> Self
Show fallback value in --help using Display
representation
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)
.display_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), display_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 presented to the user
Error: couldn't parse ten: invalid digit found in string
With display_fallback,
debug_fallback, and
format_fallback, you can make it so the default value
is visible in the --help output.
Usage: app [--jobs=JOBS]
- --jobs=JOBS
- Number of jobs
- [default: 42]
- -h, --help
- Prints help information
Source§impl<P, T: Debug> ParseFallback<P, T>
impl<P, T: Debug> ParseFallback<P, T>
Sourcepub fn debug_fallback(self) -> Self
pub fn debug_fallback(self) -> Self
Show fallback value in --help using Debug
representation
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)
.debug_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), debug_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 presented to the 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> ParseFallback<P, T>
impl<P, T> ParseFallback<P, T>
Sourcepub fn format_fallback(
self,
format: impl Fn(&T, &mut Formatter<'_>) -> Result,
) -> Self
pub fn format_fallback( self, format: impl Fn(&T, &mut Formatter<'_>) -> Result, ) -> Self
Show fallback value in --help using the provided formatting
function.
Combinatoric example
use std::{fmt::Display as _, path::PathBuf};
#[derive(Debug, Clone)]
pub struct Options {
log_file: PathBuf,
}
pub fn options() -> OptionParser<Options> {
let log_file = long("log-file")
.help("Path to log file")
.argument::<PathBuf>("FILE")
.guard(
|log_file| !log_file.is_dir(),
"The log file can't be a directory",
)
.fallback(PathBuf::from("logfile.txt"))
.format_fallback(|path, f| path.display().fmt(f));
construct!(Options { log_file }).to_options()
}
fn main() {
println!("{:?}", options().run())
}Derive example
use std::{fmt::Display as _, path::PathBuf};
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
#[allow(dead_code)]
pub struct Options {
/// Path to log file
#[bpaf(
argument("FILE"),
guard(|log_file| !log_file.is_dir(), "The log file can't be a directory"),
fallback(PathBuf::from("logfile.txt")),
format_fallback(|path, f| path.display().fmt(f)),
)]
log_file: PathBuf,
}
fn main() {
println!("{:?}", options().run())
}Output
fallback changes parser to fallback to a default value used when argument is not specified
Options { log_file: "logfile.txt" }
If value is present - fallback value is ignored
Options { log_file: "output.txt" }
Parsing errors are preserved and presented to the user
Error: /: The log file can't be a directory
With display_fallback,
debug_fallback, and
format_fallback, you can make it so the default value
is visible in the --help output.
Usage: app [--log-file=FILE]
- --log-file=FILE
- Path to log file
- [default: logfile.txt]
- -h, --help
- Prints help information