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
This commit is contained in:
Janne Valkealahti
2022-10-14 06:33:44 +01:00
parent df45b625c7
commit 3fe26025cd
3 changed files with 45 additions and 3 deletions

View File

@@ -323,6 +323,11 @@ public interface CommandParser {
List<String> subArgs = lr.subList(1, lr.size());
ConvertArgumentsHolder holder = convertArguments(o, subArgs);
Object value = holder.value;
if (conversionService != null && o.getType() != null && value != null) {
if (conversionService.canConvert(value.getClass(), o.getType().getRawClass())) {
value = conversionService.convert(value, o.getType().getRawClass());
}
}
Stream<ParserResult> unmapped = holder.unmapped.stream()
.map(um -> ParserResult.of(null, Arrays.asList(um), null, null));
Stream<ParserResult> res = Stream.of(ParserResult.of(o, subArgs, value, null));
@@ -341,7 +346,7 @@ public interface CommandParser {
defaultValueOptionsToCheck.remove(pr.option);
}
});
defaultValueOptionsToCheck.stream()
defaultValueOptionsToCheck.stream()
.filter(co -> co.getDefaultValue() != null)
.forEach(co -> {
Object value = co.getDefaultValue();

View File

@@ -292,7 +292,7 @@ public class CommandParserTests extends AbstractCommandTests {
CommandParserResults results = parser.parse(options, args);
assertThat(results.results()).hasSize(1);
assertThat(results.results().get(0).option()).isSameAs(option1);
assertThat(results.results().get(0).value()).isEqualTo(new String[] { "1", "2" });
assertThat(results.results().get(0).value()).isEqualTo(new int[] { 1, 2 });
}
@Test
@@ -337,6 +337,34 @@ public class CommandParserTests extends AbstractCommandTests {
assertThat(results.results().get(0).value()).isEqualTo(true);
}
@Test
public void testIntegerWithDefault() {
ResolvableType type = ResolvableType.forType(Integer.class);
CommandOption option1 = CommandOption.of(new String[] { "arg1" }, new Character[0], "description", type, false,
"1", null, null, null, null, null);
List<CommandOption> options = Arrays.asList(option1);
String[] args = new String[]{};
CommandParserResults results = parser.parse(options, args);
assertThat(results.results()).hasSize(1);
assertThat(results.results().get(0).option()).isSameAs(option1);
assertThat(results.results().get(0).value()).isEqualTo(1);
}
@Test
public void testIntegerWithGivenValue() {
ResolvableType type = ResolvableType.forType(Integer.class);
CommandOption option1 = CommandOption.of(new String[] { "arg1" }, new Character[0], "description", type, false,
null, null, null, null, null, null);
List<CommandOption> options = Arrays.asList(option1);
String[] args = new String[] { "--arg1", "1" };
CommandParserResults results = parser.parse(options, args);
assertThat(results.results()).hasSize(1);
assertThat(results.results().get(0).option()).isSameAs(option1);
assertThat(results.results().get(0).value()).isEqualTo(1);
}
private static CommandOption longOption(String name) {
return longOption(name, null);
}

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.shell.samples.e2e;
import java.io.PrintWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.standard.ShellComponent;
@@ -48,7 +50,14 @@ public class OptionTypeCommands extends BaseE2ECommands {
.label("MYLABEL")
.and()
.withTarget()
.consumer(ctx -> {})
.consumer(ctx -> {
PrintWriter writer = ctx.getTerminal().writer();
if (ctx.hasMappedOption("arg3")) {
int v = ctx.getOptionValue("arg3");
writer.append("arg3=" + Integer.toString(v) + "\n");
}
writer.flush();
})
.and()
.build();
}