Skip to content

Commit a0ad3ad

Browse files
committed
Support label with @option
- Add `label` field into `@Option`. This sets option label if `label` field has any text. - Fixes #732
1 parent 755cf66 commit a0ad3ad

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

spring-shell-core/src/main/java/org/springframework/shell/command/annotation/Option.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@
7070
*/
7171
String description() default "";
7272

73+
/**
74+
* Return a label of the option.
75+
*
76+
* @return label of the option
77+
*/
78+
String label() default "";
79+
7380
/**
7481
* Define option arity.
7582
*

spring-shell-core/src/main/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBean.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ private void onCommandParameter(MethodParameter mp, Builder builder) {
251251
optionSpec.shortNames(shortNames.toArray(new Character[0]));
252252
optionSpec.position(mp.getParameterIndex());
253253
optionSpec.description(so.description());
254+
if (StringUtils.hasText(so.label())) {
255+
optionSpec.label(so.label());
256+
}
254257
int arityMin = so.arityMin();
255258
int arityMax = so.arityMax();
256259
if (arityMin > -1) {

spring-shell-core/src/test/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBeanTests.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,27 @@ void command3(@Option(longNames = "arg", arityMax = 2) String arg) {
305305
@Command
306306
void command4(@Option(longNames = "arg", arityMax = 2, arity = OptionArity.EXACTLY_ONE) String arg) {
307307
}
308+
}
308309

309-
@Bean
310-
CompletionProvider completionProvider() {
311-
return ctx -> {
312-
return Collections.emptyList();
313-
};
314-
}
310+
@Test
311+
void setsOptionWithLabel() {
312+
configCommon(OptionWithLabel.class, new OptionWithLabel(), "command1", new Class[] { String.class })
313+
.run((context) -> {
314+
CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF,
315+
CommandRegistrationFactoryBean.class);
316+
assertThat(fb).isNotNull();
317+
CommandRegistration registration = fb.getObject();
318+
assertThat(registration).isNotNull();
319+
assertThat(registration.getOptions().get(0).getLabel()).isEqualTo("label");
320+
});
321+
}
322+
323+
@Command
324+
private static class OptionWithLabel {
315325

326+
@Command
327+
void command1(@Option(longNames = "arg", label = "label") String arg) {
328+
}
316329
}
317330

318331
private <T> ApplicationContextRunner configCommon(Class<T> type, T bean) {

spring-shell-docs/src/main/asciidoc/using-shell-options-label.adoc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ what a default `help` command outputs. Within a command documentation
77
a type of an option is documented but this is not always super useful. Thus
88
you may want to give better descriptive word for an option.
99

10-
====
11-
[source, java, indent=0]
10+
NOTE: Label is not supported with `legacy annotation`.
11+
12+
[source,java,indent=0,role="primary"]
13+
.Programmatic
1214
----
13-
include::{snippets}/OptionSnippets.java[tag=option-registration-label]
15+
include::{snippets}/OptionSnippets.java[tag=option-label-programmatic]
1416
----
15-
====
1617

17-
Defining label is then shown in `help`.
18+
[source,java,indent=0,role="secondary"]
19+
.Annotation
20+
----
21+
include::{snippets}/OptionSnippets.java[tag=option-label-annotation]
22+
----
1823

24+
Defining label is then shown in `help`.
1925
====
2026
[source, bash]
2127
----

spring-shell-docs/src/test/java/org/springframework/shell/docs/OptionSnippets.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,13 @@ void defaultOption(
372372
}
373373
// end::option-default-annotation[]
374374

375+
// tag::option-label-annotation[]
376+
void labelOption(
377+
@Option(label = "MYLABEL") String arg
378+
) {
379+
}
380+
// end::option-label-annotation[]
381+
375382
}
376383

377384
static class Registration {
@@ -436,5 +443,15 @@ CommandRegistration defaultOption() {
436443
}
437444
// end::option-default-programmatic[]
438445

446+
// tag::option-label-programmatic[]
447+
CommandRegistration labelOption() {
448+
return CommandRegistration.builder()
449+
.withOption()
450+
.longNames("arg")
451+
.label("MYLABEL")
452+
.and()
453+
.build();
454+
}
455+
// end::option-label-programmatic[]
439456
}
440457
}

0 commit comments

Comments
 (0)