Package io.vertx.core.cli
Class Option
- java.lang.Object
-
- io.vertx.core.cli.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 Summary
Fields Modifier and Type Field Description protected StringargNamethe option name used in usage message.protected Set<String>choicesif the option value has to be in a definited set, this field represents the set of values.static StringDEFAULT_ARG_NAMEDefault name in the usage message.protected StringdefaultValuethe option default value.protected StringdescriptionThe option description.protected booleanflagwhether or not the option is a flag.protected booleanhelpwhether or not the option is a "help" option.protected booleanhiddenwhether or not the option is hidden.protected StringlongNamethe option long name.protected booleanmultiValuedwhether or not the option can recevie multiple values.static StringNO_NAMEThe default long name / short name of the option.protected booleanrequiredwhether or not the option is required.protected StringshortNamethe option short name.protected booleansingleValuedwhether or not the option receives a single value.
-
Constructor Summary
Constructors Constructor Description Option()Creates a new empty instance ofOption.Option(Option other)Option(JsonObject json)Creates a new instance ofOptionfrom the givenJsonObject
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanacceptValue()OptionaddChoice(String choice)Adds a choice to the list of values accepted by this option.voidensureValidity()Checks whether or not the option is valid.StringgetArgName()Set<String>getChoices()StringgetDefaultValue()StringgetDescription()StringgetLongName()StringgetName()StringgetShortName()booleanisFlag()booleanisHelp()Checks whether or not this option is a "Help" option.booleanisHidden()booleanisMultiValued()booleanisRequired()booleanisSingleValued()OptionsetArgName(String argName)Sets te arg name for this option.OptionsetChoices(Set<String> choices)Sets the list of values accepted by this option.OptionsetDefaultValue(String defaultValue)Sets the default value of this optionOptionsetDescription(String description)Sets te description of this option.OptionsetFlag(boolean flag)Configures the currentOptionto be a flag.OptionsetHelp(boolean help)Sets whether or not this option is a "help" optionOptionsetHidden(boolean hidden)Sets whether or not this option should be hiddenOptionsetLongName(String longName)Sets the long name of this option.OptionsetMultiValued(boolean multiValued)Sets whether or not this option can receive several values.OptionsetRequired(boolean required)Sets whether or not this option is mandatory.OptionsetShortName(String shortName)Sets the short name of this option.OptionsetSingleValued(boolean singleValued)Sets whether or not this option can receive a value.JsonObjecttoJson()Gets the json representation of thisOption.
-
-
-
Field Detail
-
DEFAULT_ARG_NAME
public static final String DEFAULT_ARG_NAME
Default name in the usage message.- See Also:
- Constant Field Values
-
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 aMissingOptionException.
-
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.trueby 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.
-
-
Constructor Detail
-
Option
public Option()
Creates a new empty instance ofOption.
-
Option
public Option(Option other)
- Parameters:
other- the other option
-
Option
public Option(JsonObject json)
Creates a new instance ofOptionfrom the givenJsonObject- Parameters:
json- the json object representing the option- See Also:
toJson()
-
-
Method Detail
-
toJson
public JsonObject toJson()
Gets the json representation of thisOption.- 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 aCLIis valid before starting the parsing. If the configuration is not valid, this method throws aIllegalArgumentException.
-
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
nullfor 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
Optioninstance
-
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
Optioninstance
-
getArgName
public String getArgName()
- Returns:
- the option arg name used in usage messages,
nullif not set.
-
setArgName
public Option setArgName(String argName)
Sets te arg name for this option.- Parameters:
argName- the arg name, must not benull- Returns:
- the current
Optioninstance
-
getDescription
public String getDescription()
- Returns:
- the description of this option,
nullif not set.
-
setDescription
public Option setDescription(String description)
Sets te description of this option.- Parameters:
description- the description- Returns:
- the current
Optioninstance
-
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-trueto make this option hidden, {@link false} otherwise- Returns:
- the current
Optioninstance
-
getLongName
public String getLongName()
- Returns:
- the option long name,
nullif not set.
-
setLongName
public Option setLongName(String longName)
Sets the long name of this option.- Parameters:
longName- the long name- Returns:
- the current
Optioninstance
-
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-trueto make this option mandatory, {@link false} otherwise- Returns:
- the current
Optioninstance
-
getShortName
public String getShortName()
- Returns:
- the short name of this option,
nullif not set.
-
setShortName
public Option setShortName(String shortName)
Sets the short name of this option.- Parameters:
shortName- the short name- Returns:
- the current
Optioninstance
-
getDefaultValue
public String getDefaultValue()
- Returns:
- the default value of this option,
nullif not set.
-
setDefaultValue
public Option setDefaultValue(String defaultValue)
Sets the default value of this option- Parameters:
defaultValue- the default value- Returns:
- the current
Optioninstance
-
isFlag
public boolean isFlag()
- Returns:
- whether or not this option is a flag.
-
setFlag
public Option setFlag(boolean flag)
Configures the currentOptionto be a flag. It will be evaluated totrueif 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:
trueif this option is a "help" option.
-
setHelp
public Option setHelp(boolean help)
Sets whether or not this option is a "help" option- Parameters:
help-trueto 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, aInvalidValueExceptionexception 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, aInvalidValueExceptionexception is thrown.- Parameters:
choice- the choice- Returns:
- the current
Option
-
-