diff --git a/spring-shell-docs/src/main/asciidoc/using-shell-options-definition.adoc b/spring-shell-docs/src/main/asciidoc/using-shell-options-definition.adoc index 538894c0..c39eb455 100644 --- a/spring-shell-docs/src/main/asciidoc/using-shell-options-definition.adoc +++ b/spring-shell-docs/src/main/asciidoc/using-shell-options-definition.adoc @@ -25,6 +25,16 @@ include::{snippets}/OptionSnippets.java[tag=option-with-annotation] ---- ==== +If option name is defined without prefix, either `-` or `--`, it is discovered +from _ShellMethod#prefix_. + +==== +[source, java, indent=0] +---- +include::{snippets}/OptionSnippets.java[tag=option-with-annotation-without-prefix] +---- +==== + Programmatic way with `CommandRegistration` is to use method adding a long name. ==== diff --git a/spring-shell-docs/src/test/java/org/springframework/shell/docs/OptionSnippets.java b/spring-shell-docs/src/test/java/org/springframework/shell/docs/OptionSnippets.java index e3142c47..2c472752 100644 --- a/spring-shell-docs/src/test/java/org/springframework/shell/docs/OptionSnippets.java +++ b/spring-shell-docs/src/test/java/org/springframework/shell/docs/OptionSnippets.java @@ -23,12 +23,20 @@ public class OptionSnippets { class Dump1 { // tag::option-with-annotation[] - public String example(@ShellOption(value = { "argx" }) String arg1) { + public String example(@ShellOption(value = { "--argx" }) String arg1) { return "Hello " + arg1; } // end::option-with-annotation[] } + class Dump7 { + // tag::option-with-annotation-without-prefix[] + public String example(@ShellOption(value = { "argx" }) String arg1) { + return "Hello " + arg1; + } + // end::option-with-annotation-without-prefix[] + } + class Dump2 { // tag::option-without-annotation[] public String example(String arg1) { diff --git a/spring-shell-standard/src/main/java/org/springframework/shell/standard/StandardMethodTargetRegistrar.java b/spring-shell-standard/src/main/java/org/springframework/shell/standard/StandardMethodTargetRegistrar.java index 9db814c2..580a5ac1 100644 --- a/spring-shell-standard/src/main/java/org/springframework/shell/standard/StandardMethodTargetRegistrar.java +++ b/spring-shell-standard/src/main/java/org/springframework/shell/standard/StandardMethodTargetRegistrar.java @@ -118,6 +118,14 @@ public class StandardMethodTargetRegistrar implements MethodTargetRegistrar, App else if (o.length() == stripped.length() + 1 && stripped.length() == 1) { shortNames.add(stripped.charAt(0)); } + else if (o.length() == stripped.length()) { + if ("--".equals(shellMapping.prefix())) { + longNames.add(stripped); + } + else if ("-".equals(shellMapping.prefix()) && stripped.length() == 1) { + shortNames.add(stripped.charAt(0)); + } + } }); } else { diff --git a/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardMethodTargetRegistrarTests.java b/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardMethodTargetRegistrarTests.java index 1c563336..bb01e02d 100644 --- a/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardMethodTargetRegistrarTests.java +++ b/spring-shell-standard/src/test/java/org/springframework/shell/standard/StandardMethodTargetRegistrarTests.java @@ -443,4 +443,44 @@ public class StandardMethodTargetRegistrarTests { public void foo3(@ShellOption boolean arg1) { } } + + @Test + public void testOptionWithoutHyphenRegisterFromDefaultPrefix() { + applicationContext = new AnnotationConfigApplicationContext(OptionWithoutHyphenRegisterFromDefaultPrefix.class); + registrar.setApplicationContext(applicationContext); + registrar.register(catalog); + + assertThat(catalog.getRegistrations().get("foo1")).isNotNull(); + assertThat(catalog.getRegistrations().get("foo1").getOptions()).hasSize(1); + assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).getLongNames()).hasSize(1); + assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).getShortNames()).hasSize(0); + } + + @ShellComponent + public static class OptionWithoutHyphenRegisterFromDefaultPrefix { + + @ShellMethod(value = "foo1") + public void foo1(@ShellOption("xxx") boolean arg1) { + } + } + + @Test + public void testOptionWithoutHyphenRegisterFromChangedPrefix() { + applicationContext = new AnnotationConfigApplicationContext(OptionWithoutHyphenRegisterFromChangedPrefix.class); + registrar.setApplicationContext(applicationContext); + registrar.register(catalog); + + assertThat(catalog.getRegistrations().get("foo1")).isNotNull(); + assertThat(catalog.getRegistrations().get("foo1").getOptions()).hasSize(1); + assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).getLongNames()).hasSize(0); + assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).getShortNames()).hasSize(1); + } + + @ShellComponent + public static class OptionWithoutHyphenRegisterFromChangedPrefix { + + @ShellMethod(value = "foo1", prefix = "-") + public void foo1(@ShellOption("x") boolean arg1) { + } + } }