I am working with JCommander 1.48, and i came across the following problem:
For Example I have got these Parameters:
@Parameter(names = "user", description = "the User")
private String user;
@Parameter(names = "password", description = "the password")
private String password
I am running my program with the following arguments:
--user hugo --password secret
and:
--user hugo david --password secret
Both of them amount to the same solution, the program is running perfectly. But I want the program to throw an Exception if there are too many values for a parameter. Iam aware of "arity"( amount of values for parameter ) as a configuration of the @Parameter annotation, but the default value of arity for Strings is 1 allready. It seems like everything is just getting ignored after the first value, as long as it is not another parameter.
Any solutions or ideas?
EDIT:
The Basic Solution (posted by assylias) does not work for me. More accurate Example:
public class MyTestProgram {
private final Params options;
public MyTestProgram(String[] args) {
options = new Params();
new JCommander(options).parse("--user hugo david --password secret".split(" "));
//pass "args" to parse() instead of hardcoded string.
}
public static void main(String[] args) throws Exception {
System.setProperty("org.jboss.logging.provider", "slf4j");
new MyTestProgram(args);
}
}
public class Params {
@Parameter
private List<String> parameters = new ArrayList<>();
@Parameter(names = "--user", description = "the user", required = true)
private String webuser;
@Parameter(names = "--password", description = "the password", required = true)
private String stage;
//getters and setters
}
This is everything that happens before the actual program-code starts.
EDIT: assylias updated answer solved the problem.