CLI2 provides a mechanism to validate argument values. The Validator
interface must be implemented to create an argument validator. This interface has
a single method validate(java.util.List values) throws InvalidArgumentException.
CLI2 has some standard validators included. They validate the following:
The ClassValidator validates a value using three criteria:
TODO: add section about values being replaced with class instances
The DateValidator validates values against a
java.text.DateFormat. There are three helper methods
that create built-in validators.
getDateInstance returns a validator for the default
date format for the default locale.
getTimeInstance returns a validator for the default
time format for the default locale.
getDateTimeInstance returns a validator for the default
date and time format for the default locale.
A DateValidator can also be created by passing your
a custom DateFormat into the constructor.
In addition to basic format checking you can also check if the date/time specified
is before/after a specific date/time. The lower bound is set using the
setMinimum( java.util.Date ), and the upper bound is set using
the setMaximum( java.util.Date ).
TODO: add section about values being replaced with date instances
The EnumValidator validates values against a list of
allowed string values. The values that are allowed are specified in
a java.util.Set and passed in to the constructor.
The FileValidator validates that values represent
existing files. You can also specify a combination of the
following additional criteria:
Each of the criteria listed here are specified using the appropriate setter.
There are three helper methods to create validators:
getExistingInstance returns a validator that ensures
a value represents an existing file.
getExistingFileInstance returns a validator that ensures
a value represents an existing file that is not a directory.
getExistingDirectoryInstance returns a validator that ensures
a value represents an existing file this is a directory.
The NumberValidator validates that values adhere to
certain rules like the following:
getCurrencyInstance returns a validator for the default
currency format for the default locale.
getPercentInstance returns a validator for the default
percent format for the default locale.
getIntegerInstance returns a validator for the default
integer format for the default locale.
getNumberInstance returns a validator for the default
number format for the default locale.
A NumberValidator can also be created by passing your
a custom NumberFormat into the constructor.
In addition to basic format checking you can also check if the number specified
is less than or greater than a specific number. The lower bound is set using
the setMinimum( Number ), and the upper bound is set using
the setMaximum( Number ).
A UrlValidator validates that values are URLs and if you
choose it will also validate the protocol is of the type you have
specified.