Skip to content

Commit

Permalink
OptionCompleter: added a test, script and improved comment
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 28, 2020
1 parent 4ae7e84 commit 09f14e5
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
13 changes: 11 additions & 2 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
* Copyright (c) 2002-2020, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -35,6 +35,7 @@
import org.jline.reader.Parser;
import org.jline.reader.impl.completer.AggregateCompleter;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.reader.ParsedLine;
import org.jline.terminal.Terminal;
Expand Down Expand Up @@ -720,6 +721,14 @@ protected static List<OptDesc> compile(Map<String,List<String>> optionValues, Co
return out;
}

/**
* Command option description. If option does not have short/long option assign to it null value.
* If option does not have value set valueCompleter = NullCompleter.INSTANCE
* @param shortOption '-s'
* @param longOption '--long'
* @param description short option description
* @param valueCompleter option value completer
*/
public OptDesc(String shortOption, String longOption, String description, org.jline.reader.Completer valueCompleter) {
this.shortOption = shortOption;
this.longOption = longOption;
Expand Down Expand Up @@ -759,7 +768,7 @@ protected String description() {
}

protected boolean hasValue() {
return valueCompleter != null;
return valueCompleter != null && valueCompleter != NullCompleter.INSTANCE;
}

protected org.jline.reader.Completer valueCompleter() {
Expand Down
51 changes: 51 additions & 0 deletions builtins/src/test/java/org/jline/builtins/OptionCompleterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2002-2020, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.builtins;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;

import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Completers.OptDesc;
import org.jline.reader.LineReader;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.Completer;
import org.junit.Test;


public class OptionCompleterTest extends ReaderTestSupport {

@Test
public void testOptions() throws Exception {
List<Completer> argsCompleters = new ArrayList<>();
List<OptDesc> options = new ArrayList<>();
argsCompleters.add(new StringsCompleter("bar", "rab"));
argsCompleters.add(new StringsCompleter("foo", "oof"));
argsCompleters.add(NullCompleter.INSTANCE);
options.add(new OptDesc("-s", "--sopt", new StringsCompleter("val", "lav")));
options.add(new OptDesc(null, "--option", NullCompleter.INSTANCE));

reader.setCompleter(new ArgumentCompleter(new StringsCompleter("command"),
new OptionCompleter(argsCompleters, options, 1))
);

assertBuffer("command ", new TestBuffer("c").tab());
assertBuffer("command -s", new TestBuffer("command -").tab());
assertBuffer("command -s val ", new TestBuffer("command -s v").tab());
assertBuffer("command -s val bar ", new TestBuffer("command -s val b").tab());
assertBuffer("command -s val bar --option ", new TestBuffer("command -s val bar --o").tab());
assertBuffer("command -s val bar --option foo ", new TestBuffer("command -s val bar --option f").tab());

}

}
6 changes: 5 additions & 1 deletion demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@
<resource>
<directory>src/main/scripts</directory>
<includes>
<include>hello*</include>
<include>*.jline</include>
<include>*.groovy</include>
</includes>
<excludes>
<exclude>init.jline</exclude>
</excludes>
</resource>
</resources>
</configuration>
Expand Down
28 changes: 28 additions & 0 deletions demo/src/main/scripts/optionCompleterTest.jline
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import org.jline.reader.impl.completer.*
import org.jline.reader.*
import org.jline.builtins.Completers.OptionCompleter
import org.jline.builtins.SystemRegistry

def complete(commandLine) {
candidates = []
completer.complete(reader, parser.parse(commandLine, commandLine.length(), Parser.ParseContext.ACCEPT_LINE), candidates)
candidates.each { println it.value()}
}

reader=null // reader is not really needed here!
parser=SystemRegistry.get().parser

options=['--opt1','--opt2']
optionValues=['--option':['val1','val2'],'--option2':['val3','val4']]
optionCompleter = new OptionCompleter(Arrays.asList(new StringsCompleter("p1", "p11")
, new StringsCompleter("p2", "p22"), NullCompleter.INSTANCE)
, optionValues, options, 1)
completer = new ArgumentCompleter(new StringsCompleter("Command1"), optionCompleter)
#
# test completer...
#
complete('Comm')
complete('Command1 --')
complete('Command1 --option=')
complete('Command1 --option=val1 ')
complete('Command1 p1 ')
1 change: 0 additions & 1 deletion hello.jline

This file was deleted.

1 comment on commit 09f14e5

@mattirn
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.