Default boolean arg to false

- In a case where arg is given as boolean and with plain
  @ShellOption (user doesn't define defaults), configure
  arg not to be mandatory and with default value false.
- This brings this spesific case more close how it behave
  in older shell version.
- Having `@ShellOption boolean arg1` it now works as:
    my-shell:>e2e reg default-value-boolean3
    Hello false
    my-shell:>e2e reg default-value-boolean3 --arg1
    Hello true
    my-shell:>e2e reg default-value-boolean3 --arg1 false
    Hello false
    my-shell:>e2e reg default-value-boolean3 --arg1 true
    Hello true
- Fixes #461
This commit is contained in:
Janne Valkealahti
2022-07-18 10:29:31 +01:00
parent 5ba8e185bc
commit 643b189fb8
4 changed files with 504 additions and 1 deletions

View File

@@ -53,4 +53,83 @@ public class DefaultValueCommands extends BaseE2ECommands {
.and()
.build();
}
@ShellMethod(key = LEGACY_ANNO + "default-value-boolean1", group = GROUP)
public String testDefaultValueBoolean1(
@ShellOption(defaultValue = "false") boolean arg1
) {
return "Hello " + arg1;
}
@Bean
public CommandRegistration testDefaultValueBoolean1Registration() {
return CommandRegistration.builder()
.command(REG, "default-value-boolean1")
.group(GROUP)
.withOption()
.longNames("arg1")
.defaultValue("false")
.type(boolean.class)
.and()
.withTarget()
.function(ctx -> {
boolean arg1 = ctx.getOptionValue("arg1");
return "Hello " + arg1;
})
.and()
.build();
}
@ShellMethod(key = LEGACY_ANNO + "default-value-boolean2", group = GROUP)
public String testDefaultValueBoolean2(
@ShellOption(defaultValue = "true") boolean arg1
) {
return "Hello " + arg1;
}
@Bean
public CommandRegistration testDefaultValueBoolean2Registration() {
return CommandRegistration.builder()
.command(REG, "default-value-boolean2")
.group(GROUP)
.withOption()
.longNames("arg1")
.defaultValue("true")
.type(boolean.class)
.and()
.withTarget()
.function(ctx -> {
boolean arg1 = ctx.getOptionValue("arg1");
return "Hello " + arg1;
})
.and()
.build();
}
@ShellMethod(key = LEGACY_ANNO + "default-value-boolean3", group = GROUP)
public String testDefaultValueBoolean3(
@ShellOption boolean arg1
) {
return "Hello " + arg1;
}
@Bean
public CommandRegistration testDefaultValueBoolean3Registration() {
return CommandRegistration.builder()
.command(REG, "default-value-boolean3")
.group(GROUP)
.withOption()
.longNames("arg1")
.required(false)
.type(boolean.class)
.defaultValue("false")
.and()
.withTarget()
.function(ctx -> {
boolean arg1 = ctx.getOptionValue("arg1");
return "Hello " + arg1;
})
.and()
.build();
}
}