Class Options

  • All Implemented Interfaces:
    java.io.Serializable

    public class Options
    extends java.lang.Object
    implements java.io.Serializable
    Main entry-point into the library.

    Options represents a collection of Option objects, which describe the possible options for a command-line.

    It may flexibly parse long and short options, with or without values. Additionally, it may parse only a portion of a command-line, allowing for flexible multi-stage parsing.

    See Also:
    CommandLine, Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Options()
      Constructs new instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Options addOption​(java.lang.String opt, boolean hasArg, java.lang.String description)
      Adds an option that only contains a short-name.
      Options addOption​(java.lang.String opt, java.lang.String description)
      Adds an option that only contains a short name.
      Options addOption​(java.lang.String opt, java.lang.String longOpt, boolean hasArg, java.lang.String description)
      Adds an option that contains a short-name and a long-name.
      Options addOption​(Option opt)
      Adds an option instance.
      Options addOptionGroup​(OptionGroup optionGroup)
      Adds the specified option group.
      Options addOptions​(Options options)
      Adds options to this option.
      Options addRequiredOption​(java.lang.String opt, java.lang.String longOpt, boolean hasArg, java.lang.String description)
      Adds an option that contains a short-name and a long-name.
      java.util.List<java.lang.String> getMatchingOptions​(java.lang.String opt)
      Gets the options with a long name starting with the name specified.
      Option getOption​(java.lang.String opt)
      Gets the Option matching the long or short name specified.
      OptionGroup getOptionGroup​(Option option)
      Gets the OptionGroup the opt belongs to.
      java.util.Collection<Option> getOptions()
      Gets a read-only list of options in this set.
      java.util.List<?> getRequiredOptions()
      Gets the required options.
      boolean hasLongOption​(java.lang.String opt)
      Tests whether the named Option is a member of this Options.
      boolean hasOption​(java.lang.String opt)
      Tests whether the named Option is a member of this Options.
      boolean hasShortOption​(java.lang.String opt)
      Tests whether the named Option is a member of this Options.
      java.lang.String toString()
      Dump state, suitable for debugging.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Options

        public Options()
        Constructs new instance.
    • Method Detail

      • addOption

        public Options addOption​(Option opt)
        Adds an option instance.
        Parameters:
        opt - the option that is to be added.
        Returns:
        the resulting Options instance.
      • addOption

        public Options addOption​(java.lang.String opt,
                                 boolean hasArg,
                                 java.lang.String description)
        Adds an option that only contains a short-name.

        It may be specified as requiring an argument.

        Parameters:
        opt - Short single-character name of the option.
        hasArg - flag signaling if an argument is required after this option.
        description - Self-documenting description.
        Returns:
        the resulting Options instance.
      • addOption

        public Options addOption​(java.lang.String opt,
                                 java.lang.String description)
        Adds an option that only contains a short name.

        The option does not take an argument.

        Parameters:
        opt - Short single-character name of the option.
        description - Self-documenting description.
        Returns:
        the resulting Options instance.
        Since:
        1.3
      • addOption

        public Options addOption​(java.lang.String opt,
                                 java.lang.String longOpt,
                                 boolean hasArg,
                                 java.lang.String description)
        Adds an option that contains a short-name and a long-name.

        It may be specified as requiring an argument.

        Parameters:
        opt - Short single-character name of the option.
        longOpt - Long multi-character name of the option.
        hasArg - flag signaling if an argument is required after this option.
        description - Self-documenting description.
        Returns:
        the resulting Options instance.
      • addOptionGroup

        public Options addOptionGroup​(OptionGroup optionGroup)
        Adds the specified option group.

        An Option cannot be required if it is in an OptionGroup, either the group is required or nothing is required. This means that Option in the given group are set to optional.

        Parameters:
        optionGroup - the OptionGroup that is to be added.
        Returns:
        the resulting Options instance.
      • addOptions

        public Options addOptions​(Options options)
        Adds options to this option. If any Option in options already exists in this Options an IllegalArgumentException is thrown.
        Parameters:
        options - the options to add.
        Returns:
        The resulting Options instance.
        Since:
        1.7.0
      • addRequiredOption

        public Options addRequiredOption​(java.lang.String opt,
                                         java.lang.String longOpt,
                                         boolean hasArg,
                                         java.lang.String description)
        Adds an option that contains a short-name and a long-name.

        The added option is set as required. It may be specified as requiring an argument. This method is a shortcut for:

         
         Options option = new Option(opt, longOpt, hasArg, description);
         option.setRequired(true);
         options.add(option);
         
         
        Parameters:
        opt - Short single-character name of the option.
        longOpt - Long multi-character name of the option.
        hasArg - flag signaling if an argument is required after this option.
        description - Self-documenting description.
        Returns:
        the resulting Options instance.
        Since:
        1.4
      • getMatchingOptions

        public java.util.List<java.lang.String> getMatchingOptions​(java.lang.String opt)
        Gets the options with a long name starting with the name specified.
        Parameters:
        opt - the partial name of the option.
        Returns:
        the options matching the partial name specified, or an empty list if none matches.
        Since:
        1.3
      • getOption

        public Option getOption​(java.lang.String opt)
        Gets the Option matching the long or short name specified.

        The leading hyphens in the name are ignored (up to 2).

        Parameters:
        opt - short or long name of the Option.
        Returns:
        the option represented by opt.
      • getOptionGroup

        public OptionGroup getOptionGroup​(Option option)
        Gets the OptionGroup the opt belongs to.
        Parameters:
        option - the option whose OptionGroup is being queried.
        Returns:
        the OptionGroup if opt is part of an OptionGroup, otherwise return null.
      • getOptions

        public java.util.Collection<OptiongetOptions()
        Gets a read-only list of options in this set.
        Returns:
        read-only Collection of Option objects in this descriptor.
      • getRequiredOptions

        public java.util.List<?> getRequiredOptions()
        Gets the required options.
        Returns:
        read-only List of required options.
      • hasLongOption

        public boolean hasLongOption​(java.lang.String opt)
        Tests whether the named Option is a member of this Options.
        Parameters:
        opt - long name of the Option.
        Returns:
        true if the named Option is a member of this Options.
        Since:
        1.3
      • hasOption

        public boolean hasOption​(java.lang.String opt)
        Tests whether the named Option is a member of this Options.
        Parameters:
        opt - short or long name of the Option.
        Returns:
        true if the named Option is a member of this Options.
      • hasShortOption

        public boolean hasShortOption​(java.lang.String opt)
        Tests whether the named Option is a member of this Options.
        Parameters:
        opt - short name of the Option.
        Returns:
        true if the named Option is a member of this Options.
        Since:
        1.3
      • toString

        public java.lang.String toString()
        Dump state, suitable for debugging.
        Overrides:
        toString in class java.lang.Object
        Returns:
        Stringified form of this object.