Class CommandManager

java.lang.Object
com.everdro1d.libs.commands.CommandManager

public class CommandManager extends Object
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

  1. 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:

     private static final Map<String, CommandInterface> CUSTOM_COMMANDS_MAP = Map.of(
         "-debug", new DebugCommand()
     );
     private static CommandManager commandManager = new CommandManager(CUSTOM_COMMANDS_MAP);
     
    However, you can also create a CommandManager without any commands and add them later.

    For example:

     private static CommandManager commandManager = new CommandManager();
     commandManager.registerCommand("-debug", new DebugCommand());
     
  2. 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);
     }
     
  3. 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 Details

    • CommandManager

      public CommandManager()
      Creates a new CommandManager with only the default -help command.
    • CommandManager

      public CommandManager(Map<String, CommandInterface> commandMap)
      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

      public CommandInterface getCommand(String commandString)
      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

      public Map<String, CommandInterface> getCommandMap()
      Retrieves the command map.
      Returns:
      Map containing all existing arg keys and their command objects.
    • getValidCommands

      public Set<String> 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.
    • getValidCommandsWithDescription

      public List<String> 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 format commandKey: 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

      public void registerCommand(String commandString, CommandInterface commandToExecute)
      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

      public void registerCommands(Map<String, CommandInterface> commandMap)
      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

      public void executeCommand(String commandString)
      Execute a command from the map. If the command is not found, an error message is printed to System.err.
      Parameters:
      commandString - the key of the CommandInterface to execute
      See Also: