1

次のようなものを入力できるように、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 を日付として検証できますか?)

4

3 に答える 3

3

同じ問題があり、jLineでコマンドを完了するための独自のクラスを作成することで問題を解決しました。自分のCompletorを実装する必要がありました。

私は、DBAがコマンド名だけでなくパラメーターも入力するのを支援できるアプリケーションを開発しています。ターミナルの相互作用のためだけにjLineを使用しており、別のCompletorを作成しました。

私は完全な文法をコンプリーターに提供する必要があり、それが私のアプリケーションの目的です。Zemucanと呼ばれ、SourceForgeでホストされています。このアプリケーションは当初DB2に焦点を合わせていますが、任意の文法を組み込むことができます。私が使用しているコンプリーターの例は次のとおりです。

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を実装して、オプションがユーザーに表示される方法を改善する必要があります。

完全なコードはこちらから入手できます。

于 2011-10-25T08:31:03.743 に答える