我正在寻找一种方法来模拟 C 如何使用getopt
. 我想使用 docopt 将以下 C 代码段转换为 Rust。我似乎无法将标志传递给命令行参数:
char in;
char* stringName;
while(( in = getopt( argc, argv, "a:b:c:d:e:")) != EOF) {
switch(in) {
case 'a':
stringName = optarg;
break;
// ... and so on
然后我想跑
cargo run -a "hello" -b 3 ... and so on
到目前为止,我已经写了这个:
extern crate rustc_serialize;
extern crate docopt;
use docopt::Docopt;
// Define a USAGE string
const USAGE: &'static str = "
Program.
Usage: [options] [<value1>] [options] [<value2>] [options] [<value3>] [options] [<value4>]
Options:
-a,
-b,
-c,
-d,
";
#[derive(Debug, RustcDecodable)]
struct Args {
arg_value1: Option<String>,
flag_a: bool,
flag_b: bool,
flag_c: bool,
arg_value2: Option<String>,
arg_value3: Option<String>,
arg_value4: Option<String>,
}
fn main() {
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.decode())
.unwrap_or_else(|e| e.exit());
println!("{:?}", args);
}
当cargo run
我得到
未知标志 -a