Class Option

  • Direct Known Subclasses:
    TypedOption

    public class Option
    extends Object
    Models command line options. Options are values passed to a command line interface using -x or --x. Supported syntaxes depend on the parser.

    Short name is generally used with a single dash, while long name requires a double-dash.

    Author:
    Clement Escoffier
    • Field Detail

      • NO_NAME

        public static final String NO_NAME
        The default long name / short name of the option. Notice that options requires at least a regular long name or short name.
        See Also:
        Constant Field Values
      • longName

        protected String longName
        the option long name.
      • shortName

        protected String shortName
        the option short name.
      • argName

        protected String argName
        the option name used in usage message.
      • description

        protected String description
        The option description.
      • required

        protected boolean required
        whether or not the option is required. A mandatory not set throws a MissingOptionException.
      • hidden

        protected boolean hidden
        whether or not the option is hidden. Hidden options are not displayed in usage.
      • singleValued

        protected boolean singleValued
        whether or not the option receives a single value. true by default.
      • multiValued

        protected boolean multiValued
        whether or not the option can recevie multiple values.
      • defaultValue

        protected String defaultValue
        the option default value.
      • flag

        protected boolean flag
        whether or not the option is a flag. Flag option does not require a value. If an option is a flag, it is evaluated to {@link true} if the option is used in the command line.
      • help

        protected boolean help
        whether or not the option is a "help" option. Is the user execute the command line enabling a help option, the command line validation won't fail, and give the command the opportunity to display the usage message, instead of throwing an exception during the parsing.
      • choices

        protected Set<String> choices
        if the option value has to be in a definited set, this field represents the set of values. Value are sorted alphabetically.
    • Constructor Detail

      • Option

        public Option()
        Creates a new empty instance of Option.
      • Option

        public Option​(Option other)
        Creates a new instance of Option by copying the state of another Option.
        Parameters:
        other - the other option
      • Option

        public Option​(JsonObject json)
        Creates a new instance of Option from the given JsonObject
        Parameters:
        json - the json object representing the option
        See Also:
        toJson()
    • Method Detail

      • toJson

        public JsonObject toJson()
        Gets the json representation of this Option.
        Returns:
        the json representation
      • ensureValidity

        public void ensureValidity()
        Checks whether or not the option is valid. This implementation check that it has a short name or a long name. This method is intended to be extended by sub-class. Parser should check that the set of option of a CLI is valid before starting the parsing.

        If the configuration is not valid, this method throws a IllegalArgumentException.

      • acceptValue

        public boolean acceptValue()
        Returns:
        whether or not the option can receive a value.
      • getName

        public String getName()
        Returns:
        the option name. It returns the long name if set, the short name otherwise. It cannot return null for valid option
        See Also:
        ensureValidity()
      • isMultiValued

        public boolean isMultiValued()
        Returns:
        whether or not this option can receive several values.
      • setMultiValued

        public Option setMultiValued​(boolean multiValued)
        Sets whether or not this option can receive several values.
        Parameters:
        multiValued - whether or not this option is multi-valued.
        Returns:
        the current Option instance
      • isSingleValued

        public boolean isSingleValued()
        Returns:
        whether or not this option is single valued.
      • setSingleValued

        public Option setSingleValued​(boolean singleValued)
        Sets whether or not this option can receive a value.
        Parameters:
        singleValued - whether or not this option is single-valued.
        Returns:
        the current Option instance
      • getArgName

        public String getArgName()
        Returns:
        the option arg name used in usage messages, null if not set.
      • setArgName

        public Option setArgName​(String argName)
        Sets te arg name for this option.
        Parameters:
        argName - the arg name, must not be null
        Returns:
        the current Option instance
      • getDescription

        public String getDescription()
        Returns:
        the description of this option, null if not set.
      • setDescription

        public Option setDescription​(String description)
        Sets te description of this option.
        Parameters:
        description - the description
        Returns:
        the current Option instance
      • isHidden

        public boolean isHidden()
        Returns:
        whtehr or not this option is hidden.
      • setHidden

        public Option setHidden​(boolean hidden)
        Sets whether or not this option should be hidden
        Parameters:
        hidden - true to make this option hidden, {@link false} otherwise
        Returns:
        the current Option instance
      • getLongName

        public String getLongName()
        Returns:
        the option long name, null if not set.
      • setLongName

        public Option setLongName​(String longName)
        Sets the long name of this option.
        Parameters:
        longName - the long name
        Returns:
        the current Option instance
      • isRequired

        public boolean isRequired()
        Returns:
        whether or not this option is mandatory.
      • setRequired

        public Option setRequired​(boolean required)
        Sets whether or not this option is mandatory.
        Parameters:
        required - true to make this option mandatory, {@link false} otherwise
        Returns:
        the current Option instance
      • getShortName

        public String getShortName()
        Returns:
        the short name of this option, null if not set.
      • setShortName

        public Option setShortName​(String shortName)
        Sets the short name of this option.
        Parameters:
        shortName - the short name
        Returns:
        the current Option instance
      • getDefaultValue

        public String getDefaultValue()
        Returns:
        the default value of this option, null if not set.
      • setDefaultValue

        public Option setDefaultValue​(String defaultValue)
        Sets the default value of this option
        Parameters:
        defaultValue - the default value
        Returns:
        the current Option instance
      • isFlag

        public boolean isFlag()
        Returns:
        whether or not this option is a flag.
      • setFlag

        public Option setFlag​(boolean flag)
        Configures the current Option to be a flag. It will be evaluated to true if it's found in the command line. If you need a flag that may receive a value, use, in this order:
           option.setFlag(true).setSingleValued(true)
         
        Parameters:
        flag - whether or not the option is a flag.
        Returns:
        the current Option
      • isHelp

        public boolean isHelp()
        Checks whether or not this option is a "Help" option.
        Returns:
        true if this option is a "help" option.
      • setHelp

        public Option setHelp​(boolean help)
        Sets whether or not this option is a "help" option
        Parameters:
        help - true to set this option as a "Help" option
        Returns:
        the current Option
      • getChoices

        public Set<String> getChoices()
        Returns:
        get the list of choices for the given option. Empty if this option does not define choices.
      • setChoices

        public Option setChoices​(Set<String> choices)
        Sets the list of values accepted by this option. If the value set by the user does not match once of these values, a InvalidValueException exception is thrown.
        Parameters:
        choices - the choices
        Returns:
        the current Option
      • addChoice

        public Option addChoice​(String choice)
        Adds a choice to the list of values accepted by this option. If the value set by the user does not match once of these values, a InvalidValueException exception is thrown.
        Parameters:
        choice - the choice
        Returns:
        the current Option