Imagine I have this two options in args4j:
@Option(name = "-a")
boolean a;
@Option(name = "-b")
boolean b;
Is it possible, with annotation, to say that i want exactly either a or b (and only one of them)
Thanks!
It is not possible to do with annotations only. But it is possible to do with annotation and one check.
You can use forbids
in annotations to set At-Most-One constraint on your options.
@Options(name="-a1", forbids{"-a2", "-a3", /*...*/, "-aN"})
T a1;
@Options(name="-a2", forbids{"-a1", "-a3", /*...*/, "-aN"})
T a2;
/*...*/
@Options(name="-aN", forbids{"-a1", "-a2", /*...*/, "-a(N-1)"})
T aN;
And you can add one check in your class to set At-Least-One constraint on your options.
if (a1 == null && a2 == null && /*...*/ && aN == null) {
throw new CmdLineException();
}
You can set exception message same with message shown when required
option set for consistency.