Class CommandLine.Interpreter

  • Enclosing class:
    CommandLine

    private class CommandLine.Interpreter
    extends java.lang.Object
    Helper class responsible for processing command line arguments.
    • Constructor Summary

      Constructors 
      Constructor Description
      Interpreter​(java.lang.Object command)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private int applyOption​(java.lang.reflect.Field field, java.lang.Class<?> annotation, CommandLine.Range arity, boolean valueAttachedToOption, java.util.Stack<java.lang.String> args, java.util.Set<java.lang.reflect.Field> initialized, java.lang.String argDescription)  
      private int applyValuesToArrayField​(java.lang.reflect.Field field, java.lang.Class<?> annotation, CommandLine.Range arity, java.util.Stack<java.lang.String> args, java.lang.Class<?> cls, java.lang.String argDescription)  
      private int applyValuesToCollectionField​(java.lang.reflect.Field field, java.lang.Class<?> annotation, CommandLine.Range arity, java.util.Stack<java.lang.String> args, java.lang.Class<?> cls, java.lang.String argDescription)  
      private int applyValuesToMapField​(java.lang.reflect.Field field, java.lang.Class<?> annotation, CommandLine.Range arity, java.util.Stack<java.lang.String> args, java.lang.Class<?> cls, java.lang.String argDescription)  
      private int applyValueToSingleValuedField​(java.lang.reflect.Field field, CommandLine.Range arity, java.util.Stack<java.lang.String> args, java.lang.Class<?> cls, java.util.Set<java.lang.reflect.Field> initialized, java.lang.String argDescription)  
      private void assertNoMissingParameters​(java.lang.reflect.Field field, int arity, java.util.Stack<java.lang.String> args)  
      private void checkMaxArityExceeded​(CommandLine.Range arity, int remainder, java.lang.reflect.Field field, java.lang.String[] values)  
      private java.util.List<java.lang.Object> consumeArguments​(java.lang.reflect.Field field, java.lang.Class<?> annotation, CommandLine.Range arity, java.util.Stack<java.lang.String> args, java.lang.Class<?> type, int originalSize, java.lang.String argDescription)  
      private void consumeMapArguments​(java.lang.reflect.Field field, CommandLine.Range arity, java.util.Stack<java.lang.String> args, java.lang.Class<?>[] classes, CommandLine.ITypeConverter<?> keyConverter, CommandLine.ITypeConverter<?> valueConverter, java.util.Map<java.lang.Object,​java.lang.Object> result, java.lang.String argDescription)  
      private int consumeOneArgument​(java.lang.reflect.Field field, CommandLine.Range arity, java.util.Stack<java.lang.String> args, java.lang.Class<?> type, java.util.List<java.lang.Object> result, int index, int originalSize, java.lang.String argDescription)  
      private void consumeOneMapArgument​(java.lang.reflect.Field field, CommandLine.Range arity, java.util.Stack<java.lang.String> args, java.lang.Class<?>[] classes, CommandLine.ITypeConverter<?> keyConverter, CommandLine.ITypeConverter<?> valueConverter, java.util.Map<java.lang.Object,​java.lang.Object> result, int index, java.lang.String argDescription)  
      private java.util.Collection<java.lang.Object> createCollection​(java.lang.Class<?> collectionClass)  
      private java.util.Map<java.lang.Object,​java.lang.Object> createMap​(java.lang.Class<?> mapClass)  
      private CommandLine.ITypeConverter<?> getTypeConverter​(java.lang.Class<?> type, java.lang.reflect.Field field)  
      private void handleUnmatchedArguments​(java.lang.String arg)  
      private void handleUnmatchedArguments​(java.util.Stack<java.lang.String> args)  
      private boolean is​(java.lang.reflect.Field f, java.lang.String description, boolean value)  
      private boolean isAnyHelpRequested()  
      private boolean isOption​(java.lang.String arg)
      Called when parsing varargs parameters for a multi-value option.
      private java.lang.String optionDescription​(java.lang.String prefix, java.lang.reflect.Field field, int index)  
      (package private) java.util.List<CommandLine> parse​(java.lang.String... args)
      Entry point into parsing command line arguments.
      private void parse​(java.util.List<CommandLine> parsedCommands, java.util.Stack<java.lang.String> argumentStack, java.lang.String[] originalArgs)  
      private void processArguments​(java.util.List<CommandLine> parsedCommands, java.util.Stack<java.lang.String> args, java.util.Collection<java.lang.reflect.Field> required, java.util.Set<java.lang.reflect.Field> initialized, java.lang.String[] originalArgs)  
      private void processClusteredShortOptions​(java.util.Collection<java.lang.reflect.Field> required, java.util.Set<java.lang.reflect.Field> initialized, java.lang.String arg, java.util.Stack<java.lang.String> args)  
      private void processPositionalParameter​(java.util.Collection<java.lang.reflect.Field> required, java.util.Set<java.lang.reflect.Field> initialized, java.util.Stack<java.lang.String> args)  
      private void processRemainderAsPositionalParameters​(java.util.Collection<java.lang.reflect.Field> required, java.util.Set<java.lang.reflect.Field> initialized, java.util.Stack<java.lang.String> args)  
      private void processStandaloneOption​(java.util.Collection<java.lang.reflect.Field> required, java.util.Set<java.lang.reflect.Field> initialized, java.lang.String arg, java.util.Stack<java.lang.String> args, boolean paramAttachedToKey)  
      private boolean resemblesOption​(java.lang.String arg)  
      private java.lang.String[] split​(java.lang.String value, java.lang.reflect.Field field)  
      private java.lang.String splitRegex​(java.lang.reflect.Field field)  
      private java.lang.String trim​(java.lang.String value)  
      private java.lang.Object tryConvert​(java.lang.reflect.Field field, int index, CommandLine.ITypeConverter<?> converter, java.lang.String value, java.lang.Class<?> type)  
      private java.lang.String unquote​(java.lang.String value)  
      private void updateHelpRequested​(java.lang.reflect.Field field)  
      • Methods inherited from class java.lang.Object

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

      • commands

        private final java.util.Map<java.lang.String,​CommandLine> commands
      • optionName2Field

        private final java.util.Map<java.lang.String,​java.lang.reflect.Field> optionName2Field
      • singleCharOption2Field

        private final java.util.Map<java.lang.Character,​java.lang.reflect.Field> singleCharOption2Field
      • requiredFields

        private final java.util.List<java.lang.reflect.Field> requiredFields
      • positionalParametersFields

        private final java.util.List<java.lang.reflect.Field> positionalParametersFields
      • command

        private final java.lang.Object command
      • isHelpRequested

        private boolean isHelpRequested
      • separator

        private java.lang.String separator
      • position

        private int position
    • Constructor Detail

      • Interpreter

        Interpreter​(java.lang.Object command)
    • Method Detail

      • parse

        java.util.List<CommandLine> parse​(java.lang.String... args)
        Entry point into parsing command line arguments.
        Parameters:
        args - the command line arguments
        Returns:
        a list with all commands and subcommands initialized by this method
        Throws:
        CommandLine.ParameterException - if the specified command line arguments are invalid
      • parse

        private void parse​(java.util.List<CommandLine> parsedCommands,
                           java.util.Stack<java.lang.String> argumentStack,
                           java.lang.String[] originalArgs)
      • processArguments

        private void processArguments​(java.util.List<CommandLine> parsedCommands,
                                      java.util.Stack<java.lang.String> args,
                                      java.util.Collection<java.lang.reflect.Field> required,
                                      java.util.Set<java.lang.reflect.Field> initialized,
                                      java.lang.String[] originalArgs)
                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • resemblesOption

        private boolean resemblesOption​(java.lang.String arg)
      • handleUnmatchedArguments

        private void handleUnmatchedArguments​(java.lang.String arg)
      • handleUnmatchedArguments

        private void handleUnmatchedArguments​(java.util.Stack<java.lang.String> args)
      • processRemainderAsPositionalParameters

        private void processRemainderAsPositionalParameters​(java.util.Collection<java.lang.reflect.Field> required,
                                                            java.util.Set<java.lang.reflect.Field> initialized,
                                                            java.util.Stack<java.lang.String> args)
                                                     throws java.lang.Exception
        Throws:
        java.lang.Exception
      • processPositionalParameter

        private void processPositionalParameter​(java.util.Collection<java.lang.reflect.Field> required,
                                                java.util.Set<java.lang.reflect.Field> initialized,
                                                java.util.Stack<java.lang.String> args)
                                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • processStandaloneOption

        private void processStandaloneOption​(java.util.Collection<java.lang.reflect.Field> required,
                                             java.util.Set<java.lang.reflect.Field> initialized,
                                             java.lang.String arg,
                                             java.util.Stack<java.lang.String> args,
                                             boolean paramAttachedToKey)
                                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • processClusteredShortOptions

        private void processClusteredShortOptions​(java.util.Collection<java.lang.reflect.Field> required,
                                                  java.util.Set<java.lang.reflect.Field> initialized,
                                                  java.lang.String arg,
                                                  java.util.Stack<java.lang.String> args)
                                           throws java.lang.Exception
        Throws:
        java.lang.Exception
      • applyOption

        private int applyOption​(java.lang.reflect.Field field,
                                java.lang.Class<?> annotation,
                                CommandLine.Range arity,
                                boolean valueAttachedToOption,
                                java.util.Stack<java.lang.String> args,
                                java.util.Set<java.lang.reflect.Field> initialized,
                                java.lang.String argDescription)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • applyValueToSingleValuedField

        private int applyValueToSingleValuedField​(java.lang.reflect.Field field,
                                                  CommandLine.Range arity,
                                                  java.util.Stack<java.lang.String> args,
                                                  java.lang.Class<?> cls,
                                                  java.util.Set<java.lang.reflect.Field> initialized,
                                                  java.lang.String argDescription)
                                           throws java.lang.Exception
        Throws:
        java.lang.Exception
      • applyValuesToMapField

        private int applyValuesToMapField​(java.lang.reflect.Field field,
                                          java.lang.Class<?> annotation,
                                          CommandLine.Range arity,
                                          java.util.Stack<java.lang.String> args,
                                          java.lang.Class<?> cls,
                                          java.lang.String argDescription)
                                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • consumeMapArguments

        private void consumeMapArguments​(java.lang.reflect.Field field,
                                         CommandLine.Range arity,
                                         java.util.Stack<java.lang.String> args,
                                         java.lang.Class<?>[] classes,
                                         CommandLine.ITypeConverter<?> keyConverter,
                                         CommandLine.ITypeConverter<?> valueConverter,
                                         java.util.Map<java.lang.Object,​java.lang.Object> result,
                                         java.lang.String argDescription)
                                  throws java.lang.Exception
        Throws:
        java.lang.Exception
      • consumeOneMapArgument

        private void consumeOneMapArgument​(java.lang.reflect.Field field,
                                           CommandLine.Range arity,
                                           java.util.Stack<java.lang.String> args,
                                           java.lang.Class<?>[] classes,
                                           CommandLine.ITypeConverter<?> keyConverter,
                                           CommandLine.ITypeConverter<?> valueConverter,
                                           java.util.Map<java.lang.Object,​java.lang.Object> result,
                                           int index,
                                           java.lang.String argDescription)
                                    throws java.lang.Exception
        Throws:
        java.lang.Exception
      • checkMaxArityExceeded

        private void checkMaxArityExceeded​(CommandLine.Range arity,
                                           int remainder,
                                           java.lang.reflect.Field field,
                                           java.lang.String[] values)
      • applyValuesToArrayField

        private int applyValuesToArrayField​(java.lang.reflect.Field field,
                                            java.lang.Class<?> annotation,
                                            CommandLine.Range arity,
                                            java.util.Stack<java.lang.String> args,
                                            java.lang.Class<?> cls,
                                            java.lang.String argDescription)
                                     throws java.lang.Exception
        Throws:
        java.lang.Exception
      • applyValuesToCollectionField

        private int applyValuesToCollectionField​(java.lang.reflect.Field field,
                                                 java.lang.Class<?> annotation,
                                                 CommandLine.Range arity,
                                                 java.util.Stack<java.lang.String> args,
                                                 java.lang.Class<?> cls,
                                                 java.lang.String argDescription)
                                          throws java.lang.Exception
        Throws:
        java.lang.Exception
      • consumeArguments

        private java.util.List<java.lang.Object> consumeArguments​(java.lang.reflect.Field field,
                                                                  java.lang.Class<?> annotation,
                                                                  CommandLine.Range arity,
                                                                  java.util.Stack<java.lang.String> args,
                                                                  java.lang.Class<?> type,
                                                                  int originalSize,
                                                                  java.lang.String argDescription)
                                                           throws java.lang.Exception
        Throws:
        java.lang.Exception
      • consumeOneArgument

        private int consumeOneArgument​(java.lang.reflect.Field field,
                                       CommandLine.Range arity,
                                       java.util.Stack<java.lang.String> args,
                                       java.lang.Class<?> type,
                                       java.util.List<java.lang.Object> result,
                                       int index,
                                       int originalSize,
                                       java.lang.String argDescription)
                                throws java.lang.Exception
        Throws:
        java.lang.Exception
      • splitRegex

        private java.lang.String splitRegex​(java.lang.reflect.Field field)
      • split

        private java.lang.String[] split​(java.lang.String value,
                                         java.lang.reflect.Field field)
      • isOption

        private boolean isOption​(java.lang.String arg)
        Called when parsing varargs parameters for a multi-value option. When an option is encountered, the remainder should not be interpreted as vararg elements.
        Parameters:
        arg - the string to determine whether it is an option or not
        Returns:
        true if it is an option, false otherwise
      • tryConvert

        private java.lang.Object tryConvert​(java.lang.reflect.Field field,
                                            int index,
                                            CommandLine.ITypeConverter<?> converter,
                                            java.lang.String value,
                                            java.lang.Class<?> type)
                                     throws java.lang.Exception
        Throws:
        java.lang.Exception
      • optionDescription

        private java.lang.String optionDescription​(java.lang.String prefix,
                                                   java.lang.reflect.Field field,
                                                   int index)
      • isAnyHelpRequested

        private boolean isAnyHelpRequested()
      • updateHelpRequested

        private void updateHelpRequested​(java.lang.reflect.Field field)
      • is

        private boolean is​(java.lang.reflect.Field f,
                           java.lang.String description,
                           boolean value)
      • createCollection

        private java.util.Collection<java.lang.Object> createCollection​(java.lang.Class<?> collectionClass)
                                                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • createMap

        private java.util.Map<java.lang.Object,​java.lang.Object> createMap​(java.lang.Class<?> mapClass)
                                                                          throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getTypeConverter

        private CommandLine.ITypeConverter<?> getTypeConverter​(java.lang.Class<?> type,
                                                               java.lang.reflect.Field field)
      • assertNoMissingParameters

        private void assertNoMissingParameters​(java.lang.reflect.Field field,
                                               int arity,
                                               java.util.Stack<java.lang.String> args)
      • trim

        private java.lang.String trim​(java.lang.String value)
      • unquote

        private java.lang.String unquote​(java.lang.String value)