-
Why can't I set a fallback value for use bpaf::Bpaf;
#[allow(dead_code)]
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub struct Options {
#[bpaf(long, fallback(Some(String::from("main"))))]
revision: Option<String>,
#[bpaf(long, fallback(String::from("main")))]
revision2: String,
}
fn main() {
println!("{:?}", options().run())
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
First let's take a look at the code that gets generated in both cases: let revision = long("revision").argument::<String>("ARG").optional().fallback(Some(String::from("main")));
let revision2 = long("revision2").argument::<String>("ARG").fallback(String::from("main")); Both
If use bpaf::Bpaf;
#[allow(dead_code)]
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub struct Options {
#[bpaf(long, argument, fallback(String::from("main")), map(Some))]
revision: String,
}
fn main() {
println!("{:?}", options().run())
} |
Beta Was this translation helpful? Give feedback.
First let's take a look at the code that gets generated in both cases:
Both
fallback
andoptional
work by catching "item not found" error and converting it into a value - specified value in case offallback
andNone
in case ofoptional
. Later additionally wraps present value withSome
.If
revision
is always will be present either from user input or from the fall…