Class CommandManager

    • Constructor Detail

      • CommandManager

        public CommandManager()
        Initialze the command.
    • Method Detail

      • getName

        public String getName()
        Name of the command.
        Specified by:
        getName in interface Command
        Returns:
        name
      • getDescription

        public String getDescription()
        Brief description of the command.
        Specified by:
        getDescription in interface Command
        Returns:
        description
      • getUsage

        public String getUsage()
        Command-line usage for the command.
        Specified by:
        getUsage in interface Command
        Returns:
        usage
      • getOptions

        public org.apache.commons.cli.Options getOptions()
        Command-line options for the command.
        Specified by:
        getOptions in interface Command
        Returns:
        options
      • addCommand

        public void addCommand​(String commandName,
                               Command command)
        Add a new command to this manager.
        Parameters:
        commandName - the of the command (one word)
        command - the Command object to register
      • getOptionArgs

        public List<String> getOptionArgs​(org.apache.commons.cli.Options options,
                                          List<String> arguments)
                                   throws org.apache.commons.cli.ParseException
        Given some Options and some arguments, collect all the options until the first non-option argument, then remove those used argument strings from the arguments list and return the used arguments as a new list. WARN: Mutates the `arguments` list.
        Parameters:
        options - the options to collect
        arguments - a list of remaining command-line arguments; used option strings are removed from this list
        Returns:
        the list of used argument strings
        Throws:
        org.apache.commons.cli.ParseException - if command line cannot be parsed
      • main

        public void main​(String[] args)
        Given command-line arguments, execute one or more commands.
        Specified by:
        main in interface Command
        Parameters:
        args - the command line arguments
      • execute

        public CommandState execute​(CommandState state,
                                    String[] args)
                             throws Exception
        Given an input state and command-line arguments, execute one or more commands.
        Specified by:
        execute in interface Command
        Parameters:
        state - an state to work with, or null
        args - the command-line arguments
        Returns:
        the result state of the last subcommand or null on bad input
        Throws:
        Exception - on any problems
      • executeCommand

        public CommandState executeCommand​(CommandState state,
                                           List<String> globalOptionArgs,
                                           List<String> arguments)
                                    throws Exception
        Given an input state, global option strings, and remaining command-line argument strings, use as many arguments as needed to execute a single command. The arguments used by the command are removed from the arguments list, which can then be used to execute further commands.
        Parameters:
        state - the state from the previous command, or null
        globalOptionArgs - a list of global option strings
        arguments - the list of remaining command-line arguments; any arguments that are used will be removed from this list
        Returns:
        the state that results from this command
        Throws:
        Exception - on any problems
      • printHelp

        public void printHelp()
        Print general help plus a list of available commands.
      • printHelpEntry

        public void printHelpEntry​(Map.Entry<String,​Command> entry)
        Print a help entry for a single command.
        Parameters:
        entry - an entry from the map of commands
      • printHelpEntry

        public void printHelpEntry​(String name,
                                   String description)
        Print a help entry for a single command.
        Parameters:
        name - the name of the command
        description - a brief description of the command