0

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!

Jitsumi
  • 127
  • 2
  • 9

1 Answers1

0

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.

  • All the "simple" CmdLineException ctors are deprecated now. The non-deprecated either require a Throwable, or are like the one you mentioned and require a Localizable. The problem is that all the code used to reproduce the missing required options are package-private and rely on the args4j ResourceBundle. Do you have a workaround? – Novaterata Aug 11 '17 at 14:56