如何通过 Clap 将所有命令行参数传递给另一个程序?

HiD*_*der 3 command-line rust clap

我有一个foo使用Clap来处理命令参数解析的程序。foo调用另一个程序,bar. 最近,我决定用户如果愿意的话foo应该能够传递参数。bar我将bar命令添加到 Clap 中:

let matches = App::new("Foo")
    .arg(Arg::with_name("file").value_name("FILE").required(true))
    .arg(
        Arg::with_name("bar")
            .value_name("[BAR_OPTIONS]")
            .short("b")
            .long("bar")
            .multiple(true)
            .help("Invoke bar with these options"),
    )
    .get_matches();
Run Code Online (Sandbox Code Playgroud)

当我尝试将命令传递"-baz=3"bar这样的情况时:

let matches = App::new("Foo")
    .arg(Arg::with_name("file").value_name("FILE").required(true))
    .arg(
        Arg::with_name("bar")
            .value_name("[BAR_OPTIONS]")
            .short("b")
            .long("bar")
            .multiple(true)
            .help("Invoke bar with these options"),
    )
    .get_matches();
Run Code Online (Sandbox Code Playgroud)

或者

./foo -b -baz=3 file.txt
Run Code Online (Sandbox Code Playgroud)

clap返回此错误:

./foo -b "-baz=3" file.txt
Run Code Online (Sandbox Code Playgroud)

如何通过 Clap 传送命令?

har*_*mic 5

如果参数的值bar本身可能以连字符开头,那么您需要设置选项allow_hyphen_values

let _matches = App::new("Foo")
    .arg(Arg::with_name("file").value_name("FILE").required(true))
    .arg(
        Arg::with_name("bar")
            .value_name("[BAR_OPTIONS]")
            .allow_hyphen_values(true)
            .short("b")
            .long("bar")
            .multiple(true)
            .help("Invoke bar with these options"),
    )
    .get_matches();
Run Code Online (Sandbox Code Playgroud)