2010-10-29 52 views
1

我想獲得的JLine做標籤完成,所以我可以像輸入下面的內容:的JLine多參數解析

commandname --arg1 value1 --arg2 value2 

我使用下面的代碼:

final List<Completor> completors = Arrays.asList(
    new SimpleCompletor("commandname "), 
    new SimpleCompletor("--arg1"), 
    new SimpleCompletor("--arg2"), 
    new NullCompletor()); 

consoleReader.addCompletor(new ArgumentCompletor(completors)); 

但經過我鍵入value2選項卡完成停止。

(Suplementary的問題,我可以使用的JLine驗證值1的日期?)

回答

3

我有同樣的問題,我通過創建我自己的教學班的JLine完成命令解決它。我只需要實現我自己的Complector。

我正在開發一個應用程序,它可以幫助DBA鍵入不僅命令名稱,而且還有參數。我正在使用jLine進行終端交互,並創建了另一個Complector。

我必須向Complector提供完整的語法,這是我的應用程序的目標。它被稱爲Zemucan,它在SourceForge中託管;此應用程序最初專注於DB2,但可以合併任何語法。我使用的Complector的示例是:

public final int complete(final String buffer, final int cursor, 
     @SuppressWarnings("rawtypes") final List candidateRaw) { 
final List<String> candidates = candidateRaw; 

    final String phrase = buffer.substring(0, cursor); 
    try { 
     // Analyzes the typed phrase. This is my program: Zemucan. 
     // ReturnOptions is an object that contains the possible options of the command. 
     // It can propose complete the command name, or propose options. 
     final ReturnOptions answer = InterfaceCore.analyzePhrase(phrase); 

     // The first candidate is the new phrase. 
     final String complete = answer.getPhrase().toLowerCase(); 

     // Deletes extra spaces. 
     final String trim = phrase.trim().toLowerCase(); 

     // Compares if they are equal. 
     if (complete.startsWith(trim)) { 
      // Takes the difference. 
      String diff = complete.substring(trim.length()); 
      if (diff.startsWith(" ") && phrase.endsWith(" ")) { 
       diff = diff.substring(1, diff.length()); 
      } 
      candidates.add(diff); 
     } else { 
      candidates.add(""); 
     } 

     // There are options or phrases, then add them as 
     // candidates. There is not a predefined phrase. 
     candidates.addAll(this.fromArrayToColletion(answer.getPhrases())); 
     candidates.addAll(this.fromArrayToColletion(answer.getOptions())); 
     // Adds a dummy option, in order to prevent that 
     // jLine adds automatically the option as a phrase. 
     if ((candidates.size() == 2) && (answer.getOptions().length == 1) 
       && (answer.getPhrases().length == 0)) { 
      candidates.add(""); 
     } 
    } catch (final AbstractZemucanException e) { 
     String cause = ""; 
     if (e.getCause() != null) { 
      cause = e.getCause().toString(); 
     } 
     if (e.getCause() != null) { 
      final Throwable ex = e.getCause(); 
     } 
     System.exit(InputReader.ASSISTING_ERROR); 
    } 

    return cursor; 

這是應用程序的摘錄。你可以做一個簡單的Complector,你必須提供一系列選項。最終,您將需要實現自己的CompletionHandler以改進選項呈現給用戶的方式。

完整代碼可用here

0

創建2個填充符,然後使用它們來完成arbituary參數。請注意,並非所有參數都需要完成。

List<Completer> completors = new LinkedList<>(); 

// Completes using the filesystem 
completors.add(new FileNameCompleter()); 

// Completes using random words 
completors.add(new StringsCompleter("--arg0", "--arg1", "command")); 

// Aggregate the above completors 
AggregateCompleter aggComp = new AggregateCompleter(completors); 

// Parse the buffer line and complete each token 
ArgumentCompleter argComp = new ArgumentCompleter(aggComp); 

// Don't require all completors to match 
argComp.setStrict(false); 

// Add it all together 
conReader.addCompleter(argComp); 
+0

我認爲你已經在這裏切換了AggregateCompleter和ArgumentCompleter。你也不需要aggregatecompleter只有1 Completer。 – 2017-08-28 20:55:27

0

刪除NullCompletor,你會得到你想要的。 NullCompletor確保你的整個命令只有3個字。