jline多参数解析

发布于 2024-09-29 12:30:10 字数 485 浏览 3 评论 0原文

我试图让 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 验证为日期吗?)

I am trying to get JLine to do tab completion so I can enter something like the following:

commandname --arg1 value1 --arg2 value2

I am using the following code:

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

consoleReader.addCompletor(new ArgumentCompletor(completors));

But after I type the value2 tab completion stops.

(Suplementary question, can I validate value1 as a date using jline?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单 2024-10-06 12:30:10

我遇到了同样的问题,我通过创建自己的类来使用 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 来改进向用户呈现选项的方式。

完整的代码可以此处

I had the same problem, and I solved it by creating my own classes to complete the commands with jLine. I just needed to implement my own Completor.

I am developing an application that could assist DBAs to type not only the command names, but also the parameters. I am using jLine for just for the Terminal interactions, and I created another Completor.

I have to provide the complete grammar to the Completor, and that is the objective of my application. It is called Zemucan and it is hosted in SourceForge; this application is initially focused to DB2, but any grammar could be incorporated. The example of the Completor I am using is:

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;

This is an extract of the application. You could do a simple Completor, and you have to provide an array of options. Eventually, you will want to implement your own CompletionHandler to improve the way that the options are presented to the user.

The complete code is available here.

王权女流氓 2024-10-06 12:30:10

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

Create 2 completors, then use them to complete arbituary arguments. Note that not all the arguments need to be completed.

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);
数理化全能战士 2024-10-06 12:30:10

删除 NullCompletor,您将得到您想要的。 NullCompletor 确保您的整个命令只有 3 个单词长。

Remove the NullCompletor and you will have what you want. NullCompletor makes sure your entire command is only 3 words long.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文