Class DefaultParser

  • All Implemented Interfaces:
    CommandLineParser

    public class DefaultParser
    extends java.lang.Object
    implements CommandLineParser
    Default parser.
    Since:
    1.3
    Version:
    $Id: DefaultParser.java 1783175 2017-02-16 07:52:05Z britter $
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected CommandLine cmd
      The command-line instance.
      protected Option currentOption
      The last option parsed.
      protected java.lang.String currentToken
      The token currently processed.
      protected java.util.List expectedOpts
      The required options and groups expected to be found when parsing the command line.
      protected Options options
      The current options.
      protected boolean skipParsing
      Flag indicating if tokens should no longer be analyzed and simply added as arguments of the command line.
      protected boolean stopAtNonOption
      Flag indicating how unrecognized tokens are handled.
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultParser()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void checkRequiredArgs()
      Throw a MissingArgumentException if the current option didn't receive the number of arguments expected.
      private void checkRequiredOptions()
      Throws a MissingOptionException if all of the required options are not present.
      private java.lang.String getLongPrefix​(java.lang.String token)
      Search for a prefix that is the long name of an option (-Xmx512m)
      protected void handleConcatenatedOptions​(java.lang.String token)
      Breaks token into its constituent parts using the following algorithm.
      private void handleLongOption​(java.lang.String token)
      Handles the following tokens: --L --L=V --L V --l
      private void handleLongOptionWithEqual​(java.lang.String token)
      Handles the following tokens: --L=V -L=V --l=V -l=V
      private void handleLongOptionWithoutEqual​(java.lang.String token)
      Handles the following tokens: --L -L --l -l
      private void handleOption​(Option option)  
      private void handleProperties​(java.util.Properties properties)
      Sets the values of Options using the values in properties.
      private void handleShortAndLongOption​(java.lang.String token)
      Handles the following tokens: -S -SV -S V -S=V -S1S2 -S1S2 V -SV1=V2 -L -LV -L V -L=V -l
      private void handleToken​(java.lang.String token)
      Handle any command line token.
      private void handleUnknownToken​(java.lang.String token)
      Handles an unknown token.
      private boolean isArgument​(java.lang.String token)
      Returns true is the token is a valid argument.
      private boolean isJavaProperty​(java.lang.String token)
      Check if the specified token is a Java-like property (-Dkey=value).
      private boolean isLongOption​(java.lang.String token)
      Tells if the token looks like a long option.
      private boolean isNegativeNumber​(java.lang.String token)
      Check if the token is a negative number.
      private boolean isOption​(java.lang.String token)
      Tells if the token looks like an option.
      private boolean isShortOption​(java.lang.String token)
      Tells if the token looks like a short option.
      CommandLine parse​(Options options, java.lang.String[] arguments)
      Parse the arguments according to the specified options.
      CommandLine parse​(Options options, java.lang.String[] arguments, boolean stopAtNonOption)
      Parse the arguments according to the specified options.
      CommandLine parse​(Options options, java.lang.String[] arguments, java.util.Properties properties)
      Parse the arguments according to the specified options and properties.
      CommandLine parse​(Options options, java.lang.String[] arguments, java.util.Properties properties, boolean stopAtNonOption)
      Parse the arguments according to the specified options and properties.
      private void updateRequiredOptions​(Option option)
      Removes the option or its group from the list of expected elements.
      • Methods inherited from class java.lang.Object

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

      • cmd

        protected CommandLine cmd
        The command-line instance.
      • options

        protected Options options
        The current options.
      • stopAtNonOption

        protected boolean stopAtNonOption
        Flag indicating how unrecognized tokens are handled. true to stop the parsing and add the remaining tokens to the args list. false to throw an exception.
      • currentToken

        protected java.lang.String currentToken
        The token currently processed.
      • currentOption

        protected Option currentOption
        The last option parsed.
      • skipParsing

        protected boolean skipParsing
        Flag indicating if tokens should no longer be analyzed and simply added as arguments of the command line.
      • expectedOpts

        protected java.util.List expectedOpts
        The required options and groups expected to be found when parsing the command line.
    • Constructor Detail

      • DefaultParser

        public DefaultParser()
    • Method Detail

      • parse

        public CommandLine parse​(Options options,
                                 java.lang.String[] arguments)
                          throws ParseException
        Description copied from interface: CommandLineParser
        Parse the arguments according to the specified options.
        Specified by:
        parse in interface CommandLineParser
        Parameters:
        options - the specified Options
        arguments - the command line arguments
        Returns:
        the list of atomic option and value tokens
        Throws:
        ParseException - if there are any problems encountered while parsing the command line tokens.
      • parse

        public CommandLine parse​(Options options,
                                 java.lang.String[] arguments,
                                 java.util.Properties properties)
                          throws ParseException
        Parse the arguments according to the specified options and properties.
        Parameters:
        options - the specified Options
        arguments - the command line arguments
        properties - command line option name-value pairs
        Returns:
        the list of atomic option and value tokens
        Throws:
        ParseException - if there are any problems encountered while parsing the command line tokens.
      • parse

        public CommandLine parse​(Options options,
                                 java.lang.String[] arguments,
                                 boolean stopAtNonOption)
                          throws ParseException
        Description copied from interface: CommandLineParser
        Parse the arguments according to the specified options.
        Specified by:
        parse in interface CommandLineParser
        Parameters:
        options - the specified Options
        arguments - the command line arguments
        stopAtNonOption - if true an unrecognized argument stops the parsing and the remaining arguments are added to the CommandLines args list. If false an unrecognized argument triggers a ParseException.
        Returns:
        the list of atomic option and value tokens
        Throws:
        ParseException - if there are any problems encountered while parsing the command line tokens.
      • parse

        public CommandLine parse​(Options options,
                                 java.lang.String[] arguments,
                                 java.util.Properties properties,
                                 boolean stopAtNonOption)
                          throws ParseException
        Parse the arguments according to the specified options and properties.
        Parameters:
        options - the specified Options
        arguments - the command line arguments
        properties - command line option name-value pairs
        stopAtNonOption - if true an unrecognized argument stops the parsing and the remaining arguments are added to the CommandLines args list. If false an unrecognized argument triggers a ParseException.
        Returns:
        the list of atomic option and value tokens
        Throws:
        ParseException - if there are any problems encountered while parsing the command line tokens.
      • handleProperties

        private void handleProperties​(java.util.Properties properties)
                               throws ParseException
        Sets the values of Options using the values in properties.
        Parameters:
        properties - The value properties to be processed.
        Throws:
        ParseException
      • handleToken

        private void handleToken​(java.lang.String token)
                          throws ParseException
        Handle any command line token.
        Parameters:
        token - the command line token to handle
        Throws:
        ParseException
      • isArgument

        private boolean isArgument​(java.lang.String token)
        Returns true is the token is a valid argument.
        Parameters:
        token -
      • isNegativeNumber

        private boolean isNegativeNumber​(java.lang.String token)
        Check if the token is a negative number.
        Parameters:
        token -
      • isOption

        private boolean isOption​(java.lang.String token)
        Tells if the token looks like an option.
        Parameters:
        token -
      • isShortOption

        private boolean isShortOption​(java.lang.String token)
        Tells if the token looks like a short option.
        Parameters:
        token -
      • isLongOption

        private boolean isLongOption​(java.lang.String token)
        Tells if the token looks like a long option.
        Parameters:
        token -
      • handleUnknownToken

        private void handleUnknownToken​(java.lang.String token)
                                 throws ParseException
        Handles an unknown token. If the token starts with a dash an UnrecognizedOptionException is thrown. Otherwise the token is added to the arguments of the command line. If the stopAtNonOption flag is set, this stops the parsing and the remaining tokens are added as-is in the arguments of the command line.
        Parameters:
        token - the command line token to handle
        Throws:
        ParseException
      • handleLongOption

        private void handleLongOption​(java.lang.String token)
                               throws ParseException
        Handles the following tokens: --L --L=V --L V --l
        Parameters:
        token - the command line token to handle
        Throws:
        ParseException
      • handleLongOptionWithoutEqual

        private void handleLongOptionWithoutEqual​(java.lang.String token)
                                           throws ParseException
        Handles the following tokens: --L -L --l -l
        Parameters:
        token - the command line token to handle
        Throws:
        ParseException
      • handleLongOptionWithEqual

        private void handleLongOptionWithEqual​(java.lang.String token)
                                        throws ParseException
        Handles the following tokens: --L=V -L=V --l=V -l=V
        Parameters:
        token - the command line token to handle
        Throws:
        ParseException
      • handleShortAndLongOption

        private void handleShortAndLongOption​(java.lang.String token)
                                       throws ParseException
        Handles the following tokens: -S -SV -S V -S=V -S1S2 -S1S2 V -SV1=V2 -L -LV -L V -L=V -l
        Parameters:
        token - the command line token to handle
        Throws:
        ParseException
      • getLongPrefix

        private java.lang.String getLongPrefix​(java.lang.String token)
        Search for a prefix that is the long name of an option (-Xmx512m)
        Parameters:
        token -
      • isJavaProperty

        private boolean isJavaProperty​(java.lang.String token)
        Check if the specified token is a Java-like property (-Dkey=value).
      • handleConcatenatedOptions

        protected void handleConcatenatedOptions​(java.lang.String token)
                                          throws ParseException
        Breaks token into its constituent parts using the following algorithm.
        • ignore the first character ("-")
        • for each remaining character check if an Option exists with that id.
        • if an Option does exist then add that character prepended with "-" to the list of processed tokens.
        • if the Option can have an argument value and there are remaining characters in the token then add the remaining characters as a token to the list of processed tokens.
        • if an Option does NOT exist AND stopAtNonOption IS set then add the special token "--" followed by the remaining characters and also the remaining tokens directly to the processed tokens list.
        • if an Option does NOT exist AND stopAtNonOption IS NOT set then add that character prepended with "-".
        Parameters:
        token - The current token to be burst at the first non-Option encountered.
        Throws:
        ParseException - if there are any problems encountered while parsing the command line token.