diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java index 9afce6a68e..13ee7da70b 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java @@ -44,6 +44,7 @@ public class SpringCli { runner.addCommand(new ShellCommand()); runner.addCommand(new HintCommand(runner)); runner.setOptionCommands(HelpCommand.class, VersionCommand.class); + runner.setHiddenCommands(HintCommand.class); int exitCode = runner.runAndHandleErrors(args); if (exitCode != 0) { diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/CommandRunner.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/CommandRunner.java index 2863f33baa..2afdabdbb4 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/CommandRunner.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/CommandRunner.java @@ -46,6 +46,8 @@ public class CommandRunner implements Iterable { private Class[] optionCommandClasses = {}; + private Class[] hiddenCommandClasses = {}; + /** * Create a new {@link CommandRunner} instance. * @param name the name of the runner or {@code null} @@ -94,6 +96,16 @@ public class CommandRunner implements Iterable { this.optionCommandClasses = commandClasses; } + /** + * Set the command classes which should be hidden (i.e. executed but not displayed in + * the available commands list). + * @param commandClasses the classes of hidden commands + */ + public void setHiddenCommands(Class... commandClasses) { + Assert.notNull(commandClasses, "CommandClasses must not be null"); + this.hiddenCommandClasses = commandClasses; + } + /** * Returns if the specified command is an option command. * @param command the command to test @@ -101,8 +113,16 @@ public class CommandRunner implements Iterable { * @see #setOptionCommands(Class...) */ public boolean isOptionCommand(Command command) { - for (Class optionCommandClass : this.optionCommandClasses) { - if (optionCommandClass.isInstance(command)) { + return isCommandInstanceOf(command, this.optionCommandClasses); + } + + private boolean isHiddenCommand(Command command) { + return isCommandInstanceOf(command, this.hiddenCommandClasses); + } + + private boolean isCommandInstanceOf(Command command, Class[] commandClasses) { + for (Class commandClass : commandClasses) { + if (commandClass.isInstance(command)) { return true; } } @@ -249,7 +269,7 @@ public class CommandRunner implements Iterable { Log.info(""); Log.info("Available commands are:"); for (Command command : this.commands) { - if (!isOptionCommand(command)) { + if (!isOptionCommand(command) && !isHiddenCommand(command)) { String usageHelp = command.getUsageHelp(); String description = command.getDescription(); Log.info(String.format("\n %1$s %2$-15s\n %3$s", command.getName(),