Skip to content

Add logic to ArgPredicate #4731

@jaskij

Description

@jaskij

Please complete the following tasks

Clap Version

4.1.6

Describe your use case

I want to set an arguments default value, using default_value_ifs() depending on multiple other arguments.

For example, in the code below, I would like to change the value for destination depending on the value of data_type (since in actual usage the UDP port depends on the type of data we're sending).

#[derive(Debug, clap::Parser)]
#[command(
    long_about = None,
    long_version(crate::build_info::BUILD_INFO.long_version_string(),
))]
pub(crate) struct Cli {
    #[arg(value_enum)]
    // Which data type to send
    pub(crate) data_type: DataType,

    #[arg(value_enum)]
    // How to send
    pub(crate) sender: Sender,

    #[arg(
        short,
        long,
        default_value_ifs([
            ("sender", clap::builder::ArgPredicate::Equals("nats".into()), "localhost:4222"),
            ("sender", clap::builder::ArgPredicate::Equals("udp".into()), "localhost:8585")
        ]),
    )]
    /// Where to send
    pub destination: String,
}

Describe the solution you'd like

The idea is to expand ArgPredicate to look something like this:

pub enum ArgPredicate {
    IsPresent,
    Equals(OsStr),
    And(ArgPredicate, ArgPredicate),
    Or(ArgPredicate, ArgPredicate),
}

Alternatives, if applicable

No idea

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Raise on the bar on expectations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions