K- the type of the object that is the result of the conversion
public static interface CommandLine.ITypeConverter<K>
This interface defines the contract for classes that know how to convert a String into some domain object.
Custom converters can be registered with the
CommandLine.registerConverter(Class, ITypeConverter) method.
Java 8 lambdas make it easy to register custom type converters:
commandLine.registerConverter(java.nio.file.Path.class, s -> java.nio.file.Paths.get(s)); commandLine.registerConverter(java.time.Duration.class, s -> java.time.Duration.parse(s));
Built-in type converters are pre-registered for the following java 1.5 types:
value- the command line argument String value
Exception- an exception detailing what went wrong during the conversion. Any exception thrown from this method will be caught and shown to the end user. An example error message shown to the end user could look something like this:
Invalid value for option '--some-option': cannot convert 'xxxinvalidinput' to SomeType (java.lang.IllegalArgumentException: Invalid format: must be 'x:y:z' but was 'xxxinvalidinput')
CommandLine.TypeConversionException- throw this exception to have more control over the error message that is shown to the end user when type conversion fails. An example message shown to the user could look like this:
Invalid value for option '--some-option': Invalid format: must be 'x:y:z' but was 'xxxinvalidinput'