Class OptionParser


  • public class OptionParser
    extends java.lang.Object
    A replacement of getopt.

    Allows parsing command line options

    Since:
    14415
    • Constructor Detail

      • OptionParser

        public OptionParser​(java.lang.String program)
        Create a new option parser.
        Parameters:
        program - The program name.
    • Method Detail

      • addShortAlias

        public OptionParser addShortAlias​(java.lang.String optionName,
                                          java.lang.String shortName)
        Adds an alias for the long option --optionName to the short version -name
        Parameters:
        optionName - The long option
        shortName - The short version
        Returns:
        this OptionParser
      • addFlagParameter

        public OptionParser addFlagParameter​(java.lang.String optionName,
                                             java.lang.Runnable handler)
        Adds an option that may be used as a flag, e.g. --debug
        Parameters:
        optionName - The parameter name
        handler - The handler that is called when the flag is encountered.
        Returns:
        this OptionParser
      • checkOptionName

        private void checkOptionName​(java.lang.String optionName)
      • addArgumentParameter

        public OptionParser addArgumentParameter​(java.lang.String optionName,
                                                 OptionParser.OptionCount count,
                                                 java.util.function.Consumer<java.lang.String> handler)
        Add a parameter that expects a string attribute. E.g.: --config=/path/to/file
        Parameters:
        optionName - The name of the parameter.
        count - The number of times the parameter may occur.
        handler - A function that gets the current object and the parameter. It should throw an OptionParser.OptionParseException if the parameter cannot be handled / is invalid.
        Returns:
        this OptionParser
      • parseOptionsOrExit

        public java.util.List<java.lang.String> parseOptionsOrExit​(java.util.List<java.lang.String> arguments)
        Same as parseOptions(List), but exits if option parsing fails.
        Parameters:
        arguments - The options
        Returns:
        The remaining program arguments that are no options.
      • parseOptions

        public java.util.List<java.lang.String> parseOptions​(java.util.List<java.lang.String> arguments)
        Parses the options.

        It first checks if all required options are present, if all options are known and validates the option count.

        Then, all option handlers are called in the order in which the options are encountered.

        Parameters:
        arguments - Program arguments
        Returns:
        The remaining program arguments that are no options.
        Throws:
        OptionParser.OptionParseException - The error to display if option parsing failed.