You can register a Converter to get this functionality to the String type. There are already a few already created converters and the org.springframework.shell.converters.EnumConverter has the functionality you are looking for.
To register a Converter you need to implement the org.springframework.shell.core.Converter interface:
@Component
public class PropertyConverter implements Converter<String> {
public boolean supports(Class<?> type, String optionContext) {
return String.class.isAssignableFrom(type)
&& optionContext != null && optionContext.contains("enable-prop-converter");
}
public String convertFromText(String value, Class<?> targetType, String optionContext) {
return value;
}
public boolean getAllPossibleValues(List<Completion> completions, Class<?> targetType, String existingData,
String optionContext, MethodTarget target) {
boolean result = false;
if (String.class.isAssignableFrom(targetType)) {
String [] values = {"ONE", "BOOKING", "BOOK"};
for (String candidate : values) {
if ("".equals(existingData) || candidate.startsWith(existingData) || existingData.startsWith(candidate)
|| candidate.toUpperCase().startsWith(existingData.toUpperCase())
|| existingData.toUpperCase().startsWith(candidate.toUpperCase())) {
completions.add(new Completion(candidate));
}
}
result = true;
}
return result;
}
}
In the previous example I've registered a converter for the String Object that it enables when the optionContext of the @CliOption Annotation has enable-prop-converter
.
You also have to disable the default StringConverter. In the next line I disabled the default String converter in the @CliOption Annotation with disable-string-converter
and enabled the new PropertyConverter with enable-prop-converter
.
@CliOption(key = { "idProp" }, mandatory = true, help = "The the id of the property", optionContext="disable-string-converter,enable-prop-converter") final String idProp