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).
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) class  CommandLine.Option  
    • Method Summary

      All Methods Static 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
      private CommandLine.Option getOption​(java.lang.String optionName, java.lang.reflect.Method m)  
      private java.util.Map<java.lang.String,​java.lang.reflect.Method> getOptions​(java.lang.Class<? extends Options> interf)
      Answer a list of the options specified in an options interface
      <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()  
      private java.lang.String getSynopsis​(java.lang.String cmd, java.util.Map<java.lang.String,​java.lang.reflect.Method> options, Arguments patterns)  
      private java.lang.String getTypeDescriptor​(java.lang.reflect.Type type)
      Show a type in a nice way
      private java.lang.String help​(java.lang.Object target, java.lang.String cmd, java.lang.Class<? extends Options> type)  
      private void help​(java.util.Formatter f, java.lang.Class<? extends Options> specification, java.lang.String title)  
      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.
      private boolean isMandatory​(java.lang.reflect.Method m)
      Answer if the method is marked mandatory
      private boolean isOption​(java.lang.reflect.Method m)  
      private static java.lang.String lastPart​(java.lang.String name)  
      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
    • Field Detail

      • LINELENGTH

        static int LINELENGTH
      • ASSIGNMENT

        static java.util.regex.Pattern ASSIGNMENT
      • result

        private java.lang.Object result
      • LAST_PART

        static java.util.regex.Pattern LAST_PART
    • 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)
      • help

        private java.lang.String help​(java.lang.Object target,
                                      java.lang.String cmd,
                                      java.lang.Class<? extends Options> type)
                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • 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
      • getOptions

        private java.util.Map<java.lang.String,​java.lang.reflect.Method> getOptions​(java.lang.Class<? extends Options> interf)
        Answer a list of the options specified in an options interface
      • 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

        private void help​(java.util.Formatter f,
                          java.lang.Class<? extends Options> specification,
                          java.lang.String title)
      • getOption

        private CommandLine.Option getOption​(java.lang.String optionName,
                                             java.lang.reflect.Method m)
      • getSynopsis

        private java.lang.String getSynopsis​(java.lang.String cmd,
                                             java.util.Map<java.lang.String,​java.lang.reflect.Method> options,
                                             Arguments patterns)
      • lastPart

        private static java.lang.String lastPart​(java.lang.String name)
      • 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
      • isMandatory

        private boolean isMandatory​(java.lang.reflect.Method m)
        Answer if the method is marked mandatory
      • isOption

        private boolean isOption​(java.lang.reflect.Method m)
        Parameters:
        m -
      • getTypeDescriptor

        private java.lang.String getTypeDescriptor​(java.lang.reflect.Type type)
        Show a type in a nice way
      • 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