Class CLAParser


  • public final class CLAParser
    extends Object
    Utility class to provide support for command-line options parsing.

    Supports both POSIX-style short-form (e.g. -v) and GNU-style long-form (e.g. --verbose) options. The short-form is mandatory, and the long-form optional.

    Processing of options can be explicitly terminated by the argument "--", to allow (non-option) arguments to be specified.

    All options must have at least a short name, and are recommended to also have a long name. Options that require a value have the value specified immediately after the option name on the command-line.

    For example, an option to display a help message might be specified with the short name ? and the long name help (shown as [-?,--help] in the long usage message). An option for a size value might be specified as [-s,--size] and require a value parameter, which may be specified in any of these forms:

    • -s 123
    • -s=123
    • -s:123
    • /s 123
    • /s=123
    • /s:123
    • --size 123
    • --size=123
    • --size:123

    There is also support for automatic usage message based on the configured options. For example, an instance which has three options configured { w, h, ? } with long-names { width, height, help } and only w being mandatory, might have a short usage message as follows:

         java AppName -w <integer> [-h <integer>] [-?]
     
    and a long usage message as follows:
         java AppName <options>
         Options:
                 -w,--width <integer>    (mandatory)
                         Width of image.
    
                 [-h,--height <integer>]
                         Height of image.
    
                 [-?,--help]
                         Display help information.
     

    Here's a summary of JCLAP features:

    • Option short names are mandatory, and long names optional (as per POSIX standard, but retaining GNU enhancements).
    • Supports enumerated option types (String, Integer).
    • Supports filtered string option types.
    • Supports file/folder option types (using filtered-string feature).
    • Support for automatic usage printing (both long & short styles).
    • Supports custom application launch text for automatic usage.
    • Supports non-option argument text for automatic usage.
    • Supports custom extra info text for automatic usage.
    • Uses a single OptionException type for signalling problems.
    • Supports query for single-hyphen argument to facilitate reading from STDIN.
    • Internationalization support.

    Automatic usage printing can be accessed via the printUsage(...) method, allowing printing to a specified PrintStream in either long or short versions. The options are printed in the same order in which they were added to the CLAParser instance. The command used to launch the application can either be user-specified, or can be deduced automatically if required. Internationalization support is included for a few locales (en/fr/de/es/pt), and may be extended in future. If required users may author their own locale property files to reference.

    For an example of how to use the CLAParser class, visit the website.

    Author:
    Giles Winstanley
    See Also:
    JCLAP website
    • Constructor Detail

      • CLAParser

        public CLAParser​(Locale locale)
        Creates a new CLAParser instance, using the default locale.
        Parameters:
        locale - Locale to use for i18n resources
      • CLAParser

        public CLAParser()
        Creates a new CLAParser instance, using the default locale.
    • Method Detail

      • showLongNamesInShortUsage

        public void showLongNamesInShortUsage()
        Allows overriding default behaviour of only showing short option names in the short automatic usage message. Once this method has been called, any long option names are also shown (as in older versions).
      • parsedSolitaryHyphen

        public boolean parsedSolitaryHyphen()
        Returns whether a single - argument was found. This is typically used to specify that an application should read from STDIN or write to STDOUT. The argument is written to the non-option arguments, so this can be tested independently, but this is just a convenience method to do the same.
        Returns:
        true if solitary hyphen was parsed, false otherwise
      • addOption

        public <T> Option<T> addOption​(Option<T> opt)
        Adds the specified Option to the list of those to be parsed from the command-line arguments.
        Type Parameters:
        T - the return type of the option
        Parameters:
        opt - Option instance to add
        Returns:
        the Option instance that was added
        Throws:
        IllegalArgumentException - if either short/long name of the specified Option is already in use
      • setHidden

        public void setHidden​(String optionName)
                       throws OptionException
        Hides the specified Option from being printed in the usage message.
        Parameters:
        optionName - short/long name of option to "hide"
        Throws:
        OptionException - if the specified option can't be found
      • addBooleanOption

        public Option<Boolean> addBooleanOption​(String shortName,
                                                String longName,
                                                String description,
                                                int minCount,
                                                int maxCount)
        Convenience method to add an Option of type Boolean.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addBooleanOption

        public Option<Boolean> addBooleanOption​(String shortName,
                                                String longName,
                                                String description,
                                                boolean allowMany)
        Convenience method to add an Option of type Boolean.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addBooleanOption

        public Option<Boolean> addBooleanOption​(String shortName,
                                                String longName,
                                                String description,
                                                int maxCount)
        Convenience method to add an Option of type Boolean.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addBooleanOption

        public Option<Boolean> addBooleanOption​(String shortName,
                                                String longName,
                                                String description)
        Convenience method to add an Option of type Boolean (single value allowed).
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        Returns:
        the new Option instance
      • addBooleanOption

        public Option<Boolean> addBooleanOption​(String shortName,
                                                boolean allowMany)
        Convenience method to add an Option of type Boolean.
        Parameters:
        shortName - short name of the option
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addBooleanOption

        public Option<Boolean> addBooleanOption​(String shortName,
                                                int maxCount)
        Convenience method to add an Option of type Boolean.
        Parameters:
        shortName - short name of the option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addBooleanOption

        public Option<Boolean> addBooleanOption​(String shortName)
        Convenience method to add an Option of type Boolean (single value allowed).
        Parameters:
        shortName - short name of the option
        Returns:
        the new Option instance
      • addIntegerOption

        public Option<Integer> addIntegerOption​(String shortName,
                                                String longName,
                                                String description,
                                                boolean mandatory,
                                                boolean allowMany)
        Convenience method to add an Option of type Integer.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addIntegerOption

        public Option<Integer> addIntegerOption​(String shortName,
                                                String longName,
                                                String description,
                                                int minCount,
                                                int maxCount)
        Convenience method to add an Option of type Integer.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addIntegerOption

        public Option<Integer> addIntegerOption​(String shortName,
                                                boolean mandatory,
                                                boolean allowMany)
        Convenience method to add an Option of type Integer.
        Parameters:
        shortName - short name of the option
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addIntegerOption

        public Option<Integer> addIntegerOption​(String shortName,
                                                int minCount,
                                                int maxCount)
        Convenience method to add an Option of type Integer.
        Parameters:
        shortName - short name of the option
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addLongOption

        public Option<Long> addLongOption​(String shortName,
                                          String longName,
                                          String description,
                                          boolean mandatory,
                                          boolean allowMany)
        Convenience method to add an Option of type Long.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addLongOption

        public Option<Long> addLongOption​(String shortName,
                                          String longName,
                                          String description,
                                          int minCount,
                                          int maxCount)
        Convenience method to add an Option of type Long.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addLongOption

        public Option<Long> addLongOption​(String shortName,
                                          boolean mandatory,
                                          boolean allowMany)
        Convenience method to add an Option of type Long.
        Parameters:
        shortName - short name of the option
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addLongOption

        public Option<Long> addLongOption​(String shortName,
                                          int minCount,
                                          int maxCount)
        Convenience method to add an Option of type Long.
        Parameters:
        shortName - short name of the option
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addDoubleOption

        public Option<Double> addDoubleOption​(String shortName,
                                              String longName,
                                              String description,
                                              boolean mandatory,
                                              boolean allowMany)
        Convenience method to add an Option of type Double.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addDoubleOption

        public Option<Double> addDoubleOption​(String shortName,
                                              String longName,
                                              String description,
                                              int minCount,
                                              int maxCount)
        Convenience method to add an Option of type Double.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addDoubleOption

        public Option<Double> addDoubleOption​(String shortName,
                                              boolean mandatory,
                                              boolean allowMany)
        Convenience method to add an Option of type Double.
        Parameters:
        shortName - short name of the option
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addDoubleOption

        public Option<Double> addDoubleOption​(String shortName,
                                              int minCount,
                                              int maxCount)
        Convenience method to add an Option of type Double.
        Parameters:
        shortName - short name of the option
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addFloatOption

        public Option<Float> addFloatOption​(String shortName,
                                            String longName,
                                            String description,
                                            boolean mandatory,
                                            boolean allowMany)
        Convenience method to add an Option of type Float.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addFloatOption

        public Option<Float> addFloatOption​(String shortName,
                                            String longName,
                                            String description,
                                            int minCount,
                                            int maxCount)
        Convenience method to add an Option of type Float.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addFloatOption

        public Option<Float> addFloatOption​(String shortName,
                                            boolean mandatory,
                                            boolean allowMany)
        Convenience method to add an Option of type Float.
        Parameters:
        shortName - short name of the option
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addFloatOption

        public Option<Float> addFloatOption​(String shortName,
                                            int minCount,
                                            int maxCount)
        Convenience method to add an Option of type Float.
        Parameters:
        shortName - short name of the option
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addStringOption

        public Option<String> addStringOption​(String shortName,
                                              String longName,
                                              String description,
                                              boolean mandatory,
                                              boolean allowMany)
        Convenience method to add an Option of type String.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addStringOption

        public Option<String> addStringOption​(String shortName,
                                              String longName,
                                              String description,
                                              int minCount,
                                              int maxCount)
        Convenience method to add an Option of type String.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addStringOption

        public Option<String> addStringOption​(String shortName,
                                              String longName,
                                              String description)
        Convenience method to add an Option of type String (single value allowed, non-mandatory).
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        Returns:
        the new Option instance
      • addStringOption

        public Option<String> addStringOption​(String shortName,
                                              boolean mandatory,
                                              boolean allowMany)
        Convenience method to add an Option of type String (no long name, no description).
        Parameters:
        shortName - short name of the option
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addStringOption

        public Option<String> addStringOption​(String shortName,
                                              int minCount,
                                              int maxCount)
        Convenience method to add an Option of type String (no long name, no description).
        Parameters:
        shortName - short name of the option
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addFileOption

        public Option<String> addFileOption​(String shortName,
                                            String longName,
                                            String description,
                                            boolean mandatory,
                                            boolean allowMany)
        Convenience method to add an Option of type String which refers to a file/folder. For example, use this method to:
        • Allow specification of a existing or new file/folder.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addFileNewOption

        public Option<String> addFileNewOption​(String shortName,
                                               String longName,
                                               String description,
                                               boolean mandatory,
                                               boolean allowMany)
        Convenience method to add an Option of type String which refers to a new or existing file/folder.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addFileExistingOption

        public Option<String> addFileExistingOption​(String shortName,
                                                    String longName,
                                                    String description,
                                                    int minCount,
                                                    int maxCount)
        Convenience method to add an Option of type String which refers to an existing file (which is not a folder). For example, use this method to:
        • Enforce specification of an existing file (which is not a folder)
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addFileExistingOption

        public Option<String> addFileExistingOption​(String shortName,
                                                    String longName,
                                                    String description,
                                                    boolean mandatory,
                                                    boolean allowMany)
        Convenience method to add an Option of type String which refers to an existing file (which is not a folder). For example, use this method to:
        • Enforce specification of an existing file (which is not a folder)
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addDirectoryExistingOption

        public Option<String> addDirectoryExistingOption​(String shortName,
                                                         String longName,
                                                         String description,
                                                         int minCount,
                                                         int maxCount)
        Convenience method to add an Option of type String which refers to an existing folder. For example, use this method to enforce specification of an existing folder. To allow specification of a new folder (one that does not yet exist), use the addFileOption() method instead.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addFolderExistingOption

        @Deprecated
        public Option<String> addFolderExistingOption​(String shortName,
                                                      String longName,
                                                      String description,
                                                      int minCount,
                                                      int maxCount)
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addDirectoryExistingOption

        public Option<String> addDirectoryExistingOption​(String shortName,
                                                         String longName,
                                                         String description,
                                                         boolean mandatory,
                                                         boolean allowMany)
        Convenience method to add an Option of type String which refers to an existing folder. For example, use this method to enforce specification of an existing folder. To allow specification of a new folder (one that does not yet exist), use the addFileOption() method instead.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addFolderExistingOption

        @Deprecated
        public Option<String> addFolderExistingOption​(String shortName,
                                                      String longName,
                                                      String description,
                                                      boolean mandatory,
                                                      boolean allowMany)
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addEnumStringOption

        public Option<String> addEnumStringOption​(String shortName,
                                                  String longName,
                                                  String description,
                                                  boolean mandatory,
                                                  boolean allowMany,
                                                  String[] allowedValues,
                                                  boolean ignoreCase)
        Convenience method to add an Option of enumerated String type.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        allowedValues - array of string values allowed by this enumerated option
        ignoreCase - whether to ignore the case of string values
        Returns:
        the new Option instance
      • addEnumStringOption

        public Option<String> addEnumStringOption​(String shortName,
                                                  String longName,
                                                  String description,
                                                  int minCount,
                                                  int maxCount,
                                                  String[] allowedValues,
                                                  boolean ignoreCase)
        Convenience method to add an Option of enumerated String type.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        allowedValues - array of string values allowed by this enumerated option
        ignoreCase - whether to ignore the case of string values
        Returns:
        the new Option instance
      • addEnumStringOption

        public Option<String> addEnumStringOption​(String shortName,
                                                  String longName,
                                                  String description,
                                                  boolean mandatory,
                                                  boolean allowMany,
                                                  String[] allowedValues)
        Convenience method to add an Option of enumerated String type (case-insensitive string matching).
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        allowedValues - array of string values allowed by this enumerated option
        Returns:
        the new Option instance
      • addEnumStringOption

        public Option<String> addEnumStringOption​(String shortName,
                                                  String longName,
                                                  String description,
                                                  String[] allowedValues)
        Convenience method to add an Option of enumerated String type (single value allowed, non-mandatory).
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        allowedValues - array of string values allowed by this enumerated option
        Returns:
        the new Option instance
      • addEnumIntegerOption

        public Option<Integer> addEnumIntegerOption​(String shortName,
                                                    String longName,
                                                    String description,
                                                    boolean mandatory,
                                                    boolean allowMany,
                                                    int[] allowedValues)
        Convenience method to add an Option of enumerated Integer type.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        allowedValues - array of string values allowed by this enumerated option
        Returns:
        the new Option instance
      • addEnumIntegerOption

        public Option<Integer> addEnumIntegerOption​(String shortName,
                                                    String longName,
                                                    String description,
                                                    int[] allowedValues)
        Convenience method to add an Option of enumerated Integer type (single value allowed, non-mandatory).
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        allowedValues - array of string values allowed by this enumerated option
        Returns:
        the new Option instance
      • addLocalDateOption

        public Option<LocalDate> addLocalDateOption​(String shortName,
                                                    String longName,
                                                    String description,
                                                    boolean mandatory,
                                                    boolean allowMany)
        Convenience method to add an Option of type LocalDate.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addLocalDateOption

        public Option<LocalDate> addLocalDateOption​(String shortName,
                                                    String longName,
                                                    String description,
                                                    int minCount,
                                                    int maxCount)
        Convenience method to add an Option of type LocalDate.
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • addLocalDateOption

        public Option<LocalDate> addLocalDateOption​(String shortName,
                                                    String longName,
                                                    String description)
        Convenience method to add an Option of type LocalDate (single value allowed, non-mandatory).
        Parameters:
        shortName - short name of the option
        longName - long name of the option
        description - helpful description of the option (printed for usage message)
        Returns:
        the new Option instance
      • addLocalDateOption

        public Option<LocalDate> addLocalDateOption​(String shortName,
                                                    boolean mandatory,
                                                    boolean allowMany)
        Convenience method to add an Option of type LocalDate (no long name, no description).
        Parameters:
        shortName - short name of the option
        mandatory - whether this option must be specified
        allowMany - whether this option can take more than one value (i.e. be specified more than once)
        Returns:
        the new Option instance
      • addLocalDateOption

        public Option<LocalDate> addLocalDateOption​(String shortName,
                                                    int minCount,
                                                    int maxCount)
        Convenience method to add an Option of type LocalDate (no long name, no description).
        Parameters:
        shortName - short name of the option
        minCount - minimum number of occurrences required for this option
        maxCount - maximum number of occurrences required for this option
        Returns:
        the new Option instance
      • getOption

        public <T> Option<T> getOption​(String optionName,
                                       Class<T> type)
                                throws OptionException
        Returns the Option with the specified name (either short or long).
        Type Parameters:
        T - the return type of the option
        Parameters:
        optionName - option name for which to get value (either short/long)
        type - class type for option value compatibility check
        Returns:
        Option instance
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getOptions

        public List<Option> getOptions()
        Returns all the Option instances registered with the parser.
        Returns:
        list of options
      • getOptionValues

        public <T> List<T> getOptionValues​(Option<T> opt)
                                    throws OptionException
        Returns a list of all the parsed values for the specified option, or an empty list if the option was not set.
        Type Parameters:
        T - the return type of the option
        Parameters:
        opt - option for which to get values
        Returns:
        A list of all the parsed values for the specified option, or an empty list if the option was not set.
        Throws:
        OptionException - if opt is null, cannot be found, or is of the wrong type
      • getOptionValues

        public <T> List<T> getOptionValues​(String optionName,
                                           Class<T> type)
                                    throws OptionException
        Returns a list of all the parsed values for the specified option, or an empty list if the option was not set. This method performs type-checking of the Option.
        Type Parameters:
        T - the return type of the option
        Parameters:
        optionName - option name for which to get value (either short/long)
        type - class type for option value compatibility check
        Returns:
        list of option values
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getOptionValue

        public <T> T getOptionValue​(Option<T> opt)
                             throws OptionException
        Convenience method to return the parsed value of the specified option, or null if the option was not set. This method is only usable with options that cannot take multiple values.
        Type Parameters:
        T - the return type of the option
        Parameters:
        opt - option for which to get value
        Returns:
        option value
        Throws:
        OptionException - if opt is null, cannot be found, or is of the wrong type
      • getOptionValue

        public <T> T getOptionValue​(Option<T> opt,
                                    T def)
                             throws OptionException
        Convenience method to return the parsed value of the specified option, or the specified default value if the option was not set. This method is only usable with options that cannot take multiple values.
        Type Parameters:
        T - the return type of the option
        Parameters:
        opt - option for which to get value
        def - default value to use if the option was not set
        Returns:
        option value
        Throws:
        OptionException - if opt is null, cannot be found, or is of the wrong type
      • getOptionValue

        public <T> T getOptionValue​(String optionName,
                                    Class<T> type,
                                    T def)
                             throws OptionException
        Convenience method to return the parsed value of the named option, or the specified default value if the option was not set. This method should only be used with typed options that cannot take multiple values.
        Type Parameters:
        T - the return type of the option
        Parameters:
        optionName - name of the option
        type - class type for option value compatibility check
        def - default value to use if the option was not set
        Returns:
        option value, or default value if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getOptionValue

        public <T> T getOptionValue​(String optionName,
                                    Class<T> type)
                             throws OptionException
        Convenience method to return the parsed value of the named option, or null if the option was not set, equivalent to getOptionValue(optionName, type, null). This method should only be used with typed options that cannot take multiple values.
        Type Parameters:
        T - the return type of the option
        Parameters:
        optionName - name of the option
        type - class type for option value compatibility check
        Returns:
        option value
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getBooleanOptionValues

        public List<Boolean> getBooleanOptionValues​(String optionName)
                                             throws OptionException
        Returns a list of all the parsed values for the specified option, or an empty list if the option was not set.
        Parameters:
        optionName - name of the option
        Returns:
        list of option values
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getBooleanOptionValue

        public Boolean getBooleanOptionValue​(String optionName,
                                             Boolean def)
                                      throws OptionException
        Convenience method to return the parsed value of the named option, or the specified default value if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        def - default value to use if the option was not set
        Returns:
        option value, or default value if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getBooleanOptionValue

        public Boolean getBooleanOptionValue​(String optionName)
                                      throws OptionException
        Convenience method to return the parsed value of the named option, or false if the option was not set. This method should only be used with options that cannot take multiple values. NOTE: this method operates slightly differently compared to the other getXOptionValue(String) methods, in that when the option is not defined, it returned Boolean.FALSE instead of null.
        Parameters:
        optionName - name of the option
        Returns:
        option value, or null if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getIntegerOptionValues

        public List<Integer> getIntegerOptionValues​(String optionName)
                                             throws OptionException
        Returns a list of all the parsed values for the specified option, or an empty list if the option was not set.
        Parameters:
        optionName - name of the option
        Returns:
        list of option values
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getIntegerOptionValue

        public Integer getIntegerOptionValue​(String optionName,
                                             Integer def)
                                      throws OptionException
        Convenience method to return the parsed value of the named option, or the specified default value if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        def - default value to use if the option was not set
        Returns:
        option value, or default value if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getIntegerOptionValue

        public Integer getIntegerOptionValue​(String optionName)
                                      throws OptionException
        Convenience method to return the parsed value of the named option, or null if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        Returns:
        option value, or null if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getLongOptionValues

        public List<Long> getLongOptionValues​(String optionName)
                                       throws OptionException
        Returns a list of all the parsed values for the specified option, or an empty list if the option was not set.
        Parameters:
        optionName - name of the option
        Returns:
        list of option values
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getLongOptionValue

        public Long getLongOptionValue​(String optionName,
                                       Long def)
                                throws OptionException
        Convenience method to return the parsed value of the named option, or the specified default value if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        def - default value to use if the option was not set
        Returns:
        option value, or default value if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getLongOptionValue

        public Long getLongOptionValue​(String optionName)
                                throws OptionException
        Convenience method to return the parsed value of the named option, or null if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        Returns:
        option value, or null if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getDoubleOptionValues

        public List<Double> getDoubleOptionValues​(String optionName)
                                           throws OptionException
        Returns a list of all the parsed values for the specified option, or an empty list if the option was not set.
        Parameters:
        optionName - name of the option
        Returns:
        list of option values
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getDoubleOptionValue

        public Double getDoubleOptionValue​(String optionName,
                                           Double def)
                                    throws OptionException
        Convenience method to return the parsed value of the named option, or the specified default value if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        def - default value to use if the option was not set
        Returns:
        option value, or default value if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getDoubleOptionValue

        public Double getDoubleOptionValue​(String optionName)
                                    throws OptionException
        Convenience method to return the parsed value of the named option, or null if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        Returns:
        option value, or null if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getFloatOptionValues

        public List<Float> getFloatOptionValues​(String optionName)
                                         throws OptionException
        Returns a list of all the parsed values for the specified option, or an empty list if the option was not set.
        Parameters:
        optionName - name of the option
        Returns:
        list of option values
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getFloatOptionValue

        public Float getFloatOptionValue​(String optionName,
                                         Float def)
                                  throws OptionException
        Convenience method to return the parsed value of the named option, or the specified default value if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        def - default value to use if the option was not set
        Returns:
        option value, or default value if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getFloatOptionValue

        public Float getFloatOptionValue​(String optionName)
                                  throws OptionException
        Convenience method to return the parsed value of the named option, or null if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        Returns:
        option value, or null if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getStringOptionValues

        public List<String> getStringOptionValues​(String optionName)
                                           throws OptionException
        Returns a list of all the parsed values for the specified option, or an empty list if the option was not set.
        Parameters:
        optionName - name of the option
        Returns:
        list of option values
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getStringOptionValue

        public String getStringOptionValue​(String optionName,
                                           String def)
                                    throws OptionException
        Convenience method to return the parsed value of the named option, or the specified default value if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        def - default value to use if the option was not set
        Returns:
        option value, or default value if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getStringOptionValue

        public String getStringOptionValue​(String optionName)
                                    throws OptionException
        Convenience method to return the parsed value of the named option, or null if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        Returns:
        option value, or null if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getLocalDateOptionValues

        public List<LocalDate> getLocalDateOptionValues​(String optionName)
                                                 throws OptionException
        Returns a list of all the parsed values for the specified option, or an empty list if the option was not set.
        Parameters:
        optionName - name of the option
        Returns:
        list of option values
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getLocalDateOptionValue

        public LocalDate getLocalDateOptionValue​(String optionName,
                                                 LocalDate def)
                                          throws OptionException
        Convenience method to return the parsed value of the named option, or the specified default value if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        def - default value to use if the option was not set
        Returns:
        option value, or default value if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getLocalDateOptionValue

        public LocalDate getLocalDateOptionValue​(String optionName)
                                          throws OptionException
        Convenience method to return the parsed value of the named option, or null if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        Returns:
        option value, or null if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getFileOptionValues

        public List<File> getFileOptionValues​(String optionName)
                                       throws OptionException
        Returns a list of all the parsed values for the specified option, or an empty list if the option was not set.
        Parameters:
        optionName - name of the option
        Returns:
        list of option values
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getFileOptionValue

        public File getFileOptionValue​(String optionName,
                                       File def)
                                throws OptionException
        Convenience method to return the parsed value of the named option, or the specified default value if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        def - default value to use if the option was not set
        Returns:
        option value, or default value if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getFileOptionValue

        public File getFileOptionValue​(String optionName)
                                throws OptionException
        Convenience method to return the parsed value of the named option, or null if the option was not set. This method should only be used with options that cannot take multiple values.
        Parameters:
        optionName - name of the option
        Returns:
        option value, or null if option is unset
        Throws:
        OptionException - if optionName is null, cannot be found, or is of the wrong type
      • getNonOptionArguments

        public List<String> getNonOptionArguments()
        Returns a list of the non-option command-line arguments. Note that this list includes all arguments that are not related to options, and as a result may include "orphan" arguments (in between correctly specified options) in addition to the expected arguments that are specified after all the options.

        For example:

             java DemoApp -i 10 -f 1.2 foo -s bar fu bar
         

        for an application with value-taking options i/f/s, would return [foo,fu,bar] from this method.

        Returns:
        A list of the command-line string arguments that were not parsed as options.
      • parse

        public void parse​(String[] args)
                   throws OptionException
        Extract the option-mapped and unmapped arguments from the given array of command-line arguments, using the specified locale.
        Parameters:
        args - command-line arguments (as passed into main method)
        Throws:
        OptionException - if an problem is encountered parsing the specified arguments
      • printUsage

        public void printUsage​(PrintStream ps,
                               boolean longUsage)
        Prints the command-line usage message to the specified PrintStream, making a guess at the application launch command. The usage message is automatically created using the options specified. The usage can be displayed in either short or long versions, although the long version uses the options' description fields, which are not required, but should generally be included if intending to print the long usage.

        This is equivalent to calling printUsage(ps, longUsage, null, null, null).

        Parameters:
        ps - PrintStream to which to print usage message
        longUsage - whether to print long version of usage message (otherwise print short version)
      • printUsage

        @Deprecated
        public void printUsage​(PrintStream ps,
                               boolean longUsage,
                               String suffixArgs)
        Deprecated.
        Recommend to use printUsage(PrintStream, boolean, String, String, String) as a replacement to avoid confusion over parameter ordering
        Equivalent to: printUsage(ps, longUsage, null, suffixArgs, null).
        Parameters:
        ps - PrintStream to which to print usage message
        longUsage - whether to print long version of usage message (otherwise print short version)
        suffixArgs - usage suffix string for defining extra arguments
      • printUsage

        @Deprecated
        public void printUsage​(PrintStream ps,
                               boolean longUsage,
                               String appString,
                               String suffixArgs)
        Deprecated.
        Recommend to use printUsage(PrintStream, boolean, String, String, String) as a replacement to avoid confusion over parameter ordering
        Equivalent to: printUsage(ps, longUsage, appString, suffixArgs, null).
        Parameters:
        ps - PrintStream to which to print usage message
        longUsage - whether to print long version of usage message (otherwise print short version)
        appString - usage prefix string describing how application is launched; null specifies to create value automatically (e.g. "java foo.bar.AppName")
        suffixArgs - usage suffix string for defining extra arguments
      • printUsage

        public void printUsage​(PrintStream ps,
                               boolean longUsage,
                               String appString,
                               String suffixArgs,
                               String extraInfo)
        Prints the command-line usage message to the specified PrintStream. The usage message is automatically created using the options specified. The usage can be displayed in either short or long versions, although the long version uses the options' description fields, which are not required, but should generally be included if intending to print the long usage.
        Parameters:
        ps - PrintStream to which to print usage message
        longUsage - whether to print long version of usage message (otherwise print short version)
        appString - usage prefix string describing how application is launched; null specifies to create value automatically (e.g. "java foo.bar.AppName")
        suffixArgs - usage suffix string for defining extra arguments
        extraInfo - additional information to be displayed before options (only when longUsage is true)