Add support for global help options
- Essentially this commit registeres on default `--help` and `-h` options to every command and execution short circuits in presense of help options to help command. - Add Supplier<CommandRegistration.Builder> as a bean which can be autowired registration beans. - Make this common bean customisable via CommandRegistrationCustomizer. - Change StandardMethodTargetRegistrar to use supplier so that annotated commands gets common customizations. - Change sample commands to use supplier. - Add new group, spring.shell.help to config props. - Docs changes - Fixes #582 - Fixes #585
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.command.CommandRegistration.OptionArity;
|
||||
@@ -38,8 +40,8 @@ public class ArityCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testBooleanArity1DefaultTrueRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testBooleanArity1DefaultTrueRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "boolean-arity1-default-true")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
@@ -37,8 +39,8 @@ public class DefaultValueCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testDefaultValueRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testDefaultValueRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "default-value")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
@@ -62,8 +64,8 @@ public class DefaultValueCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testDefaultValueBoolean1Registration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testDefaultValueBoolean1Registration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "default-value-boolean1")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
@@ -88,8 +90,8 @@ public class DefaultValueCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testDefaultValueBoolean2Registration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testDefaultValueBoolean2Registration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "default-value-boolean2")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
@@ -114,8 +116,8 @@ public class DefaultValueCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testDefaultValueBoolean3Registration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testDefaultValueBoolean3Registration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "default-value-boolean3")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.boot.ExitCodeGenerator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
@@ -31,8 +33,8 @@ import org.springframework.shell.standard.ShellComponent;
|
||||
public class ErrorHandlingCommands extends BaseE2ECommands {
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testErrorHandlingRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testErrorHandlingRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "error-handling")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
@@ -28,8 +30,8 @@ import org.springframework.shell.standard.ShellComponent;
|
||||
public class ExitCodeCommands extends BaseE2ECommands {
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testExitCodeRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testExitCodeRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "exit-code")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
import org.springframework.shell.standard.ShellMethod;
|
||||
import org.springframework.shell.standard.ShellOption;
|
||||
|
||||
@ShellComponent
|
||||
public class HelpOptionCommands extends BaseE2ECommands {
|
||||
|
||||
@Autowired
|
||||
Supplier<CommandRegistration.Builder> builder;
|
||||
|
||||
@ShellMethod(key = LEGACY_ANNO + "help-option-default", group = GROUP)
|
||||
public String testHelpOptionDefault(
|
||||
@ShellOption(defaultValue = "hi") String arg1
|
||||
) {
|
||||
return "Hello " + arg1;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testHelpOptionDefaultRegistration() {
|
||||
return builder.get()
|
||||
.command(REG, "help-option-default")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.defaultValue("hi")
|
||||
.and()
|
||||
.withTarget()
|
||||
.function(ctx -> {
|
||||
String arg1 = ctx.getOptionValue("arg1");
|
||||
return "Hello " + arg1;
|
||||
})
|
||||
.and()
|
||||
.build();
|
||||
}
|
||||
|
||||
// @ShellMethod(key = LEGACY_ANNO + "help-option-exists", group = GROUP)
|
||||
// public String testHelpOptionExists(
|
||||
// @ShellOption(defaultValue = "hi") String help
|
||||
// ) {
|
||||
// return "Hello " + help;
|
||||
// }
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testHelpOptionExistsRegistration() {
|
||||
return builder.get()
|
||||
.command(REG, "help-option-exists")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
.longNames("help")
|
||||
.defaultValue("hi")
|
||||
.and()
|
||||
.withHelpOptions()
|
||||
.longNames("myhelp")
|
||||
.and()
|
||||
.withTarget()
|
||||
.function(ctx -> {
|
||||
String arg1 = ctx.getOptionValue("help");
|
||||
return "Hello " + arg1;
|
||||
})
|
||||
.and()
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
@@ -23,8 +25,8 @@ import org.springframework.shell.standard.ShellComponent;
|
||||
public class HiddenCommands extends BaseE2ECommands {
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testHidden1Registration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testHidden1Registration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "hidden-1")
|
||||
.group(GROUP)
|
||||
.hidden()
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -40,10 +41,10 @@ public class InteractiveCompletionCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
CommandRegistration testInteractiveCompletion1Registration() {
|
||||
CommandRegistration testInteractiveCompletion1Registration(Supplier<CommandRegistration.Builder> builder) {
|
||||
Test1ValuesProvider test1ValuesProvider = new Test1ValuesProvider();
|
||||
Test2ValuesProvider test2ValuesProvider = new Test2ValuesProvider();
|
||||
return CommandRegistration.builder()
|
||||
return builder.get()
|
||||
.command(REG, "interactive-completion-1")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
@@ -32,8 +33,8 @@ import org.springframework.shell.standard.ShellOption;
|
||||
public class OptionTypeCommands extends BaseE2ECommands {
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testOptionTypeRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testOptionTypeRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "option-type")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
@@ -76,8 +77,8 @@ public class OptionTypeCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration optionTypeStringRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration optionTypeStringRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "option-type-string")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
@@ -112,8 +113,8 @@ public class OptionTypeCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration optionTypeBooleanRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration optionTypeBooleanRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "option-type-boolean")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
@@ -172,8 +173,8 @@ public class OptionTypeCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration optionTypeIntegerRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration optionTypeIntegerRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "option-type-integer")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
@@ -212,8 +213,8 @@ public class OptionTypeCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration optionTypeEnumRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration optionTypeEnumRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "option-type-enum")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
@@ -37,8 +39,8 @@ public class OptionalValueCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testOptionalValueRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testOptionalValueRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "optional-value")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
@@ -37,8 +39,8 @@ public class RequiredValueCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testRequiredValueRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testRequiredValueRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "required-value")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.shell.samples.e2e;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -40,8 +42,8 @@ public class ValidatedValueCommands extends BaseE2ECommands {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration testValidatedValueRegistration() {
|
||||
return CommandRegistration.builder()
|
||||
public CommandRegistration testValidatedValueRegistration(Supplier<CommandRegistration.Builder> builder) {
|
||||
return builder.get()
|
||||
.command(REG, "validated-value")
|
||||
.group(GROUP)
|
||||
.withOption()
|
||||
|
||||
Reference in New Issue
Block a user