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

标签 command-line rust clap

我有一个使用 Clap 的程序 foo处理命令参数解析。 foo 调用另一个程序 bar。最近,我决定 foo 的用户如果愿意,应该能够将参数传递给 bar。我向 Clap 添加了 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" 传递给 bar 时,如下所示:

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

./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选项:

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();

关于command-line - 如何通过 Clap 将所有命令行参数传递给另一个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54191836/

相关文章:

rust - 使用 clap 和 structopt 获取不同命令行选项的相对顺序

c++ - 我如何从按钮运行命令,按下 Qt - C++ 可能

c# - .NET 命令行参数错误?

reference - 我什么时候不应该为引用该特征的实现者而实现该特征?

rust - 如何将字符串传递给接受 Into<&str> 的方法?

rust - Clap 无法解析 YAML 文件 : failed to convert YAML String ("1") value to a string

perl -a : How to change column separator?

c# - 在控制台应用程序中加载 cmd

rust - 如何在 pub extern "C"fn 中返回动态长度向量?

generics - 我如何在 Rust 中编写一个函数来接受其 Item 满足特征的任何迭代器?