1

I'm using JCommander to parse command line params. I validate parameters using:

public class SomeClass implements IParameterValidator {
    public void validate(String name, String value) throws ParameterException { 
    ...
}

I ran into edge cases, when it would be helpful if the block of code above could know what are the other command line params supplied for the application, not just that one. Is it possible with JCommander at all?

I can achieve my goals with JCommander Commands, but this is not as flexible as the desired solution.

automatictester
  • 2,436
  • 1
  • 19
  • 34

2 Answers2

4

The documentation clearly say that there is no specific annotation to make a global validation on the provided parameters. http://jcommander.org/#global_validation

It is advised to make the verification after the parmeter parsing.

chaiyachaiya
  • 2,617
  • 18
  • 19
1

I achieved this by making the variable to test against, static. For example:

@Parameter(names = {"big"}, description = "This must be greater than small", validateValueWith = GreaterThanSmall.class)
private Long big = 10000L;

@Parameter(names = {"small"}, description = "Small")
private static Long small = 10L;

public static class GreaterThanSmall implements IValueValidator<Long> {
    @Override
    public void validate(String name, Long value) throws ParameterException {
        if (value < small) {
            throw new ParameterException("Parameter " + name + " should be greater than small (found " + value +")");
        }
    }
}
Phil Winder
  • 116
  • 1
  • 4