Support label with @Option
- Add `label` field into `@Option`. This sets option label if `label` field has any text. - Fixes #732
This commit is contained in:
@@ -70,6 +70,13 @@ public @interface Option {
|
||||
*/
|
||||
String description() default "";
|
||||
|
||||
/**
|
||||
* Return a label of the option.
|
||||
*
|
||||
* @return label of the option
|
||||
*/
|
||||
String label() default "";
|
||||
|
||||
/**
|
||||
* Define option arity.
|
||||
*
|
||||
|
||||
@@ -251,6 +251,9 @@ class CommandRegistrationFactoryBean implements FactoryBean<CommandRegistration>
|
||||
optionSpec.shortNames(shortNames.toArray(new Character[0]));
|
||||
optionSpec.position(mp.getParameterIndex());
|
||||
optionSpec.description(so.description());
|
||||
if (StringUtils.hasText(so.label())) {
|
||||
optionSpec.label(so.label());
|
||||
}
|
||||
int arityMin = so.arityMin();
|
||||
int arityMax = so.arityMax();
|
||||
if (arityMin > -1) {
|
||||
|
||||
@@ -305,14 +305,27 @@ class CommandRegistrationFactoryBeanTests {
|
||||
@Command
|
||||
void command4(@Option(longNames = "arg", arityMax = 2, arity = OptionArity.EXACTLY_ONE) String arg) {
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
CompletionProvider completionProvider() {
|
||||
return ctx -> {
|
||||
return Collections.emptyList();
|
||||
};
|
||||
@Test
|
||||
void setsOptionWithLabel() {
|
||||
configCommon(OptionWithLabel.class, new OptionWithLabel(), "command1", new Class[] { String.class })
|
||||
.run((context) -> {
|
||||
CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF,
|
||||
CommandRegistrationFactoryBean.class);
|
||||
assertThat(fb).isNotNull();
|
||||
CommandRegistration registration = fb.getObject();
|
||||
assertThat(registration).isNotNull();
|
||||
assertThat(registration.getOptions().get(0).getLabel()).isEqualTo("label");
|
||||
});
|
||||
}
|
||||
|
||||
@Command
|
||||
private static class OptionWithLabel {
|
||||
|
||||
@Command
|
||||
void command1(@Option(longNames = "arg", label = "label") String arg) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private <T> ApplicationContextRunner configCommon(Class<T> type, T bean) {
|
||||
|
||||
Reference in New Issue
Block a user