默认情况下,布尔字段设置为false
,但我希望它默认设置为true
。在Rust的docopt中默认情况下是否可以将布尔选项设置为'true'?
我试图在docopt
描述中使用[default: true]
,但似乎default
不能应用于布尔选项。我也尝试使用Rust的Default
特征 - 它也不起作用。
下面是一个小例子:
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()
}
}
你能解释一下如何设置这个值为false吗? –
如果没有办法将它设置为'false',那么问题很容易解决:'const FLAG_AN_OPTION_WITH_DEFAULT_TRUE:bool = true;' – Shepmaster
@Shepmaster Errm,问题是如果一个标志默认为true,那么就没办法为最终用户设置标志为false。因此,将标志默认为true是没有意义的。一个标志是(true)或不是(false)。 – BurntSushi5