SHL-98: check option availability in case insensitive way
Add unit test, format code
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -10,4 +10,6 @@ target/
|
|||||||
/*.log
|
/*.log
|
||||||
|
|
||||||
/.idea
|
/.idea
|
||||||
/*.iml
|
/*.iml
|
||||||
|
/*.ipr
|
||||||
|
/*.iws
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ import org.springframework.util.StringUtils;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Default implementation of {@link Parser}.
|
* Default implementation of {@link Parser}.
|
||||||
*
|
*
|
||||||
* @author Ben Alex
|
* @author Ben Alex
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
@@ -81,7 +81,7 @@ public class SimpleParser implements Parser {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* get all mandatory options keys. For the options with multiple keys, the keys will be in one row.
|
* get all mandatory options keys. For the options with multiple keys, the keys will be in one row.
|
||||||
*
|
*
|
||||||
* @param cliOptions options
|
* @param cliOptions options
|
||||||
* @return mandatory options keys
|
* @return mandatory options keys
|
||||||
*/
|
*/
|
||||||
@@ -91,7 +91,7 @@ public class SimpleParser implements Parser {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* get all options key.
|
* get all options key.
|
||||||
*
|
*
|
||||||
* @param cliOptions
|
* @param cliOptions
|
||||||
* @param includeOptionalOptions
|
* @param includeOptionalOptions
|
||||||
* @return options keys
|
* @return options keys
|
||||||
@@ -393,7 +393,7 @@ public class SimpleParser implements Parser {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalises the given raw user input string ready for parsing
|
* Normalises the given raw user input string ready for parsing
|
||||||
*
|
*
|
||||||
* @param rawInput the string to normalise; can't be <code>null</code>
|
* @param rawInput the string to normalise; can't be <code>null</code>
|
||||||
* @return a non-<code>null</code> string
|
* @return a non-<code>null</code> string
|
||||||
*/
|
*/
|
||||||
@@ -407,12 +407,12 @@ public class SimpleParser implements Parser {
|
|||||||
Set<String> cliOptionKeySet = new LinkedHashSet<String>();
|
Set<String> cliOptionKeySet = new LinkedHashSet<String>();
|
||||||
for (CliOption cliOption : cliOptions) {
|
for (CliOption cliOption : cliOptions) {
|
||||||
for (String key : cliOption.key()) {
|
for (String key : cliOption.key()) {
|
||||||
cliOptionKeySet.add(key.toLowerCase());
|
cliOptionKeySet.add(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Set<String> unavailableOptions = new LinkedHashSet<String>();
|
Set<String> unavailableOptions = new LinkedHashSet<String>();
|
||||||
for (String suppliedOption : options.keySet()) {
|
for (String suppliedOption : options.keySet()) {
|
||||||
if (!cliOptionKeySet.contains(suppliedOption.toLowerCase())) {
|
if (!cliOptionKeySet.contains(suppliedOption)) {
|
||||||
unavailableOptions.add(suppliedOption);
|
unavailableOptions.add(suppliedOption);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -499,7 +499,8 @@ public class SimpleParser implements Parser {
|
|||||||
String bufferToReturn = null;
|
String bufferToReturn = null;
|
||||||
String lastWord = null;
|
String lastWord = null;
|
||||||
|
|
||||||
next_buffer_loop: for (int bufferIndex = 0; bufferIndex < buffer.length(); bufferIndex++) {
|
next_buffer_loop:
|
||||||
|
for (int bufferIndex = 0; bufferIndex < buffer.length(); bufferIndex++) {
|
||||||
String bufferSoFarIncludingThis = buffer.substring(0, bufferIndex + 1);
|
String bufferSoFarIncludingThis = buffer.substring(0, bufferIndex + 1);
|
||||||
String bufferRemaining = buffer.substring(bufferIndex + 1);
|
String bufferRemaining = buffer.substring(bufferIndex + 1);
|
||||||
|
|
||||||
@@ -649,7 +650,7 @@ public class SimpleParser implements Parser {
|
|||||||
for (String value : cmd.value()) {
|
for (String value : cmd.value()) {
|
||||||
if (buffer.startsWith(value) || value.startsWith(buffer)) {
|
if (buffer.startsWith(value) || value.startsWith(buffer)) {
|
||||||
results.add(new Completion(value)); // no space at the end, as there's no need to continue the
|
results.add(new Completion(value)); // no space at the end, as there's no need to continue the
|
||||||
// command further
|
// command further
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
candidates.addAll(results);
|
candidates.addAll(results);
|
||||||
@@ -750,7 +751,7 @@ public class SimpleParser implements Parser {
|
|||||||
// option key/value pair)
|
// option key/value pair)
|
||||||
if (lastOptionKey == null
|
if (lastOptionKey == null
|
||||||
|| (!"".equals(lastOptionKey) && !"".equals(lastOptionValue) && translated.endsWith(" ") && !tokenizer
|
|| (!"".equals(lastOptionKey) && !"".equals(lastOptionValue) && translated.endsWith(" ") && !tokenizer
|
||||||
.openingQuotesHaveNotBeenClosed())) {
|
.openingQuotesHaveNotBeenClosed())) {
|
||||||
// We have either NEVER specified an option key/value pair
|
// We have either NEVER specified an option key/value pair
|
||||||
// OR we have specified a full option key/value pair
|
// OR we have specified a full option key/value pair
|
||||||
|
|
||||||
@@ -978,7 +979,7 @@ public class SimpleParser implements Parser {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* populate completion for mandatory options
|
* populate completion for mandatory options
|
||||||
*
|
*
|
||||||
* @param translated user's input
|
* @param translated user's input
|
||||||
* @param unspecified unspecified options
|
* @param unspecified unspecified options
|
||||||
* @param value the option key
|
* @param value the option key
|
||||||
@@ -1002,7 +1003,7 @@ public class SimpleParser implements Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void obtainHelp(
|
public void obtainHelp(
|
||||||
@CliOption(key = { "", "command" }, optionContext = "availableCommands", help = "Command name to provide help for")
|
@CliOption(key = {"", "command"}, optionContext = "availableCommands", help = "Command name to provide help for")
|
||||||
String buffer) {
|
String buffer) {
|
||||||
synchronized (mutex) {
|
synchronized (mutex) {
|
||||||
if (buffer == null) {
|
if (buffer == null) {
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ import org.hamcrest.DiagnosingMatcher;
|
|||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.shell.converters.StringConverter;
|
||||||
import org.springframework.shell.core.annotation.CliCommand;
|
import org.springframework.shell.core.annotation.CliCommand;
|
||||||
import org.springframework.shell.core.annotation.CliOption;
|
import org.springframework.shell.core.annotation.CliOption;
|
||||||
import org.springframework.shell.event.ParseResult;
|
import org.springframework.shell.event.ParseResult;
|
||||||
@@ -428,6 +430,17 @@ public class SimpleParserTests {
|
|||||||
assertThat(result, nullValue(ParseResult.class));
|
assertThat(result, nullValue(ParseResult.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMixedCaseOptions_SHL_98() {
|
||||||
|
parser.add(new MixedCaseOptions());
|
||||||
|
parser.add(new StringConverter());
|
||||||
|
ParseResult result = parser.parse("foo --upPer bar");
|
||||||
|
assertThat(result.getMethod().getName(), equalTo("foo"));
|
||||||
|
|
||||||
|
result = parser.parse("foo --upper fizz");
|
||||||
|
assertThat(result, nullValue());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a matcher that asserts that a completion, when added to {@link #buffer} at the given {@link #offset},
|
* Return a matcher that asserts that a completion, when added to {@link #buffer} at the given {@link #offset},
|
||||||
* indeed matches the provided matcher.
|
* indeed matches the provided matcher.
|
||||||
@@ -523,6 +536,18 @@ public class SimpleParserTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class MixedCaseOptions implements CommandMarker {
|
||||||
|
@CliCommand(value = "foo")
|
||||||
|
public String foo(@CliOption(key = "upPer")
|
||||||
|
String arg) {
|
||||||
|
return "foo " + arg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static class StringCompletions implements Converter<String> {
|
public static class StringCompletions implements Converter<String> {
|
||||||
|
|
||||||
private final List<String> completions;
|
private final List<String> completions;
|
||||||
|
|||||||
Reference in New Issue
Block a user