rust - 使用 clap 3 derive 宏,我如何指定必须至少存在一个组的参数?

标签 rust clap

我有一个拍手结构如下:

#[derive(Clap)]
#[clap(
    version = "0.1.0",
    author = "..."
)]
pub struct Cli {
    #[clap(long)]
    arg_a: Option<String>,

    #[clap(long)]
    arg_b: Option<String>,

    #[clap(long)]
    arg_c: Option<String>,

    #[clap(long)]
    other_arguments: Option<usize>,
}

我如何指定至少必须存在 {arg_a, arg_b, arg_c} 之一,但还可以存在更多?

最佳答案

您可以在 https://github.com/clap-rs/clap/blob/v3.1.14/examples/tutorial_derive/README.md#argument-relations 阅读有关参数关系的信息

添加例如:

#[clap(group(
            ArgGroup::new("my-group")
                .required(true)
                .args(&["arg-a", "arg-b", "arg-c"]),
        ))]

https://docs.rs/clap/latest/clap/struct.ArgGroup.html#method.required “解析时需要来自组的参数。”

关于rust - 使用 clap 3 derive 宏,我如何指定必须至少存在一个组的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67803027/

相关文章:

rust - 使用 clap 遍历位置参数

rust - 类型不匹配,使用 lookup_host 时预期 () 找到结果

Rust 书,猜谜游戏类型不匹配

rust - 如何在stuctopt定义中访问运行时定义的变量?

rust - 如何防止最后一个参数需要用 clap 引用?

rust - 正确解析 Clap ArgMatches 的惯用使用rust 方法

generics - 可以将实现特征的所有类型存储在列表中并遍历该列表吗?

templates - 如何为泛型使用括号?

rust - 如何使用代表 StructOpt 子命令的枚举?