您可以註冊一個Converter以將此功能轉換爲String類型。已經有幾個已經創建的轉換器,並且org.springframework.shell.converters.EnumConverter具有您正在尋找的功能。
要註冊,你需要實現org.springframework.shell.core.Converter接口轉換器:
@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;
}
}
在我註冊了String對象的轉換器,它能夠在@CliOption註釋的optionContext具有enable-prop-converter
前面的例子。
您還必須禁用默認StringConverter。在下一行中,我禁用@CliOption註釋中的默認字符串轉換器disable-string-converter
,並啓用新的PropertyConverter與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
如果你還在試圖弄清楚,你使用的是什麼版本的Spring Shell? – czobrisky