Search code examples
javabean-validationargs4j

How can I validate options with args4j?


With following code,

@Option(name = "age") // min = "1"
private int age;

How can I validate age field? Say must be bigger than zero?


Solution

  • You can place Option annotation on the method of the form void methodName(T value). So you can easily do it in the following way.

    private int age;
    
    @Option(name = "age")
    void setAge(int age) {
        if (age < 1) {
            throw new CmdLineException("message");
        }
        this.age = age;
    }