-
Notifications
You must be signed in to change notification settings - Fork 991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow require and conflicts on the same arguments #2392
Comments
Thanks for the links to previous issues and description. I did some research last year on what other packages offer, and there are a couple of packages with support for this case. Oclif has Python |
@shadowspawn Ultimately the two you mentioned have the same result (just different APIs for achieving it): you can specify that only 1 of the mutually exclusive options are chosen. Either of these seem clunky given the existing My initial suggestion was actually to consider any options that are BOTH mutually exclusive AND required as if they were a single anonymous option with an alias for each of the different choices. Another way to look at it is, you would consider the required constraint as a mutual constraint over the ENTIRE mutually exclusive set of options. Basically, to consider the This would be more like creating a 'virtual' mutually exclusive group (as is manually created with argParse), but without having to use the existing A benefit of this approach comes from the fact that the CURRENT behavior of setting both That's why I'm suggesting it as the best way to solve the problem, since it shouldn't break anyone's existing code and doesn't add to the complexity of the yargs API (though arguably could be harder to reason about than solutions that involve a more explicit declaration by the user). All this being said, I'm not a yargs expert, and there very well may be a reason this actually doesn't work, so definitely down to hear your thoughts! |
I did see that, and it is interesting that it turns a broken combination into something which covers another case. That is clever. But I don't like it since it is not clear that this particular combination should/could work in this way. As you said: may be "deemed too likely to be misunderstood" and "arguably could be harder to reason about". Yes, that is me, I am concerned. |
yeah fair, it does change the semantics of anyway, happy to have the discussion! To be clear, I'd be just as happy with one of the other solutions (and the addition of an error check on the "conflict" "require" situation), just thought it was cool that there was space in the existing API for it.... |
to extend the evaluation of possible cases with intersections between the
|
Context:
#189
#1294
#1093
Story:
I would like to be able to create two flags on a command, one MUST be included, but both MUST NOT be included.
Put another way, I would like my command to be accepted IFF (flag1, flag2) => XOR(flag1, flag2) === true
Workarounds:
It is possible to enforce this kind of thing with
check()
but this has a few issues:Example:
This yargs command currently doesn't work:
If I call this with:
mycommand --mutually_exclusive_option_1
--mutually_exclusive_option_2 is required
mycommand --mutually_exclusive_option_2
--mutually_exclusive_option_1 is required
mycommand --mutually_exclusive_option_1 --mutually_exclusive_option_2
--mutually_exclusive_option_1 and --mutually_exclusive_option_2 are mutually exclusive
Proposal:
This should allow one, but not both of the options which are mutually exclusive to be passed. Alternatively, if this is deemed too likely to be misunderstood (I can see that more complex cases might be hard to grok), we could allow a new parameter on
group
allowing us to set arequire_amount
or justrequire
on the group itself to say that at LEAST one option from the group is required. In conjunction withconflicts
, this gives us the tool to create a required, yet mutually exclusive set of options.In the case that this feature proposal isn't accepted, the parser should at least be updated to throw an error (hopefully with some helpful guidance on how to work around the issue) if a dev tries to create a set of options that are both required and mutually exclusive.
Group Example:
The text was updated successfully, but these errors were encountered: