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) {
|
||||
|
||||
@@ -7,15 +7,21 @@ what a default `help` command outputs. Within a command documentation
|
||||
a type of an option is documented but this is not always super useful. Thus
|
||||
you may want to give better descriptive word for an option.
|
||||
|
||||
====
|
||||
[source, java, indent=0]
|
||||
NOTE: Label is not supported with `legacy annotation`.
|
||||
|
||||
[source,java,indent=0,role="primary"]
|
||||
.Programmatic
|
||||
----
|
||||
include::{snippets}/OptionSnippets.java[tag=option-registration-label]
|
||||
include::{snippets}/OptionSnippets.java[tag=option-label-programmatic]
|
||||
----
|
||||
|
||||
[source,java,indent=0,role="secondary"]
|
||||
.Annotation
|
||||
----
|
||||
include::{snippets}/OptionSnippets.java[tag=option-label-annotation]
|
||||
----
|
||||
====
|
||||
|
||||
Defining label is then shown in `help`.
|
||||
|
||||
====
|
||||
[source, bash]
|
||||
----
|
||||
|
||||
@@ -372,6 +372,13 @@ public class OptionSnippets {
|
||||
}
|
||||
// end::option-default-annotation[]
|
||||
|
||||
// tag::option-label-annotation[]
|
||||
void labelOption(
|
||||
@Option(label = "MYLABEL") String arg
|
||||
) {
|
||||
}
|
||||
// end::option-label-annotation[]
|
||||
|
||||
}
|
||||
|
||||
static class Registration {
|
||||
@@ -436,5 +443,15 @@ public class OptionSnippets {
|
||||
}
|
||||
// end::option-default-programmatic[]
|
||||
|
||||
// tag::option-label-programmatic[]
|
||||
CommandRegistration labelOption() {
|
||||
return CommandRegistration.builder()
|
||||
.withOption()
|
||||
.longNames("arg")
|
||||
.label("MYLABEL")
|
||||
.and()
|
||||
.build();
|
||||
}
|
||||
// end::option-label-programmatic[]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user