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
This commit is contained in:
Janne Valkealahti
2022-12-05 09:30:06 +00:00
parent ef191e66f3
commit f5d6bae117
4 changed files with 68 additions and 2 deletions

View File

@@ -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;
}

View File

@@ -230,4 +230,26 @@ public class OptionTypeCommands extends BaseE2ECommands {
.and()
.build();
}
//
// Void
//
@Bean
public CommandRegistration optionTypeVoidRegistration(Supplier<CommandRegistration.Builder> 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();
}
}

View File

@@ -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";
}
}
}

View File

@@ -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();
}
}