I would like to make the
--table
option be required if and only if no value is given for
--in-file
. How might I go about doing this? I know that there are some solutions (I think at least) where I can do this with multiple classes, but that seems like overkill for only two arguments in a simple program.
I know I can also manually check the values after the parsing is completed, but that seems to defeat the purpose of using args4j.
–
–
I realized I can use the
forbids
annotation option to most of accomplish this. You only need the tag on one or the other, but I put it on both for completeness.
Using
forbids
@Options(name="--in-file", forbids{"--table"})
String fileName;
@Option(name="--table", forbids={"--in-file"})
String table;
One could use the depends
annotation for reverse logic.
Why the above is not complete:
The above is only partially correct: the solution still fails if you want one of the options to be required if another isn't given. The required part overrules the forbids, so doing
@Options(name="--in-file", forbids{"--table"})
String fileName;
@Option(name="--table", required=true, forbids={"--in-file"})
String table;
causes a logic error in that if I give --in-file
it tries to forbid --table
but it can't because --table
is required.
So, the original solution fails because it allows the user to give neither option, even though I want --table' to be required, if and only if
--in-file` is not given. So, basically, my original question.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0
with attribution required.
rev 2019.5.14.33702