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

2024-02-29

我有一个程序foo使用Clap https://github.com/clap-rs/clap处理命令参数解析。foo调用另一个程序,bar。最近,我决定用户foo应该能够将参数传递给bar如果他们喜欢的话。我添加了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();

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

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

or

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

clap返回此错误:

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

如何通过 Clap 传送命令?


如果参数的值bar本身可能以连字符开头,那么您需要设置allow_hyphen_values https://docs.rs/clap/2.32.0/clap/struct.Arg.html#method.allow_hyphen_values option:

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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐