Class CommandManager
This class allows you to register commands, retrieve them, and execute them based on CLI input.
Setup
-
Create a new CommandManager instance during application startup.
It is recommended to define a
Map
of commands and pass it to the CommandManager constructor.For example:
However, you can also create a CommandManager without any commands and add them later.private static final Map<String, CommandInterface> CUSTOM_COMMANDS_MAP = Map.of( "--debug", new DebugCommand() ); private static CommandManager commandManager = new CommandManager(CUSTOM_COMMANDS_MAP);
For example:
private static CommandManager commandManager = new CommandManager(); commandManager.registerCommand("--debug", new DebugCommand());
-
Use the CommandManager to handle CLI arguments. You can process
arguments using the
ApplicationCore.checkCLIArgs()
method, or manually.Note: if you plan to implement this manually, I suggest taking a look at the
checkCLIArgs()
method's implementation.For example:
for(String arg : args) { commandManager.executeCommand(arg); }
-
Create new command classes for each command and add them to the
CommandManager. For example, in the code above, we added
the
DebugCommand
.Example implementation of the
DebugCommand
class:public class DebugCommand implements CommandInterface { @Override public void execute(CommandManager commandManager) { MainWorker.debug = true; System.out.println("Debug mode enabled."); } }
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new CommandManager with only the default-help
command.CommandManager
(Map<String, CommandInterface> commandMap) Creates a new CommandManager containing the default commands and any custom commands defined by the map.CommandManager
(Map<String, CommandInterface> commandMap, Map<String, String> aliasMap) Creates a new CommandManager containing the default commands and any custom commands defined by the map. -
Method Summary
Modifier and TypeMethodDescriptionvoid
executeCommand
(String commandString) Execute a command from the map.void
executeCommand
(String commandString, String[] args) Execute a command from the map with arguments.String[]
getAliases
(String commandString) Retrieves all the valid aliases for a given command.Retrieves the alias map.getCommand
(String commandString) Retrieves the matching command from the map.Retrieves the command map.Retrieves the command string associated with a given alias.Retrieves a list of existing keys in the Command Map.Retrieves a list of valid commands along with their descriptions and aliases.void
registerAlias
(String alias, String commandString) Add an alias for a command.void
registerAliases
(Map<String, String> aliasMap) Add a map of aliases to the alias map.void
registerCommand
(String commandString, CommandInterface commandToExecute) Add a command to the map.void
registerCommand
(String commandString, String alias, CommandInterface commandToExecute) Add a command to the map with an alias.void
registerCommands
(Map<String, CommandInterface> commandMap) Add a map of custom commands to the command map.void
registerCommandsWithAliases
(Map<String[], CommandInterface> commandMap) Add a map of custom commands to the command map with aliases.
-
Constructor Details
-
CommandManager
public CommandManager()Creates a new CommandManager with only the default-help
command.- See Also:
-
CommandManager
Creates a new CommandManager containing the default commands and any custom commands defined by the map.- Parameters:
commandMap
- map of commands- See Also:
-
CommandManager
Creates a new CommandManager containing the default commands and any custom commands defined by the map.- Parameters:
commandMap
- map of commandsaliasMap
- map of aliases- See Also:
-
-
Method Details
-
getCommand
Retrieves the matching command from the map.- Parameters:
commandString
- the key of the CommandInterface to retrieve- Returns:
- the CommandInterface related to the key, or
null
if no matching command is found
-
getCommandStringMatchingAlias
Retrieves the command string associated with a given alias.- Parameters:
alias
- the alias for which to retrieve the command string- Returns:
- the command string related to the alias, or
null
if no matching alias is found
-
getAliases
Retrieves all the valid aliases for a given command.- Parameters:
commandString
- the command for which to retrieve aliases- Returns:
- the aliases related to the command, or
null
if no matching command is found
-
getCommandMap
Retrieves the command map.- Returns:
- Map containing all existing arg keys and their command objects.
-
getAliasMap
Retrieves the alias map.- Returns:
- Map containing all existing aliases and their command keys.
-
getValidCommands
Retrieves a list of existing keys in the Command Map. Useful to know what commands are available.- Returns:
- A list of valid CLI args for use.
-
getValidCommandsWithInfo
Retrieves a list of valid commands along with their descriptions and aliases.Each entry in the returned list is formatted as:
commandKey (alias, alias...): commandDescription
.This method is useful for displaying all available commands, their aliases, and purposes in a user-friendly format, such as in a help menu or CLI output.
- Returns:
- a
List<String>
where each element represents a command and its description in the formatcommandKey: commandDescription
.Example:
// Assuming the CommandManager has the following commands: // --help (-h): displays a list of valid commands // --debug (-d): enables debug mode List<String> commandsWithDescriptions = commandManager.getValidCommandsWithInfo(); System.out.println(commandsWithDescriptions); // Output: // [--help (-h): displays a list of valid commands, --debug (-d): enables debug mode]
-
registerCommand
Add a command to the map. If the key already exists, the command will be replaced.Example:
registerCommand("--help", new HelpCommand());
- Parameters:
commandString
- key for the command (what to listen for)commandToExecute
- CommandInterface class- See Also:
-
registerCommand
Add a command to the map with an alias. If the key already exists, the command will be replaced.To add an alias for an existing command, use
registerAlias(String, String)
.Example:
registerCommand("--help", "-h", new HelpCommand());
- Parameters:
commandString
- key for the command (what to listen for)alias
- the alias for the commandcommandToExecute
- CommandInterface class- See Also:
-
registerAlias
Add an alias for a command. If the command does not exist, an error message is printed toSystem.err
. Commands can have multiple aliases.Example:
registerAlias("-h", "--help"); registerAlias("-d", "--debug");
- Parameters:
alias
- the alias to registercommandString
- the command to which the alias refers- See Also:
-
registerCommands
Add a map of custom commands to the command map.Example:
yourCommandMap.put("--help", new HelpCommand()); yourCommandMap.put("--debug", new DebugCommand()); commandManager.registerCommands(yourCommandMap);
- Parameters:
commandMap
- map of key-value pairs where the key is the CLI arg to listen for and the value is a new Command.- See Also:
-
registerCommandsWithAliases
Add a map of custom commands to the command map with aliases.Example:
yourCommandMap.put(new String[]{"--help", "-h"}, new HelpCommand()); yourCommandMap.put(new String[]{"--debug", "-d"}, new DebugCommand()); commandManager.registerCommandsWithAliases(yourCommandMap);
- Parameters:
commandMap
- map of key-value pairs where the key is the CLI arg to listen for and the value is a new Command.- See Also:
-
registerAliases
Add a map of aliases to the alias map.Example:
yourAliasMap.put("-h", "--help"); yourAliasMap.put("-d", "--debug"); commandManager.registerAliases(yourAliasMap);
- Parameters:
aliasMap
- map of key-value pairs where the key is the alias and the value is the command it refers to.
-
executeCommand
Execute a command from the map. If the command is not found, an error message is printed toSystem.err
.- Parameters:
commandString
- the key of the CommandInterface to execute- See Also:
-
executeCommand
Execute a command from the map with arguments. If the command is not found or the number of arguments is different from expected, an error message is printed toSystem.err
.- Parameters:
commandString
- the key of the CommandInterface to executeargs
- an array ofString
arguments passed to the command
-