使用 clap 3 派生宏,如何指定一组中至少必须存在一个参数?

Seb*_*idt 5 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>,
}
Run Code Online (Sandbox Code Playgroud)

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

ale*_*leb 4

您可以在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"]),
        ))]
Run Code Online (Sandbox Code Playgroud)

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