I am writing command line application in Java and I've chosen Apache Commons CLI to parse input arguments.
Let's say I have two required options (ie. -input and -output). I create new Option object and set required flag. For now it's all good. But I have third, not required option, ie. -help. With settings that I've mentioned, when user wants to show help (use -help option) it says "-input and -output" are required. Is there any way to implement this (via Commons CLI API, not simple if (!hasOption) throw new XXXException()).
In this situation you have to define two sets of options and parse the command line twice. The first set of options contains the options that precede the required group (typically
--help
and--version
), and the second set contains all the options.You start by parsing the first set of options, and if no match is found, you go on with the second set.
Here is an example: