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

How can I pass all command line arguments through Clap to another program?

我有一个程序 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();

当我尝试像这样将命令 "-baz=3" 传递给 bar 时:

./foo -b -baz=3 file.txt

./foo -b "-baz=3" file.txt

clap returns 这个错误:

error: Found argument '-b' which wasn't expected, or isn't valid in this context

如何通过 Clap 隧道命令?

如果参数 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();