Skip to content

Commit efbc223

Browse files
committed
Refactor option basic docs
- Document better a relationship between annotation, legacy annotation and programmatic registration. - Relates #637
1 parent 84cb5a7 commit efbc223

6 files changed

+70
-30
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[using-shell-options-basics-annotation]]
2+
==== Annotation
3+
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
4+
5+
`Option` annotation can be used to define an option name if you
6+
don't want it to be same as argument name.
7+
8+
====
9+
[source, java, indent=0]
10+
----
11+
include::{snippets}/OptionSnippets.java[tag=option-with-option-annotation]
12+
----
13+
====
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
[[using-shell-options-definition]]
2-
=== Definition
1+
[[using-shell-options-basics-legacyannotation]]
2+
==== Legacy Annotation
33
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
44

5-
Options can be defined within a target method as annotations in a method arguments
6-
or with programmatically with `CommandRegistration`.
7-
85
Having a target method with argument is automatically registered with a matching
96
argument name.
107

@@ -15,16 +12,6 @@ include::{snippets}/OptionSnippets.java[tag=option-without-annotation]
1512
----
1613
====
1714

18-
`Option` annotation can be used to define an option name if you
19-
don't want it to be same as argument name.
20-
21-
====
22-
[source, java, indent=0]
23-
----
24-
include::{snippets}/OptionSnippets.java[tag=option-with-option-annotation]
25-
----
26-
====
27-
2815
`@ShellOption` annotation can be used to define an option name if you
2916
don't want it to be same as argument name.
3017

@@ -44,12 +31,3 @@ from _ShellMethod#prefix_.
4431
include::{snippets}/OptionSnippets.java[tag=option-with-annotation-without-prefix]
4532
----
4633
====
47-
48-
Programmatic way with `CommandRegistration` is to use method adding a long name.
49-
50-
====
51-
[source, java, indent=0]
52-
----
53-
include::{snippets}/OptionSnippets.java[tag=option-registration-longarg]
54-
----
55-
====
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[[using-shell-options-basics-registration]]
2+
==== Registration
3+
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
4+
5+
Programmatic way with `CommandRegistration` is to use `withOption` to define
6+
an option.
7+
8+
====
9+
[source, java, indent=0]
10+
----
11+
include::{snippets}/OptionSnippets.java[tag=option-registration-longarg]
12+
----
13+
====
14+
15+
`CommandRegistration` can be defined as a bean or manually registered
16+
with a `CommandCatalog`.
17+
18+
NOTE: Check below sections for other option types, i.e. short format.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[using-shell-options-basics]]
2+
=== Basics
3+
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
4+
5+
This section gives a generic idea how an option can be defined. Following
6+
sections, beyond basics, discuss more about how various option behaviour
7+
can be accomplished for a particular use case.
8+
9+
include::using-shell-options-basics-annotation.adoc[]
10+
11+
include::using-shell-options-basics-registration.adoc[]
12+
13+
include::using-shell-options-basics-legacyannotation.adoc[]

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
44

55
Command line arguments can be separated into options and positional parameters.
6-
Following sections describes features how options are defined and used.
6+
Following sections describes features how options are defined and used. We first
7+
go through some basics about using options and then go into details about
8+
various ways how options and arguments works.
79

8-
include::using-shell-options-definition.adoc[]
10+
Generally speaking an _option_ is something after a commands prefixed with
11+
either `-` or `--`. An _option_ can either have a value or not depending
12+
on its context.
13+
14+
Options can be defined with a target method using annotations with a method
15+
arguments or with programmatically using `CommandRegistration`.
16+
17+
NOTE: In below sections `@ShellOption` refer to a _legacy annotation model_
18+
and `@Option` refer to an _annotation model_.
19+
20+
include::using-shell-options-basics.adoc[]
921

1022
include::using-shell-options-short.adoc[]
1123

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.shell.command.CommandRegistration;
2222
import org.springframework.shell.command.CommandRegistration.OptionArity;
2323
import org.springframework.shell.command.CommandRegistration.OptionNameModifier;
24+
import org.springframework.shell.command.annotation.Command;
2425
import org.springframework.shell.command.annotation.Option;
2526
import org.springframework.shell.standard.ShellMethod;
2627
import org.springframework.shell.standard.ShellOption;
@@ -29,30 +30,34 @@ public class OptionSnippets {
2930

3031
class Dump1Legacy {
3132
// tag::option-with-annotation[]
32-
public String example(@ShellOption(value = { "--argx" }) String arg1) {
33+
@ShellMethod
34+
public String example(@ShellOption(value = { "--arg" }) String arg1) {
3335
return "Hello " + arg1;
3436
}
3537
// end::option-with-annotation[]
3638
}
3739

3840
class Dump1 {
3941
// tag::option-with-option-annotation[]
40-
public String example(@Option(longNames = "argx") String arg1) {
42+
@Command
43+
public String example(@Option(longNames = "arg") String arg1) {
4144
return "Hello " + arg1;
4245
}
4346
// end::option-with-option-annotation[]
4447
}
4548

4649
class Dump7 {
4750
// tag::option-with-annotation-without-prefix[]
48-
public String example(@ShellOption(value = { "argx" }) String arg1) {
51+
@ShellMethod
52+
public String example(@ShellOption(value = { "arg" }) String arg1) {
4953
return "Hello " + arg1;
5054
}
5155
// end::option-with-annotation-without-prefix[]
5256
}
5357

5458
class Dump2 {
5559
// tag::option-without-annotation[]
60+
@ShellMethod
5661
public String example(String arg1) {
5762
return "Hello " + arg1;
5863
}
@@ -99,10 +104,11 @@ public String example(
99104
// end::option-with-annotation-default[]
100105
}
101106

107+
@SuppressWarnings("unused")
102108
public void dump1() {
103109

104110
// tag::option-registration-longarg[]
105-
CommandRegistration.builder()
111+
CommandRegistration registration = CommandRegistration.builder()
106112
.withOption()
107113
.longNames("arg1")
108114
.and()

0 commit comments

Comments
 (0)