Converters - S3nS3IW00/JCommands GitHub Wiki

If an argument's input type is different than its result type, it needs to be converted to the result type. By default, JCommands can convert to Boolean, Byte, Short, Integer, Long, Float and Double from String by parsing the value, but for other conversions, a custom converter has to be registered.

Creating converter

To create a converter an instance of ArgumentResultConverter<F, T> object or a class that implements this interface has to be created. The class has two generic types, the F is the type of the input, and the T is the type of the result.

  1. Lambda expression
ArgumentResultConverter<String, Integer> numberConverter = value -> Integer.parseInt(value);
  1. Class
public class NumberConverter implements ArgumentResultConverter<String, Integer> {

    @Override
    public Integer convertTo(String value) {
        return Integer.parseInt(String.valueOf(value));
    }
}

Registering converter

The converters can be registered on InputArgument<I, O> with convertResult(ArgumentResultConverter) method with the matching input and result type.

⚠️ **GitHub.com Fallback** ⚠️