'Using clap 3 derive macros, how do I specify that at least one argument of a group must be present?

I have a clap struct as follows:

#[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>,
}

How do I specify that at least one of {arg_a, arg_b, arg_c} must be present, but also more can be present?



Solution 1:[1]

You can read about argument relations at https://github.com/clap-rs/clap/blob/v3.1.14/examples/tutorial_derive/README.md#argument-relations

Add for example:

#[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 "Require an argument from the group to be present when parsing."

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aleb