By default, a boolean field is set to false
, but I want it set to true
by default.
I tried to use [default: true]
in the docopt
description, but it seems default
cannot be applied to a boolean option. I also tried to use Rust's Default
trait - it doesn't work either.
The following is a minimal example:
extern crate rustc_serialize;
extern crate docopt;
use docopt::Docopt;
const USAGE: &'static str = "
Hello World.
Usage:
helloworld [options]
Options:
--an-option-with-default-true This option should by default be true
";
#[derive(Debug, RustcDecodable)]
struct Args {
flag_an_option_with_default_true: bool,
}
impl Args {
pub fn init(str: &str) -> Args {
Docopt::new(USAGE)
.and_then(|d| d.argv(str.split_whitespace().into_iter()).parse())
.unwrap_or_else(|e| e.exit())
.decode()
.unwrap()
}
}