'Does JCommander support variableArity parameters with 0 arguments?

I'm using JCommander to parse the command-line arguments in one of my Java programs. My program requires a parameter (call it -p) which I've declared as variableArity and that can take any number of arguments (including 0). I need to always pass the -p flag to the program, regardless of whether or not it's accepting any arguments, so that it is parsed as an empty List if nothing is passed, and a List of Strings if at least one argument is passed. Problem is, whenever I pass nothing to the -p flag, the program throws a ParameterException, crashing my program. Is this behavior supported by JCommander, or will I need to find another way to do this?

To give an example, if I invoke my program with:

java program -p -arg1 ...

I want the corresponding List to be parsed as the empty list, and if I invoke it with:

java program -p p1 p2 -arg1 ...

I want the corresponding List to be parsed as the list containing p1 and p2.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source