Class CommandRunner
- java.lang.Object
- org.springframework.boot.cli.command.CommandRunner
public class CommandRunner extends Object implements Iterable<Command>
Main class used to runCommand
s.
Constructor Summary
Constructors Constructor Description CommandRunner(String name)
Create a newCommandRunner
instance.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCommand(Command command)
Add the specified command.void
addCommands(Iterable<Command> commands)
Add the specified commands.protected void
afterRun(Command command)
Subclass hook called after a command has run.protected void
beforeRun(Command command)
Subclass hook called before a command is run.protected boolean
errorMessage(String message)
Command
findCommand(String name)
Find a command by name.protected List<Command>
getCommands()
String
getName()
Return the name of the runner or an empty string.boolean
isOptionCommand(Command command)
Returns if the specified command is an option command.Iterator<Command>
iterator()
protected void
printStackTrace(Exception ex)
protected ExitStatus
run(String... args)
Parse the arguments and run a suitable command.int
runAndHandleErrors(String... args)
Run the appropriate and handle and errors.void
setHiddenCommands(Class<?>... commandClasses)
Set the command classes which should be hidden (i.e.void
setOptionCommands(Class<?>... commandClasses)
Set the command classes which should be considered option commands.protected void
showUsage()
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 newCommandRunner
instance.- 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:
true
if 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
null
if 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)