Class CommandLineParser


  • public final class CommandLineParser
    extends Object

    A command line parser that can handle non-specified arguments. Arguments can be specified by manually adding NamedOptions via the addOption(NamedOption) method or by setting the parser's owner object via the constructor which will look for fields annotated with Option. All parsers will have help (-h or --help), config (--config), and explain config (--config-explain) options added.

    The parser accepts long (e.g. --longOption) and short (e.g. -s) arguments. Multiple short (e.g. single character) arguments can be specified at one time (e.g. -xzf would set the x, z, and f options to true). Short arguments may have values (e.g. -f FILENAME).

    Values for options will be specified on the corresponding NamedOption instance. The value can be retrieved either directly from the NamedOption or by using the get(String) method. Argument names need not specify the "--" or "-" prefix.

    Author:
    David B. Bracewell
    • Constructor Detail

      • CommandLineParser

        public CommandLineParser()
        Instantiates a new Command line parser.
      • CommandLineParser

        public CommandLineParser​(Object owner,
                                 String applicationDescription)
        Instantiates a new Command line parser.
        Parameters:
        owner - the owner
        applicationDescription - the application description
    • Method Detail

      • get

        public <T> T get​(String optionName)
        Gets the value for the specified option
        Type Parameters:
        T - the type of the value for the option
        Parameters:
        optionName - the name of the option whose value we want to retrieve
        Returns:
        the value of the option or null if not set
      • getOptions

        public Set<NamedOption> getOptions()
        Gets the specified options.
        Returns:
        the specified options
      • getSetEntries

        public Set<Map.Entry<String,​String>> getSetEntries()
        Gets options and values for everything passed in to the command line including unamed options.
        Returns:
        An Map.Entry of options and values for everything passed in to the command line including unamed options.
      • isSet

        public boolean isSet​(String optionName)
        Determines if an option was set or not.
        Parameters:
        optionName - the option name
        Returns:
        True if it was set (boolean options must be true), False otherwise
      • isSet

        public boolean isSet​(NamedOption option)
        Determines if an option was set or not.
        Parameters:
        option - the option to check
        Returns:
        True if it was set (boolean options must be true), False otherwise
      • parse

        public String[] parse​(String[] args)
        Parses an array of arguments
        Parameters:
        args - The command line arguments.
        Returns:
        the non-config/option parameters
      • showHelp

        public void showHelp()
        Prints help to standard error showing the application description, if set, and the list of valid command line arguments with those required arguments marked with an asterisk.