args4j with BeanValidation


references

args4j

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

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

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

BeanValidation

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 (violations.stream().map(v -> {
    System.err.println("violation: " + v);
    return v;
}).count() > 0L) {
    return;
}
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s