Class CommandLineArguments


  • public class CommandLineArguments
    extends Object
    Simple reimplementation of a GNU getopt style command line argument parsing. The command line arguments must be given as follows:
    • Each command line option has a mandatory long form, prefixed with '--', and optionally an additional short form, prefixed with '-'.
    • Each command line option can optionally have a parameter, which can be optional or mandatory.
    • Each command line option parameter can optionally be described by a regular expression it has to match.
    • For options with optional parameters, the parameter must immediately follow the option, e.g.
      '-o param' or '--option=param'.
    • Non-option arguments are first assigned to command line options still missing a required parameter in the order of their appearance. E.g. if a, b, c are options with required parameters,
      -a -b -c one two three four
      and
      -abc one two three four
      and
      -a one -b two -c three four
      have the same meaning, where 'four' remains as a non-option argument, returned by the parse(String...) method.
    • The command line argument '-' ends the parsing of options so that all following command line arguments are considered to be non-option arguments. This allows non-option arguments to start with '-' characters without being confused with options.
    Command line options are defined together with a description. There are methods to generate a pretty printed overview of the defined options.
    • Constructor Detail

      • CommandLineArguments

        public CommandLineArguments​(BiConsumer<String,​String> callback)
        Constructs a command line argument parser.
        Parameters:
        callback - a BiConsumer to call for each command line argument, in the order of appearance of the options. The first string passes the long form of the option, which is never null. The second string passes the parameter given with the option, which may be null, if an optional parameter is not given or the argument is a flag option.
    • Method Detail

      • addFlagOption

        public CommandLineArguments addFlagOption​(String longOption,
                                                  String... description)
                                           throws OptionDefinitionException
        Adds a command line option which does not expect a parameter.
        Parameters:
        longOption - the long form of the option
        description - a multi line description for the option
        Returns:
        this CommandLineArguments
        Throws:
        OptionDefinitionException - if the same option is defined twice
      • addFlagOption

        public CommandLineArguments addFlagOption​(String longOption,
                                                  char shortOption,
                                                  String... description)
                                           throws OptionDefinitionException
        Adds a command line option which does not expect a parameter.
        Parameters:
        longOption - the long form of the option
        shortOption - the short form of the option
        description - a multi line description for the option
        Returns:
        this CommandLineArguments
        Throws:
        OptionDefinitionException - for duplicate option definitions
      • addParameterOption

        public CommandLineArguments addParameterOption​(String longOption,
                                                       boolean paramRequired,
                                                       String... description)
                                                throws OptionDefinitionException
        Adds a command line option which expects a parameter.
        Parameters:
        longOption - the long form of the option
        paramRequired - true, if the parameter is mandatory, false, if the parameter is optional
        description - a multi line description for the option
        Returns:
        this CommandLineArguments
        Throws:
        OptionDefinitionException - for duplicate option definitions
      • addParameterOption

        public CommandLineArguments addParameterOption​(String longOption,
                                                       char shortOption,
                                                       boolean paramRequired,
                                                       String... description)
                                                throws OptionDefinitionException
        Adds a command line option which expects a parameter.
        Parameters:
        longOption - the long form of the option
        shortOption - the short form of the option
        paramRequired - true, if the parameter is mandatory, false, if the parameter is optional
        description - a multi line description for the option
        Returns:
        this CommandLineArguments
        Throws:
        OptionDefinitionException - for duplicate option definitions
      • addParameterOption

        public CommandLineArguments addParameterOption​(String longOption,
                                                       String paramRegExpr,
                                                       boolean paramRequired,
                                                       String... description)
                                                throws OptionDefinitionException
        Adds a command line option which expects a parameter.
        Parameters:
        longOption - the long form of the option
        paramRegExpr - a regular expression the parameter must match
        paramRequired - true, if the parameter is mandatory, false, if the parameter is optional
        description - a multi line description for the option
        Returns:
        this CommandLineArguments
        Throws:
        OptionDefinitionException - for duplicate option definitions
      • addParameterOption

        public CommandLineArguments addParameterOption​(String longOption,
                                                       char shortOption,
                                                       String paramRegExpr,
                                                       boolean paramRequired,
                                                       String... description)
                                                throws OptionDefinitionException
        Adds a command line option which expects a parameter.
        Parameters:
        longOption - the long form of the option
        shortOption - the short form of the option
        paramRegExpr - a regular expression the parameter must match
        paramRequired - true, if the parameter is mandatory, false, if the parameter is optional
        description - a multi line description for the option
        Returns:
        this CommandLineArguments
        Throws:
        OptionDefinitionException - for duplicate option definitions
      • getFormattedDescription

        public List<String> getFormattedDescription​(boolean withHeader)
        Returns a formatted description of the defined options. The list is sorted by long options.
        Parameters:
        withHeader - true to output a header
        Returns:
        a formatted description of the defined option
      • printFormattedDescription

        public void printFormattedDescription​(PrintStream printStream,
                                              boolean withHeader)
        Prints a formatted description of the defined options.
        Parameters:
        printStream - the stream to print to
        withHeader - true to output a header