Skip to content

Commit 0ece35e

Browse files
czpilarfmbenhassine
authored andcommitted
Fix Boolean option handling when no value is supplied with arguments
Resolves #1309 Signed-off-by: David Pilar <david@czpilar.net>
1 parent 321675a commit 0ece35e

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

spring-shell-core/src/main/java/org/springframework/shell/core/command/DefaultCommandParser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ public ParsedInput parse(String input) {
121121
}
122122
nextWord = "true";
123123
}
124+
else if (isBooleanOption(commandName, currentWord) && !isBooleanValue(nextWord)) {
125+
nextWord = "true";
126+
}
124127
else {
125128
i++; // skip next word as it was used as option value
126129
}
@@ -199,4 +202,8 @@ private boolean isBooleanOption(String commandName, String currentWord) {
199202
.anyMatch(o -> o.type() == boolean.class || o.type() == Boolean.class);
200203
}
201204

205+
private boolean isBooleanValue(String rawValue) {
206+
return "true".equalsIgnoreCase(rawValue) || "false".equalsIgnoreCase(rawValue);
207+
}
208+
202209
}

spring-shell-core/src/test/java/org/springframework/shell/core/command/DefaultCommandParserTests.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,58 @@ static Stream<Arguments> parseWithBooleanOptionData() {
315315
Arguments.of("mycommand -o", "", 'o', Boolean.class, "true"));
316316
}
317317

318+
@ParameterizedTest
319+
@MethodSource("parseWithBooleanOptionAndArgumentData")
320+
void testParseWithBooleanOptionAndArgument(String input, String expectedValue, int expectedArguments,
321+
String expectedArgumentValue) {
322+
// given
323+
Command command = createCommand("mycommand", "My test command");
324+
command.getOptions().add(CommandOption.with().longName("option").type(boolean.class).build());
325+
commandRegistry.registerCommand(command);
326+
// when
327+
ParsedInput parsedInput = parser.parse(input);
328+
329+
// then
330+
assertEquals("mycommand", parsedInput.commandName());
331+
assertEquals(1, parsedInput.options().size());
332+
assertEquals(expectedValue, parsedInput.options().get(0).value());
333+
assertEquals(expectedArguments, parsedInput.arguments().size());
334+
if (expectedArguments > 0) {
335+
assertEquals(expectedArgumentValue, parsedInput.arguments().get(0).value());
336+
}
337+
}
338+
339+
static Stream<Arguments> parseWithBooleanOptionAndArgumentData() {
340+
return Stream.of(Arguments.of("mycommand --option=false", "false", 0, null),
341+
Arguments.of("mycommand --option=true", "true", 0, null),
342+
Arguments.of("mycommand --option false", "false", 0, null),
343+
Arguments.of("mycommand --option true", "true", 0, null),
344+
Arguments.of("mycommand --option", "true", 0, null),
345+
346+
Arguments.of("mycommand --option=false argument", "false", 1, "argument"),
347+
Arguments.of("mycommand --option=true argument", "true", 1, "argument"),
348+
Arguments.of("mycommand --option false argument", "false", 1, "argument"),
349+
Arguments.of("mycommand --option true argument", "true", 1, "argument"),
350+
Arguments.of("mycommand --option argument", "true", 1, "argument"),
351+
352+
Arguments.of("mycommand argument --option=false", "false", 1, "argument"),
353+
Arguments.of("mycommand argument --option=true", "true", 1, "argument"),
354+
Arguments.of("mycommand argument --option false", "false", 1, "argument"),
355+
Arguments.of("mycommand argument --option true", "true", 1, "argument"),
356+
Arguments.of("mycommand argument --option", "true", 1, "argument"),
357+
358+
Arguments.of("mycommand --option=false true", "false", 1, "true"),
359+
Arguments.of("mycommand --option=true true", "true", 1, "true"),
360+
Arguments.of("mycommand --option false false", "false", 1, "false"),
361+
Arguments.of("mycommand --option true false", "true", 1, "false"),
362+
363+
Arguments.of("mycommand false --option=false", "false", 1, "false"),
364+
Arguments.of("mycommand false --option=true", "true", 1, "false"),
365+
Arguments.of("mycommand true --option false", "false", 1, "true"),
366+
Arguments.of("mycommand true --option true", "true", 1, "true"),
367+
Arguments.of("mycommand true --option", "true", 1, "true"));
368+
}
369+
318370
@ParameterizedTest
319371
@ValueSource(strings = { "mycommand --help", "mycommand -h" })
320372
void testParseWithHelpOption(String input) {

0 commit comments

Comments
 (0)