Skip to content

Commit f5d6bae

Browse files
committed
Handle option void type
- There's been no explicit support having argument void type as you would not be able to use with annotation model but surely can be wrapped as Type with CommandRegistration. - For now change CommandInfoModel so that it uses empty string which is i.e. expected in help options which are just used as flags. - It's outside of this commit to change parser to fail if user gives an argument value for this type of options. - Fixes #586
1 parent ef191e6 commit f5d6bae

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,8 @@ public List<CommandOption> getOptions() {
11411141
if (helpOptionsSpec != null) {
11421142
String[] longNames = helpOptionsSpec.longNames != null ? helpOptionsSpec.longNames : null;
11431143
Character[] shortNames = helpOptionsSpec.shortNames != null ? helpOptionsSpec.shortNames : null;
1144-
options.add(CommandOption.of(longNames, shortNames, "help for " + command));
1144+
options.add(CommandOption.of(longNames, shortNames, "help for " + command,
1145+
ResolvableType.forType(void.class)));
11451146
}
11461147
return options;
11471148
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,26 @@ public CommandRegistration optionTypeEnumRegistration(Supplier<CommandRegistrati
230230
.and()
231231
.build();
232232
}
233+
234+
//
235+
// Void
236+
//
237+
238+
@Bean
239+
public CommandRegistration optionTypeVoidRegistration(Supplier<CommandRegistration.Builder> builder) {
240+
return builder.get()
241+
.command(REG, "option-type-void")
242+
.group(GROUP)
243+
.withOption()
244+
.longNames("arg1")
245+
.type(void.class)
246+
.and()
247+
.withTarget()
248+
.function(ctx -> {
249+
return "Hello ";
250+
})
251+
.and()
252+
.build();
253+
}
254+
233255
}

spring-shell-standard-commands/src/main/java/org/springframework/shell/standard/commands/CommandInfoModel.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,17 @@ private static String commandOptionType(CommandOption o) {
9191
return o.getLabel();
9292
}
9393
else {
94-
return o.getType() == null ? "String" : ClassUtils.getShortName(o.getType().getRawClass());
94+
if (o.getType() != null) {
95+
if (ClassUtils.isAssignable(o.getType().getRawClass(), Void.class)) {
96+
return "";
97+
}
98+
else {
99+
return ClassUtils.getShortName(o.getType().getRawClass());
100+
}
101+
}
102+
else {
103+
return "String";
104+
}
95105
}
96106
}
97107

spring-shell-standard-commands/src/test/java/org/springframework/shell/standard/commands/CommandInfoModelTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,37 @@ void hasAliases() {
122122
CommandInfoModel cim = CommandInfoModel.of("main1", r1);
123123
assertThat(cim.getAliases()).containsExactly("alias1");
124124
}
125+
126+
@Test
127+
void voidTypeUsesEmptyStringAsName() {
128+
CommandRegistration r1 = CommandRegistration.builder()
129+
.command("main1")
130+
.withOption()
131+
.longNames("arg1")
132+
.type(void.class)
133+
.and()
134+
.withTarget()
135+
.consumer(ctx -> {})
136+
.and()
137+
.build();
138+
CommandInfoModel cim1 = CommandInfoModel.of("main1", r1);
139+
assertThat(cim1.getParameters()).hasSize(1);
140+
assertThat(cim1.getParameters().get(0).getArguments()).containsExactly("--arg1");
141+
assertThat(cim1.getParameters().get(0).getType()).isEmpty();
142+
143+
CommandRegistration r2 = CommandRegistration.builder()
144+
.command("main1")
145+
.withOption()
146+
.longNames("arg1")
147+
.type(Void.class)
148+
.and()
149+
.withTarget()
150+
.consumer(ctx -> {})
151+
.and()
152+
.build();
153+
CommandInfoModel cim2 = CommandInfoModel.of("main1", r2);
154+
assertThat(cim2.getParameters()).hasSize(1);
155+
assertThat(cim2.getParameters().get(0).getArguments()).containsExactly("--arg1");
156+
assertThat(cim2.getParameters().get(0).getType()).isEmpty();
157+
}
125158
}

0 commit comments

Comments
 (0)