2

I'm using JCommander v. 1.72 and my aim is to get a (comma-seperated) list of strings in a parameter and validate the single strings. Sounds simple, right?

Here is my code:

@Parameter(names = { "-sort", "-s" }, validateWith = ParameterValidatorHeader.class)
private List<String> sort = new ArrayList<String>();

I run the programm with this command:

-sort column1,column2

I expected that JCommander would call the validator after splitting on the single items, i.e. validate "column1", validate "column2". But obviously the string "column1,column2" is sent to the validator class before splitting the parameter value.

How can I achieve to do the validation on the single items instead? (Sure, I could do the validation with pure Java after parsing them with JCommander...)

Thanks Erich

Erich13
  • 69
  • 5

1 Answers1

-1

You can't, not by using JCommander at least, the docs are quite clear on that

4.2 Global parameter validation

After parsing your parameters with JCommander, you might want to perform additional validation across these parameters, such as making sure that two mutually exclusive parameters are not both specified. Because of all the potential combinations involved in such validation, JCommander does not provide any annotation-based solution to perform this validation because such an approach would necessarily be very limited by the very nature of Java annotations. Instead, you should simply perform this validation in Java on all the arguments that JCommander just parsed.

svarog
  • 9,477
  • 4
  • 61
  • 77