如何通过Clap将所有命令行参数传递给另一个程序? [英] How can I pass all command line arguments through Clap to another program?

查看:31
本文介绍了如何通过Clap将所有命令行参数传递给另一个程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序 foo ,该程序使用拍击进行处理命令参数解析. foo 调用另一个程序 bar .最近,我认为 foo 的用户应该可以根据需要将参数传递给 bar .我在Clap中添加了 bar 命令:

I have a program foo that uses Clap to handle command argument parsing. foo invokes another program, bar. Recently, I decided that users of foo should be able to pass arguments to bar if they like. I added the bar command to 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 时,如下所示:

When I try to pass the command "-baz=3" to bar like so:

./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传递命令?

How do I tunnel commands through Clap?

推荐答案

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

If the value of argument bar may itself start with a hyphen, then you need to set the 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();

这篇关于如何通过Clap将所有命令行参数传递给另一个程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆