Apache Commons CLI : Getting list of values for an option

I’d like to add this here as an answer to @Zangdak and to add my findings on the same problem.

If you do not call #setArgs(int) then a RuntimeException will occur. When you know the exact maximum amount of arguments to this option, then set this specific value. When this value is not known, the class Option has a constant for it: Option.UNLIMITED_VALUES

This would change gerrytans answer to the following:

Options options = new Options();
Option option = new Option("c", "c desc");
// Set option c to take 1 to oo arguments
option.setArgs(Option.UNLIMITED_VALUES);
options.addOption(option);

Leave a Comment