Class CommandManager
java.lang.Object
com.everdro1d.libs.commands.CommandManager
CommandManager is a tool for managing CLI arguments and their associated commands.
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.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. -
Method Summary
Modifier and TypeMethodDescriptionvoid
executeCommand
(String commandString) Execute a command from the map.getCommand
(String commandString) Retrieves the matching command from the map.Retrieves the command map.Retrieves a list of existing keys in the Command Map.Retrieves a list of valid commands along with their descriptions.void
registerCommand
(String commandString, CommandInterface commandToExecute) Add a command to the map.void
registerCommands
(Map<String, CommandInterface> commandMap) Add a map of custom commands to the command map.
-
Constructor Details
-
CommandManager
public CommandManager()Creates a new CommandManager with only the default-help
command. -
CommandManager
Creates a new CommandManager containing the default commands and any custom commands defined by the map.- Parameters:
commandMap
- map of commands- 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
-
getCommandMap
Retrieves the command map.- Returns:
- Map containing all existing arg keys and their command objects.
-
getValidCommands
-
getValidCommandsWithDescription
Retrieves a list of valid commands along with their descriptions.Each entry in the returned list is formatted as:
commandKey: commandDescription
.This method is useful for displaying all available commands and their 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: displays a list of valid commands // -debug: enables debug mode List<String> commandsWithDescriptions = commandManager.getValidCommandsWithDescription(); System.out.println(commandsWithDescriptions); // Output: // [-help: displays a list of valid commands, -debug: 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:
-
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:
-
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:
-