2016-02-25 26 views
1

我目前正在嘗試改進Spring Shell應用程序,並且如果它支持值的選項卡完成以及選項,會使其更好的一件事情。是否可以在Spring Shell中製表符值?

舉個例子,如果我的CLI有一個命令getInfo --name <name>,其中<name>是從有限的在DB名稱的名字,這將是得心應手能夠做到

> getInfo --name Lu<tab> 
    Lucy Luke Lulu 
> getInfo --name Luk<tab> 
> getInfo --name Luke 

有什麼使用現有的Spring Shell工具來做到這一點?我已經在文檔中捅了一跤,找不到有關自動完成值的任何內容。

乾杯!

+0

如果你還在試圖弄清楚,你使用的是什麼版本的Spring Shell? – czobrisky

回答

0

您可以註冊一個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 
相關問題