args4j with BeanValidation



Annotate your @Options with some annotations defined in javax.validation.

@Option(name = "-buffer-capacity",
        usage = "each buffer's capacity in bytes")
private int bufferCapacity = 65536;

@Option(name = "-buffer-count", required = true,
        usage = "number of buffers to allocate")
private int bufferCount;


And then validate by yourself.

final Opts opts;
final ValidatorFactory factory
    = Validation.buildDefaultValidatorFactory();
final Validator validator = factory.getValidator();
final Set<ConstraintViolation<Opts>> violations
    = validator.validate(opts);
if ( -> {
    System.err.println("violation: " + v);
    return v;
}).count() > 0L) {

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s