【问题标题】:jline multi-argument parsingjline 多参数解析
【发布时间】:2017-08-29 04:54:43
【问题描述】:

我正在尝试让 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 选项卡后完成停止。

(补充问题,我可以使用 jline 将 value1 验证为日期吗?)

【问题讨论】:

    标签: java jline


    【解决方案1】:

    我遇到了同样的问题,我通过创建自己的类来完成使用 jLine 的命令来解决它。我只需要实现我自己的 Completor。

    我正在开发一个应用程序,它不仅可以帮助 DBA 输入命令名称,还可以输入参数。我将 jLine 仅用于终端交互,并创建了另一个 Completor。

    我必须向 Completor 提供完整的语法,这就是我申请的目标。它被称为 Zemucan,托管在 SourceForge 中;此应用程序最初专注于 DB2,但可以合并任何语法。我正在使用的 Completor 示例是:

    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;
    

    这是应用程序的摘录。你可以做一个简单的 Completor,你必须提供一系列选项。最终,您将希望实现自己的 CompletionHandler 以改进向用户呈现选项的方式。

    完整代码可在here获取。

    【讨论】:

      【解决方案2】:

      创建 2 个完成器,然后使用它们来完成任意参数。请注意,并非所有参数都需要完成。

      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);
      

      【讨论】:

      • 我认为您在这里切换了 AggregateCompleter 和 ArgumentCompleter。此外,您不需要聚合完成器来仅 1 个完成器。
      【解决方案3】:

      删除 NullCompletor,你就会得到你想要的。 NullCompletor 确保您的整个命令只有 3 个字长。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-29
        • 1970-01-01
        • 2021-09-16
        • 1970-01-01
        • 1970-01-01
        • 2020-08-19
        • 2011-02-02
        • 1970-01-01
        相关资源
        最近更新 更多