Class CommandRunner
- java.lang.Object
- org.springframework.boot.cli.command.CommandRunner
public class CommandRunner extends Object implements Iterable<Command>
Main class used to runCommands.
Constructor Summary
Constructors Constructor Description CommandRunner(String name)Create a newCommandRunnerinstance.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCommand(Command command)Add the specified command.voidaddCommands(Iterable<Command> commands)Add the specified commands.protected voidafterRun(Command command)Subclass hook called after a command has run.protected voidbeforeRun(Command command)Subclass hook called before a command is run.protected booleanerrorMessage(String message)CommandfindCommand(String name)Find a command by name.protected List<Command>getCommands()StringgetName()Return the name of the runner or an empty string.booleanisOptionCommand(Command command)Returns if the specified command is an option command.Iterator<Command>iterator()protected voidprintStackTrace(Exception ex)protected ExitStatusrun(String... args)Parse the arguments and run a suitable command.intrunAndHandleErrors(String... args)Run the appropriate and handle and errors.voidsetHiddenCommands(Class<?>... commandClasses)Set the command classes which should be hidden (i.e.voidsetOptionCommands(Class<?>... commandClasses)Set the command classes which should be considered option commands.protected voidshowUsage()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Constructor Detail
CommandRunner
public CommandRunner(String name)
Create a newCommandRunnerinstance.- Parameters:
name- the name of the runner ornull
Method Detail
getName
public String getName()
Return the name of the runner or an empty string. Non-empty names will include a trailing space character so that they can be used as a prefix.- Returns:
- the name of the runner
addCommands
public void addCommands(Iterable<Command> commands)
Add the specified commands.- Parameters:
commands- the commands to add
addCommand
public void addCommand(Command command)
Add the specified command.- Parameters:
command- the command to add.
setOptionCommands
public void setOptionCommands(Class<?>... commandClasses)
Set the command classes which should be considered option commands. An option command is a special type of command that usually makes more sense to present as if it is an option. For example '--version'.- Parameters:
commandClasses- the classes of option commands.- See Also:
isOptionCommand(Command)
setHiddenCommands
public void setHiddenCommands(Class<?>... commandClasses)
Set the command classes which should be hidden (i.e. executed but not displayed in the available commands list).- Parameters:
commandClasses- the classes of hidden commands
isOptionCommand
public boolean isOptionCommand(Command command)
Returns if the specified command is an option command.- Parameters:
command- the command to test- Returns:
trueif the command is an option command- See Also:
setOptionCommands(Class...)
getCommands
protected final List<Command> getCommands()
findCommand
public Command findCommand(String name)
Find a command by name.- Parameters:
name- the name of the command- Returns:
- the command or
nullif not found
runAndHandleErrors
public int runAndHandleErrors(String... args)
Run the appropriate and handle and errors.- Parameters:
args- the input arguments- Returns:
- a return status code (non boot is used to indicate an error)
run
protected ExitStatus run(String... args) throws Exception
Parse the arguments and run a suitable command.- Parameters:
args- the arguments- Returns:
- the outcome of the command
- Throws:
Exception- if the command fails
beforeRun
protected void beforeRun(Command command)
Subclass hook called before a command is run.- Parameters:
command- the command about to run
afterRun
protected void afterRun(Command command)
Subclass hook called after a command has run.- Parameters:
command- the command that has run
errorMessage
protected boolean errorMessage(String message)
showUsage
protected void showUsage()
printStackTrace
protected void printStackTrace(Exception ex)