Class CommandLine


  • public class CommandLine
    extends java.lang.Object
    Helps parsing command lines. This class takes target object, a primary command, and a list of arguments. It will then find the command in the target object. The method of this command must start with a "_" and take an parameter of Options type. Usually this is an interface that extends Options. The methods on this interface are options or flags (when they return boolean).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void assignOptionValue​(java.util.Map<java.lang.String,​java.lang.Object> options, java.lang.reflect.Method m, java.util.List<java.lang.String> args, boolean last)
      Assign an option, must handle flags, parameters, and parameters that can happen multiple times.
      java.lang.String execute​(java.lang.Object target, java.lang.String cmd, java.util.List<java.lang.String> input)
      Execute a command in a target object with a set of options and arguments and returns help text if something fails.
      void generateDocumentation​(java.lang.Object target, java.lang.Appendable out)  
      java.util.Map<java.lang.String,​java.lang.reflect.Method> getCommands​(java.lang.Object target)
      Parse a class and return a list of command names
      <T extends Options>
      T
      getOptions​(java.lang.Class<T> specification, java.util.List<java.lang.String> arguments)
      Parse the options in a command line and return an interface that provides the options from this command line.
      java.lang.Object getResult()  
      void help​(java.util.Formatter f, java.lang.Object target)
      Show all commands in a target
      void help​(java.util.Formatter f, java.lang.Object target, java.lang.String cmd)
      Show the full help for a given command
      void help​(java.util.Formatter f, java.lang.Object target, java.lang.String cmd, java.lang.Class<? extends Options> specification)
      Provide a help text.
      java.lang.String subCmd​(Options opts, java.lang.Object target)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CommandLine

        public CommandLine​(Reporter reporter)
    • Method Detail

      • execute

        public java.lang.String execute​(java.lang.Object target,
                                        java.lang.String cmd,
                                        java.util.List<java.lang.String> input)
                                 throws java.lang.Exception
        Execute a command in a target object with a set of options and arguments and returns help text if something fails. Errors are reported.
        Throws:
        java.lang.Exception
      • generateDocumentation

        public void generateDocumentation​(java.lang.Object target,
                                          java.lang.Appendable out)
      • getOptions

        public <T extends Options> T getOptions​(java.lang.Class<T> specification,
                                                java.util.List<java.lang.String> arguments)
                                         throws java.lang.Exception
        Parse the options in a command line and return an interface that provides the options from this command line. This will parse up to (and including) -- or an argument that does not start with -
        Throws:
        java.lang.Exception
      • assignOptionValue

        public void assignOptionValue​(java.util.Map<java.lang.String,​java.lang.Object> options,
                                      java.lang.reflect.Method m,
                                      java.util.List<java.lang.String> args,
                                      boolean last)
        Assign an option, must handle flags, parameters, and parameters that can happen multiple times.
        Parameters:
        options - The command line map
        args - the args input
        m - the selected method for this option
        last - if this is the last in a multi single character option
      • help

        public void help​(java.util.Formatter f,
                         java.lang.Object target,
                         java.lang.String cmd,
                         java.lang.Class<? extends Options> specification)
        Provide a help text.
      • help

        public void help​(java.util.Formatter f,
                         java.lang.Object target)
                  throws java.lang.Exception
        Show all commands in a target
        Throws:
        java.lang.Exception
      • help

        public void help​(java.util.Formatter f,
                         java.lang.Object target,
                         java.lang.String cmd)
        Show the full help for a given command
      • getCommands

        public java.util.Map<java.lang.String,​java.lang.reflect.Method> getCommands​(java.lang.Object target)
        Parse a class and return a list of command names
        Parameters:
        target -
        Returns:
        command names
      • getResult

        public java.lang.Object getResult()
      • subCmd

        public java.lang.String subCmd​(Options opts,
                                       java.lang.Object target)
                                throws java.lang.Exception
        Throws:
        java.lang.Exception