1

I get an error in Eclipse while trying a simple example with the JCommander package. The error says:

The attribute validateWith is undefined for the annotation type Parameter

and the code I'm using is the following:

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import com.beust.jcommander.*;
import com.beust.jcommander.validators.PositiveInteger;

public class JCommanderExample {

    @Parameter(names = { "-sp", "-semAndPrec"},  validateWith = CorrectPathValidator.class)
    private Path semAndPrec;

}

Of course I have provided the CorrectPathValidator class as described in the documentation at http://jcommander.org/#Parameter_validation.

Here is the class:

import java.nio.file.Paths;

import com.beust.jcommander.IParameterValidator;
import com.beust.jcommander.ParameterException;

public class CorrectPathValidator implements IParameterValidator {
    public void validate(String name, String value) throws ParameterException {
        try {
            Paths.get(value);
        } catch (Exception e) {
            String error = "Parameter " + name + " should be a path (found "
                    + value + ")";
            throw new ParameterException(error);
        }
    }
}

Apparently I'm missing something, but the example at http://jcommander.org/#Parameter_validation appears to be identical to what I tried:

@Parameter(names = "-age", validateWith = PositiveInteger.class)
private Integer age;

Can someone please tell me why I get the error?

Alby
  • 13
  • 6

1 Answers1

0

I suspect this is due to the fact that you are trying to parse into a type of "Path", which is not one of JCommander's parseable types. This error seems to be saying that your validator is trying to validate an "undefined parameter" of type "Path".

Either change your parameter to be a type that is automatically parsable: http://jcommander.org/#Types_of_options or implement a custom type: http://jcommander.org/#Custom_types.

Then the validator should work.

Phil Winder
  • 116
  • 1
  • 4