Class ConsoleCommand
- Direct Known Subclasses:
ConfigCommand
,EchoCommand
,ExitCommand
,HelpCommand
,RestartCommand
Console commands allow you to interact with your microservice through the system console.
Console commands are associated with the microservice through the following:
- The
"Console/commands" configuration value.
This is a comma-delimited list of fully-qualified names of classes implementing this interface.
When associated this way, the implementation class must have a no-arg constructor. - Specifying commands via the
Microservice.Builder.consoleCommands(Class...)
method.
This allows you to override the default implementation above and augment or replace the list with your own implementation objects.
For example, the HelpCommand
is used to provide help on other commands.
Running class 'JettyMicroservice' using config file 'examples.cfg'. Server started on port 10000 List of available commands: exit -- Shut down service restart -- Restarts service help -- Commands help echo -- Echo command > help help NAME help -- Commands help SYNOPSIS help [command] DESCRIPTION When called without arguments, prints the descriptions of all available commands. Can also be called with one or more arguments to get detailed information on a command. EXAMPLES List all commands: > help List help on the help command: > help help >
The arguments are available as an Args
object which allows for easy accessed to parsed command lines.
Some simple examples of valid command lines:
See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract boolean
execute
(Scanner in, PrintWriter out, Args args) Executes a command.Returns localized details of the command.Returns localized examples of the command.getInfo()
Returns a one-line localized description of the command.abstract String
getName()
Returns the name of the command.Returns the usage synopsis of the command.
-
Constructor Details
-
ConsoleCommand
public ConsoleCommand()
-
-
Method Details
-
getName
Returns the name of the command.Example:
"help" for the help command.- Returns:
- The name of the command.
Must not benull or contain spaces.
-
getSynopsis
Returns the usage synopsis of the command.Example:
"help [command ...]" The default implementation just returns the name, which implies the command takes no additional arguments.
- Returns:
- The synopsis of the command.
-
getInfo
Returns a one-line localized description of the command.The locale should be the system locale.
- Returns:
- The localized description of the command.
Can benull if there is no information.
-
getDescription
Returns localized details of the command.The locale should be the system locale.
- Returns:
- The localized details of the command.
Can benull if there is no additional description.
-
getExamples
Returns localized examples of the command.The locale should be the system locale.
- Returns:
- The localized examples of the command.
Can benull if there is no examples.
-
execute
Executes a command.- Parameters:
in
- The console reader.out
- The console writer.args
- The command arguments. The first argument is always the command itself.- Returns:
true if the console read thread should exit.
Normally you want to returntrue if your action is causing the microservice to exit or restart.- Throws:
Exception
- Any thrown exception will simply be sent to STDERR.
-