diff --git a/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java b/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java index 0310263e..7f2f64b2 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java @@ -1141,7 +1141,8 @@ public interface CommandRegistration { if (helpOptionsSpec != null) { String[] longNames = helpOptionsSpec.longNames != null ? helpOptionsSpec.longNames : null; Character[] shortNames = helpOptionsSpec.shortNames != null ? helpOptionsSpec.shortNames : null; - options.add(CommandOption.of(longNames, shortNames, "help for " + command)); + options.add(CommandOption.of(longNames, shortNames, "help for " + command, + ResolvableType.forType(void.class))); } return options; } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionTypeCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionTypeCommands.java index a463784c..ac87cc8a 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionTypeCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionTypeCommands.java @@ -230,4 +230,26 @@ public class OptionTypeCommands extends BaseE2ECommands { .and() .build(); } + + // + // Void + // + + @Bean + public CommandRegistration optionTypeVoidRegistration(Supplier builder) { + return builder.get() + .command(REG, "option-type-void") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(void.class) + .and() + .withTarget() + .function(ctx -> { + return "Hello "; + }) + .and() + .build(); + } + } diff --git a/spring-shell-standard-commands/src/main/java/org/springframework/shell/standard/commands/CommandInfoModel.java b/spring-shell-standard-commands/src/main/java/org/springframework/shell/standard/commands/CommandInfoModel.java index 2af7d6ba..fe79a27c 100644 --- a/spring-shell-standard-commands/src/main/java/org/springframework/shell/standard/commands/CommandInfoModel.java +++ b/spring-shell-standard-commands/src/main/java/org/springframework/shell/standard/commands/CommandInfoModel.java @@ -91,7 +91,17 @@ class CommandInfoModel { return o.getLabel(); } else { - return o.getType() == null ? "String" : ClassUtils.getShortName(o.getType().getRawClass()); + if (o.getType() != null) { + if (ClassUtils.isAssignable(o.getType().getRawClass(), Void.class)) { + return ""; + } + else { + return ClassUtils.getShortName(o.getType().getRawClass()); + } + } + else { + return "String"; + } } } diff --git a/spring-shell-standard-commands/src/test/java/org/springframework/shell/standard/commands/CommandInfoModelTests.java b/spring-shell-standard-commands/src/test/java/org/springframework/shell/standard/commands/CommandInfoModelTests.java index 3da517af..85fbff92 100644 --- a/spring-shell-standard-commands/src/test/java/org/springframework/shell/standard/commands/CommandInfoModelTests.java +++ b/spring-shell-standard-commands/src/test/java/org/springframework/shell/standard/commands/CommandInfoModelTests.java @@ -122,4 +122,37 @@ public class CommandInfoModelTests { CommandInfoModel cim = CommandInfoModel.of("main1", r1); assertThat(cim.getAliases()).containsExactly("alias1"); } + + @Test + void voidTypeUsesEmptyStringAsName() { + CommandRegistration r1 = CommandRegistration.builder() + .command("main1") + .withOption() + .longNames("arg1") + .type(void.class) + .and() + .withTarget() + .consumer(ctx -> {}) + .and() + .build(); + CommandInfoModel cim1 = CommandInfoModel.of("main1", r1); + assertThat(cim1.getParameters()).hasSize(1); + assertThat(cim1.getParameters().get(0).getArguments()).containsExactly("--arg1"); + assertThat(cim1.getParameters().get(0).getType()).isEmpty(); + + CommandRegistration r2 = CommandRegistration.builder() + .command("main1") + .withOption() + .longNames("arg1") + .type(Void.class) + .and() + .withTarget() + .consumer(ctx -> {}) + .and() + .build(); + CommandInfoModel cim2 = CommandInfoModel.of("main1", r2); + assertThat(cim2.getParameters()).hasSize(1); + assertThat(cim2.getParameters().get(0).getArguments()).containsExactly("--arg1"); + assertThat(cim2.getParameters().get(0).getType()).isEmpty(); + } }