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.

    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);
     }
     
  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

  • 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
    • getCommandStringMatchingAlias

      public String getCommandStringMatchingAlias(String alias)
      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

      public String[] getAliases(String commandString)
      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

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

      public Map<String,String> getAliasMap()
      Retrieves the alias map.
      Returns:
      Map containing all existing aliases and their command keys.
    • 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.
    • getValidCommandsWithInfo

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

      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:
    • registerCommand

      public void registerCommand(String commandString, String alias, CommandInterface commandToExecute)
      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 command
      commandToExecute - CommandInterface class
      See Also:
    • registerAlias

      public void registerAlias(String alias, String commandString)
      Add an alias for a command. If the command does not exist, an error message is printed to System.err. Commands can have multiple aliases.

      Example:

       registerAlias("-h", "--help");
       registerAlias("-d", "--debug");
       
      Parameters:
      alias - the alias to register
      commandString - the command to which the alias refers
      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:
    • registerCommandsWithAliases

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

      public void registerAliases(Map<String,String> aliasMap)
      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

      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:
    • executeCommand

      public void executeCommand(String commandString, String[] args)
      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 to System.err.
      Parameters:
      commandString - the key of the CommandInterface to execute
      args - an array of String arguments passed to the command