Skip to content

Commit 3fe2602

Browse files
committed
Process given values is a parser
- Modify CommandParser to convert given option value if its type is defined - This change makes option default value to behave same as given value what comes for the actual value in a CommandContext. - Fixes #548
1 parent df45b62 commit 3fe2602

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ ParserResults visit(List<List<String>> lexerResults, List<CommandOption> options
323323
List<String> subArgs = lr.subList(1, lr.size());
324324
ConvertArgumentsHolder holder = convertArguments(o, subArgs);
325325
Object value = holder.value;
326+
if (conversionService != null && o.getType() != null && value != null) {
327+
if (conversionService.canConvert(value.getClass(), o.getType().getRawClass())) {
328+
value = conversionService.convert(value, o.getType().getRawClass());
329+
}
330+
}
326331
Stream<ParserResult> unmapped = holder.unmapped.stream()
327332
.map(um -> ParserResult.of(null, Arrays.asList(um), null, null));
328333
Stream<ParserResult> res = Stream.of(ParserResult.of(o, subArgs, value, null));
@@ -341,7 +346,7 @@ ParserResults visit(List<List<String>> lexerResults, List<CommandOption> options
341346
defaultValueOptionsToCheck.remove(pr.option);
342347
}
343348
});
344-
defaultValueOptionsToCheck.stream()
349+
defaultValueOptionsToCheck.stream()
345350
.filter(co -> co.getDefaultValue() != null)
346351
.forEach(co -> {
347352
Object value = co.getDefaultValue();

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public void testLongOptionsWithArray() {
292292
CommandParserResults results = parser.parse(options, args);
293293
assertThat(results.results()).hasSize(1);
294294
assertThat(results.results().get(0).option()).isSameAs(option1);
295-
assertThat(results.results().get(0).value()).isEqualTo(new String[] { "1", "2" });
295+
assertThat(results.results().get(0).value()).isEqualTo(new int[] { 1, 2 });
296296
}
297297

298298
@Test
@@ -337,6 +337,34 @@ public void testBooleanWithDefault() {
337337
assertThat(results.results().get(0).value()).isEqualTo(true);
338338
}
339339

340+
@Test
341+
public void testIntegerWithDefault() {
342+
ResolvableType type = ResolvableType.forType(Integer.class);
343+
CommandOption option1 = CommandOption.of(new String[] { "arg1" }, new Character[0], "description", type, false,
344+
"1", null, null, null, null, null);
345+
346+
List<CommandOption> options = Arrays.asList(option1);
347+
String[] args = new String[]{};
348+
CommandParserResults results = parser.parse(options, args);
349+
assertThat(results.results()).hasSize(1);
350+
assertThat(results.results().get(0).option()).isSameAs(option1);
351+
assertThat(results.results().get(0).value()).isEqualTo(1);
352+
}
353+
354+
@Test
355+
public void testIntegerWithGivenValue() {
356+
ResolvableType type = ResolvableType.forType(Integer.class);
357+
CommandOption option1 = CommandOption.of(new String[] { "arg1" }, new Character[0], "description", type, false,
358+
null, null, null, null, null, null);
359+
360+
List<CommandOption> options = Arrays.asList(option1);
361+
String[] args = new String[] { "--arg1", "1" };
362+
CommandParserResults results = parser.parse(options, args);
363+
assertThat(results.results()).hasSize(1);
364+
assertThat(results.results().get(0).option()).isSameAs(option1);
365+
assertThat(results.results().get(0).value()).isEqualTo(1);
366+
}
367+
340368
private static CommandOption longOption(String name) {
341369
return longOption(name, null);
342370
}

spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionTypeCommands.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.shell.samples.e2e;
1717

18+
import java.io.PrintWriter;
19+
1820
import org.springframework.context.annotation.Bean;
1921
import org.springframework.shell.command.CommandRegistration;
2022
import org.springframework.shell.standard.ShellComponent;
@@ -48,7 +50,14 @@ public CommandRegistration testOptionTypeRegistration() {
4850
.label("MYLABEL")
4951
.and()
5052
.withTarget()
51-
.consumer(ctx -> {})
53+
.consumer(ctx -> {
54+
PrintWriter writer = ctx.getTerminal().writer();
55+
if (ctx.hasMappedOption("arg3")) {
56+
int v = ctx.getOptionValue("arg3");
57+
writer.append("arg3=" + Integer.toString(v) + "\n");
58+
}
59+
writer.flush();
60+
})
5261
.and()
5362
.build();
5463
}

0 commit comments

Comments
 (0)