Fix ShellOption with required
- Where applicable mark option required so that user is notified instead of blindly using null. - Fixes #436
This commit is contained in:
@@ -140,6 +140,9 @@ public class StandardMethodTargetRegistrar implements MethodTargetRegistrar, App
|
||||
&& !ObjectUtils.nullSafeEquals(so.defaultValue(), ShellOption.NULL)) {
|
||||
optionSpec.defaultValue(so.defaultValue());
|
||||
}
|
||||
else {
|
||||
optionSpec.required();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -64,7 +64,7 @@ public class StandardMethodTargetRegistrarTests {
|
||||
|
||||
@Test
|
||||
public void testRegistrations() {
|
||||
applicationContext = new AnnotationConfigApplicationContext(Sample.class);
|
||||
applicationContext = new AnnotationConfigApplicationContext(Sample1.class);
|
||||
registrar.setApplicationContext(applicationContext);
|
||||
registrar.register(catalog);
|
||||
Map<String, CommandRegistration> registrations = catalog.getRegistrations();
|
||||
@@ -74,6 +74,7 @@ public class StandardMethodTargetRegistrarTests {
|
||||
assertThat(registrations.get("say-hello").getAvailability()).isNotNull();
|
||||
assertThat(registrations.get("say-hello").getOptions()).hasSize(1);
|
||||
assertThat(registrations.get("say-hello").getOptions().get(0).getLongNames()).containsExactly("what");
|
||||
assertThat(registrations.get("say-hello").getOptions().get(0).isRequired()).isTrue();
|
||||
|
||||
assertThat(registrations.get("hi")).isNotNull();
|
||||
assertThat(registrations.get("hi").getAvailability()).isNotNull();
|
||||
@@ -83,7 +84,7 @@ public class StandardMethodTargetRegistrarTests {
|
||||
}
|
||||
|
||||
@ShellComponent
|
||||
public static class Sample {
|
||||
public static class Sample1 {
|
||||
|
||||
@ShellMethod("some command")
|
||||
public String sayHello(String what) {
|
||||
@@ -96,6 +97,28 @@ public class StandardMethodTargetRegistrarTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptionRequiredWithAnnotation() {
|
||||
applicationContext = new AnnotationConfigApplicationContext(Sample2.class);
|
||||
registrar.setApplicationContext(applicationContext);
|
||||
registrar.register(catalog);
|
||||
Map<String, CommandRegistration> registrations = catalog.getRegistrations();
|
||||
assertThat(registrations).hasSize(1);
|
||||
|
||||
assertThat(registrations.get("say-hello")).isNotNull();
|
||||
assertThat(registrations.get("say-hello").getOptions()).hasSize(1);
|
||||
assertThat(registrations.get("say-hello").getOptions().get(0).isRequired()).isTrue();
|
||||
}
|
||||
|
||||
@ShellComponent
|
||||
public static class Sample2 {
|
||||
|
||||
@ShellMethod("some command")
|
||||
public String sayHello(@ShellOption String what) {
|
||||
return "hello " + what;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAvailabilityIndicators() {
|
||||
applicationContext = new AnnotationConfigApplicationContext(SampleWithAvailability.class);
|
||||
|
||||
Reference in New Issue
Block a user