-
Notifications
You must be signed in to change notification settings - Fork 0
required options
For more complex command-line interfaces, it is a commonly observed problem that certain options are required under certain circumstances (but not always). cli-parsec features a description language that allows to describe either a plain "option is required" or arbitrariliy complex conditions (based on the presence or absence of other arguments).
<expr> ::= <bool> | ( <expr> ) | ! <expr> | <expr> <operator> <expr> | present(<field>) <bool> ::= "true" | "false" <operator> ::= "&" | "|" <field> ::= 'alphanumeric + underscore (same as for fields in the Java language)'
The unary NOT (!) operator has highest precedence. It negates the expression to its right. The binary AND (&) operator has higher precedence than the OR (|) operator (as is convention otherwhere). Brackets may be used to enforce deviating evaluation precedence.
.'present' function
The present(field) function evaluates to true iff an argument for the parameter described by the field is present at least once in the actual command line arguments (otherwise it evaluates to false). field must denote (by its name) a field of the same type (or a supertype) as the annotated field. The field must be annotated with the @Option annotation. If these conditions do not hold true, a RuntimeException will occur during parsing.
Examples
~~~~
consider we have three options --first, --second, --third. The --third option is mandatory if either --first or --second are present. Otherwise it is not required. To express this, you would annotate the field representing the --third option like so:
@Option(required="present(first)|present(second)")
boolean third;
@Option
boolean first;
@Option
boolean second;