You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)structCli{#[arg(value_enum)]// Which data type to sendpub(crate)data_type:DataType,#[arg(value_enum)]// How to sendpub(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 sendpubdestination:String,}
Describe the solution you'd like
The idea is to expand ArgPredicate to look something like this:
We've talked of generalizing a lot of this logic in #3476.
For this specific proposal, I'm not seeing how it would work as it sounds like you want a default for destination to read both sender and data_type but ArgPredicate doesn't support that, so adding And and Or wouldn't do much.
But, I just realized that this would lead to a full-blown logic system and end up with an inner platform effect. So might as wall ditch the predicate, accept something like Fn(&HashMap<OsStr, OsStr>) -> OsStr and let clap's user worry about it.
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).Describe the solution you'd like
The idea is to expand
ArgPredicate
to look something like this:Alternatives, if applicable
No idea
Additional Context
No response
The text was updated successfully, but these errors were encountered: