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

@@ -158,7 +158,13 @@ public class StandardMethodTargetRegistrar implements MethodTargetRegistrar, App
optionSpec.defaultValue(so.defaultValue());
}
if (ObjectUtils.nullSafeEquals(so.defaultValue(), ShellOption.NONE)) {
optionSpec.required();
if (ClassUtils.isAssignable(boolean.class, parameterType)) {
optionSpec.required(false);
optionSpec.defaultValue("false");
}
else {
optionSpec.required();
}
}
if (!ClassUtils.isAssignable(NoValueProvider.class, so.valueProvider())) {
CompletionResolver completionResolver = ctx -> {

View File

@@ -405,4 +405,42 @@ public class StandardMethodTargetRegistrarTests {
public void foo3(@ShellOption(defaultValue = ShellOption.NULL) String arg1) {
}
}
@Test
public void testOptionValuesWithBoolean() {
applicationContext = new AnnotationConfigApplicationContext(ValuesWithBoolean.class);
registrar.setApplicationContext(applicationContext);
registrar.register(catalog);
assertThat(catalog.getRegistrations().get("foo1")).isNotNull();
assertThat(catalog.getRegistrations().get("foo1").getOptions()).hasSize(1);
assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).getDefaultValue()).isEqualTo("false");
assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).isRequired()).isFalse();
assertThat(catalog.getRegistrations().get("foo2")).isNotNull();
assertThat(catalog.getRegistrations().get("foo2").getOptions()).hasSize(1);
assertThat(catalog.getRegistrations().get("foo2").getOptions().get(0).getDefaultValue()).isEqualTo("true");
assertThat(catalog.getRegistrations().get("foo2").getOptions().get(0).isRequired()).isFalse();
assertThat(catalog.getRegistrations().get("foo3")).isNotNull();
assertThat(catalog.getRegistrations().get("foo3").getOptions()).hasSize(1);
assertThat(catalog.getRegistrations().get("foo3").getOptions().get(0).isRequired()).isFalse();
assertThat(catalog.getRegistrations().get("foo3").getOptions().get(0).getDefaultValue()).isEqualTo("false");
}
@ShellComponent
public static class ValuesWithBoolean {
@ShellMethod(value = "foo1")
public void foo1(@ShellOption(defaultValue = "false") boolean arg1) {
}
@ShellMethod(value = "foo2")
public void foo2(@ShellOption(defaultValue = "true") boolean arg1) {
}
@ShellMethod(value = "foo3")
public void foo3(@ShellOption boolean arg1) {
}
}
}