Make arg enabled depending on value of other arg? #4402
-
I have a enum Foo {
A,
B,
C,
} I want to make another argument in my CLI valid only if // ...
struct CLI {
#[clap(short, value_enum, default_values_t = [Foo::A])]
foo: Foo,
#[clap(short, only_possible_to_specify_if("foo", Foo::B))]
bar: PathBuf,
} Note that How would I do this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
We currently have two facilities
but what you need is "the other argument's value is required to be XX if the current argument is present" Without a high demand for it, I'm not thrilled with adding yet another argument relationship type until we generalize argument relations As a workaround, you can catch this yourself and use clap to create the error message. |
Beta Was this translation helpful? Give feedback.
-
I have a similar question; let me know if this should be a separate discussion instead of a comment here. Is there a way to enable or disable one or more arguments based on out-of-band logic that's evaluated partway through Clap parsing? In particular, is is possible to enable or disable certain arguments based on a value that is read from a file, whose filename was specified in previous argument? I am currently trying to figure out how to do this in the Derive interface, although am wondering if the Builder interface would be necessary. My use case is that I'm writing a multi-domain problem solver, and each domain has zero or more different configuration options. A problem instance is stored in its own file, and is specific to some domain, but not to that domain's configuration: instead, the user of the solver gets to configure the domain. Since filename of problem instance file is passed to the solver as a command line argument, and since the appropriate domain name is listed in each problem instance file, I'd like to just read the domain name from the problem instance file, instead of making the user specify the problem domain as a command line argument. Then, depending on what the domain is, I'd like to require a different set of additional domain-specific command line arguments. For example, if the
and the
...I'd like the argument requirements to follow these examples respectively:
and
The control flow during parsing would be something like this:
I guess this is kind of like selecting a |
Beta Was this translation helpful? Give feedback.
We currently have two facilities
required_if_eq
. The current argument becomes required if another argument equals a valuerequires_if
: The other argument becomes required if the current argument equals a valuerequired_unless_present
: The current argument is required unless the other argument is presentbut what you need is "the other argument's value is required to be XX if the current argument is present"
Without a high demand for it, I'm not thrilled with adding yet another argument relationship type until we generalize argument relations
As a workaround, you can catch this yourself and use clap to create the error message.