Clap either -a OR (-b AND -c) arguments

I'm trying to get clap 3.2 to validate and show help correctly for the following scenario: give a token OR give username AND password.

With the current configuration I got the validation right, but the help is not very indicative of the validation.

#[derive(Parser)]
#[clap(group(
    ArgGroup::new("auth")
        .required(true)
        .multiple(false)
        .args(&["token","username"])
))]
pub struct Cli {
    #[clap(long, requires = "password")]
    pub username: Option<String>,

    #[clap(long, requires = "username")]
    pub password: Option<String>,

    /// Authentication token (replaces username and password)
    #[clap(long)]
    pub token: Option<String>,
}

Is this the closest I can get to token OR username AND password or there is a better way?

The current configuration falls a bit short when no option is given, that is not said that not only username is needed but the password too:

error: The following required arguments were not provided:
    <--token <TOKEN>|--username <USERNAME>>

Or if only --password is given, the same message is given.

You could try to make it more robust by specifically asking the user for missing arguments. For example, if the user only enters -username, it should pop up with a dialog that asks for the password.

Or, (and I'm not sure if Clap allows this), you could have a single argument like -login that takes both a username and a password, but that's probably less readable.

The current configuration does that. If you only input --username it asks for:

error: The following required arguments were not provided:
    --password <PASSWORD>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.