Skip to content

Commit 4521452

Browse files
committed
Register option without prefix
- While option should be defined i.e. using "--arg", discover prefix from ShellMethod#prefix and use that if just "arg" is defined. - Fixes #575
1 parent a900fa4 commit 4521452

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ include::{snippets}/OptionSnippets.java[tag=option-with-annotation]
2525
----
2626
====
2727

28+
If option name is defined without prefix, either `-` or `--`, it is discovered
29+
from _ShellMethod#prefix_.
30+
31+
====
32+
[source, java, indent=0]
33+
----
34+
include::{snippets}/OptionSnippets.java[tag=option-with-annotation-without-prefix]
35+
----
36+
====
37+
2838
Programmatic way with `CommandRegistration` is to use method adding a long name.
2939

3040
====

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ public class OptionSnippets {
2323

2424
class Dump1 {
2525
// tag::option-with-annotation[]
26-
public String example(@ShellOption(value = { "argx" }) String arg1) {
26+
public String example(@ShellOption(value = { "--argx" }) String arg1) {
2727
return "Hello " + arg1;
2828
}
2929
// end::option-with-annotation[]
3030
}
3131

32+
class Dump7 {
33+
// tag::option-with-annotation-without-prefix[]
34+
public String example(@ShellOption(value = { "argx" }) String arg1) {
35+
return "Hello " + arg1;
36+
}
37+
// end::option-with-annotation-without-prefix[]
38+
}
39+
3240
class Dump2 {
3341
// tag::option-without-annotation[]
3442
public String example(String arg1) {

spring-shell-standard/src/main/java/org/springframework/shell/standard/StandardMethodTargetRegistrar.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ public void register(CommandCatalog registry) {
119119
else if (o.length() == stripped.length() + 1 && stripped.length() == 1) {
120120
shortNames.add(stripped.charAt(0));
121121
}
122+
else if (o.length() == stripped.length()) {
123+
if ("--".equals(shellMapping.prefix())) {
124+
longNames.add(stripped);
125+
}
126+
else if ("-".equals(shellMapping.prefix()) && stripped.length() == 1) {
127+
shortNames.add(stripped.charAt(0));
128+
}
129+
}
122130
});
123131
}
124132
else {

spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardMethodTargetRegistrarTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,44 @@ public void foo2(@ShellOption(defaultValue = "true") boolean arg1) {
443443
public void foo3(@ShellOption boolean arg1) {
444444
}
445445
}
446+
447+
@Test
448+
public void testOptionWithoutHyphenRegisterFromDefaultPrefix() {
449+
applicationContext = new AnnotationConfigApplicationContext(OptionWithoutHyphenRegisterFromDefaultPrefix.class);
450+
registrar.setApplicationContext(applicationContext);
451+
registrar.register(catalog);
452+
453+
assertThat(catalog.getRegistrations().get("foo1")).isNotNull();
454+
assertThat(catalog.getRegistrations().get("foo1").getOptions()).hasSize(1);
455+
assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).getLongNames()).hasSize(1);
456+
assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).getShortNames()).hasSize(0);
457+
}
458+
459+
@ShellComponent
460+
public static class OptionWithoutHyphenRegisterFromDefaultPrefix {
461+
462+
@ShellMethod(value = "foo1")
463+
public void foo1(@ShellOption("xxx") boolean arg1) {
464+
}
465+
}
466+
467+
@Test
468+
public void testOptionWithoutHyphenRegisterFromChangedPrefix() {
469+
applicationContext = new AnnotationConfigApplicationContext(OptionWithoutHyphenRegisterFromChangedPrefix.class);
470+
registrar.setApplicationContext(applicationContext);
471+
registrar.register(catalog);
472+
473+
assertThat(catalog.getRegistrations().get("foo1")).isNotNull();
474+
assertThat(catalog.getRegistrations().get("foo1").getOptions()).hasSize(1);
475+
assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).getLongNames()).hasSize(0);
476+
assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).getShortNames()).hasSize(1);
477+
}
478+
479+
@ShellComponent
480+
public static class OptionWithoutHyphenRegisterFromChangedPrefix {
481+
482+
@ShellMethod(value = "foo1", prefix = "-")
483+
public void foo1(@ShellOption("x") boolean arg1) {
484+
}
485+
}
446486
}

0 commit comments

Comments
 (0)