Skip to content

converter

h908714124 edited this page Jul 8, 2021 · 16 revisions

In the DeleteCommand example, the path parameter is required, while verbosity is optional. The @Parameter and @Option annotations do not have boolean attributes like required or optional. Instead, the multiplicity is determined by the parameter type.

Skew table A: Auto conversion

(option|param) type Multiplicity Arity (if option)
boolean 0..1 (optional) nullary
Optional<A> 0..1 (optional) unary
Optional{Int,Long,Double} 0..1 (optional) unary
List<A> 0..* (repeatable) unary
A (exact match) 1 (required) unary

where A must be one of the auto types, otherwise compilation will fail.

In the DeleteCommand example, the type of path is java.nio.file.Path, which is one of the auto types, so the last rule applies, making this a required parameter. The type of verbosity is OptionalInt, so the third rule applies, which makes this an optional option.

The @Option and @Parameter annotations also have an optional attribute converter, which takes a single value of type Class<?>. A converter class must implement Function<String, E> or Supplier<Function<String, E>> for some E.

Skew table B: Converter attribute is set

When the converter attribute is set, then the multiplicity is determined by looking at both the return type and the converter type.

Converter type (option|param) type Multiplicity Arity (if option)
String -> M Optional<M> 0..1 (optional) unary
String -> {Integer,Long,Double} Optional{Int,Long,Double} 0..1 (optional) unary
String -> M List<M> 0..n (repeatable) unary
String -> M M 1 (required) unary
String -> {Integer,Float,...} {int,float,...} 1 (required) unary

When none of these rules apply, compilation will fail.

Clone this wiki locally