Package org.apache.commons.cli
Class Option
- java.lang.Object
-
- org.apache.commons.cli.Option
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable
public class Option extends java.lang.Object implements java.lang.Cloneable, java.io.Serializable
Describes a single command-line option. It maintains information regarding the short-name of the option, the long-name, if any exists, a flag indicating if an argument is required for this option, and a self-documenting description of the option.An Option is not created independently, but is created through an instance of
Options. An Option is required to have at least a short or a long-name.Note: once an
Optionhas been added to an instance ofOptions, its required flag cannot be changed.- See Also:
Options,CommandLine, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classOption.BuilderBuildsOptioninstances using descriptive methods.
-
Field Summary
Fields Modifier and Type Field Description static intUNINITIALIZEDSpecifies the number of argument values has not been specified.static intUNLIMITED_VALUESSpecifies the number of argument values is infinite.
-
Constructor Summary
Constructors Constructor Description Option(java.lang.String option, boolean hasArg, java.lang.String description)Creates an Option using the specified parameters.Option(java.lang.String option, java.lang.String description)Creates an Option using the specified parameters.Option(java.lang.String option, java.lang.String longOption, boolean hasArg, java.lang.String description)Creates an Option using the specified parameters.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanaddValue(java.lang.String value)Deprecated.Unused.static Option.Builderbuilder()Returns aOption.Builderto create anOptionusing descriptive methods.static Option.Builderbuilder(java.lang.String option)Returns aOption.Builderto create anOptionusing descriptive methods.java.lang.Objectclone()A rather odd clone method - due to incorrect code in 1.0 it is public and in 1.1 rather than throwing a CloneNotSupportedException it throws a RuntimeException so as to maintain backwards compatible at the API level.booleanequals(java.lang.Object obj)java.lang.StringgetArgName()Gets the display name for the argument value.intgetArgs()Gets the number of argument values this Option can take.Converter<?,?>getConverter()Gets the value to type converter.DeprecatedAttributesgetDeprecated()Gets deprecated attributes if any.java.lang.StringgetDescription()Gets the self-documenting description of this Option.intgetId()Gets the id of this Option.java.lang.StringgetKey()Gets the 'unique' Option identifier.java.lang.StringgetLongOpt()Gets the long name of this Option.java.lang.StringgetOpt()Gets the name of this Option.java.lang.StringgetSince()Gets the version when this option was added.java.lang.ObjectgetType()Gets the type of this Option.java.lang.StringgetValue()Gets the specified value of this Option ornullif there is no value.java.lang.StringgetValue(int index)Gets the specified value of this Option ornullif there is no value.java.lang.StringgetValue(java.lang.String defaultValue)Gets the value/first value of this Option or thedefaultValueif there is no value.java.lang.String[]getValues()Gets the values of this Option as a String array or an empty array if there are no values.chargetValueSeparator()Gets the value separator character.java.util.List<java.lang.String>getValuesList()Gets the values of this Option as a List.booleanhasArg()Tests whether this Option requires an argument.booleanhasArgName()Tests whether the display name for the argument value has been set.booleanhasArgs()Tests whether this Option can take many values.inthashCode()booleanhasLongOpt()Tests whether this Option has a long name.booleanhasOptionalArg()Tests whether this Option can have an optional argument.booleanhasValueSeparator()Tests whether this Option has specified a value separator.booleanisDeprecated()Tests whether this Option is deprecated.booleanisRequired()Tests whether this Option is required.voidsetArgName(java.lang.String argName)Sets the display name for the argument value.voidsetArgs(int num)Sets the number of argument values this Option can take.voidsetConverter(Converter<?,?> converter)Sets the value to type converter.voidsetDescription(java.lang.String description)Sets the self-documenting description of this Option.voidsetLongOpt(java.lang.String longOpt)Sets the long name of this Option.voidsetOptionalArg(boolean optionalArg)Sets whether this Option can have an optional argument.voidsetRequired(boolean required)Sets whether this Option is mandatory.voidsetType(java.lang.Class<?> type)Sets the type of this Option.voidsetType(java.lang.Object type)Deprecated.since 1.3, usesetType(Class)instead.voidsetValueSeparator(char valueSeparator)Sets the value separator.java.lang.StringtoString()Creates a String suitable for debugging.
-
-
-
Field Detail
-
UNINITIALIZED
public static final int UNINITIALIZED
Specifies the number of argument values has not been specified.- See Also:
- Constant Field Values
-
UNLIMITED_VALUES
public static final int UNLIMITED_VALUES
Specifies the number of argument values is infinite.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Option
public Option(java.lang.String option, boolean hasArg, java.lang.String description) throws java.lang.IllegalArgumentException
Creates an Option using the specified parameters.- Parameters:
option- short representation of the option.hasArg- specifies whether the Option takes an argument or not.description- describes the function of the option.- Throws:
java.lang.IllegalArgumentException- if there are any non valid Option characters inopt.
-
Option
public Option(java.lang.String option, java.lang.String description) throws java.lang.IllegalArgumentException
Creates an Option using the specified parameters. The option does not take an argument.- Parameters:
option- short representation of the option.description- describes the function of the option.- Throws:
java.lang.IllegalArgumentException- if there are any non valid Option characters inopt.
-
Option
public Option(java.lang.String option, java.lang.String longOption, boolean hasArg, java.lang.String description) throws java.lang.IllegalArgumentException
Creates an Option using the specified parameters.- Parameters:
option- short representation of the option.longOption- the long representation of the option.hasArg- specifies whether the Option takes an argument or not.description- describes the function of the option.- Throws:
java.lang.IllegalArgumentException- if there are any non valid Option characters inopt.
-
-
Method Detail
-
builder
public static Option.Builder builder()
Returns aOption.Builderto create anOptionusing descriptive methods.- Returns:
- a new
Option.Builderinstance. - Since:
- 1.3
-
builder
public static Option.Builder builder(java.lang.String option)
Returns aOption.Builderto create anOptionusing descriptive methods.- Parameters:
option- short representation of the option.- Returns:
- a new
Option.Builderinstance. - Throws:
java.lang.IllegalArgumentException- if there are any non valid Option characters inopt.- Since:
- 1.3
-
addValue
@Deprecated public boolean addValue(java.lang.String value)
Deprecated.Unused.This method is not intended to be used. It was a piece of internal API that was made public in 1.0. It currently throws an UnsupportedOperationException.- Parameters:
value- the value to add.- Returns:
- always throws an
UnsupportedOperationException. - Throws:
java.lang.UnsupportedOperationException- always.
-
clone
public java.lang.Object clone()
A rather odd clone method - due to incorrect code in 1.0 it is public and in 1.1 rather than throwing a CloneNotSupportedException it throws a RuntimeException so as to maintain backwards compatible at the API level. After calling this method, it is very likely you will want to call clearValues().- Overrides:
clonein classjava.lang.Object- Returns:
- a clone of this Option instance.
- Throws:
java.lang.RuntimeException- if aCloneNotSupportedExceptionhas been thrown bysuper.clone().
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
getArgName
public java.lang.String getArgName()
Gets the display name for the argument value.- Returns:
- the display name for the argument value.
-
getArgs
public int getArgs()
Gets the number of argument values this Option can take.A value equal to the constant
UNINITIALIZED(= -1) indicates the number of arguments has not been specified. A value equal to the constantUNLIMITED_VALUES(= -2) indicates that this options takes an unlimited amount of values.- Returns:
- num the number of argument values.
- See Also:
UNINITIALIZED,UNLIMITED_VALUES
-
getConverter
public Converter<?,?> getConverter()
Gets the value to type converter.- Returns:
- the value to type converter.
- Since:
- 1.7.0
-
getDeprecated
public DeprecatedAttributes getDeprecated()
Gets deprecated attributes if any.- Returns:
- boolean deprecated attributes or null.
- Since:
- 1.7.0
-
getDescription
public java.lang.String getDescription()
Gets the self-documenting description of this Option.- Returns:
- The string description of this option.
-
getId
public int getId()
Gets the id of this Option. This is only set when the Option shortOpt is a single character. This is used for switch statements.- Returns:
- the id of this Option.
-
getKey
public java.lang.String getKey()
Gets the 'unique' Option identifier. This is the option value if set or the long value if the options value is not set.- Returns:
- the 'unique' Option identifier.
- Since:
- 1.7.0
-
getLongOpt
public java.lang.String getLongOpt()
Gets the long name of this Option.- Returns:
- Long name of this option, or null, if there is no long name.
-
getOpt
public java.lang.String getOpt()
Gets the name of this Option. It is this String which can be used withCommandLine.hasOption(String opt)andCommandLine.getOptionValue(String opt)to check for existence and argument.- Returns:
- The name of this option.
-
getSince
public java.lang.String getSince()
Gets the version when this option was added.- Returns:
- the version when this option was added, or
nullif not set.
-
getType
public java.lang.Object getType()
Gets the type of this Option.- Returns:
- The type of this option.
-
getValue
public java.lang.String getValue()
Gets the specified value of this Option ornullif there is no value.- Returns:
- the value/first value of this Option or
nullif there is no value.
-
getValue
public java.lang.String getValue(int index) throws java.lang.IndexOutOfBoundsException
Gets the specified value of this Option ornullif there is no value.- Parameters:
index- The index of the value to be returned.- Returns:
- the specified value of this Option or
nullif there is no value. - Throws:
java.lang.IndexOutOfBoundsException- if index is less than 1 or greater than the number of the values for this Option.
-
getValue
public java.lang.String getValue(java.lang.String defaultValue)
Gets the value/first value of this Option or thedefaultValueif there is no value.- Parameters:
defaultValue- The value to be returned if there is no value.- Returns:
- the value/first value of this Option or the
defaultValueif there are no values.
-
getValues
public java.lang.String[] getValues()
Gets the values of this Option as a String array or an empty array if there are no values.- Returns:
- the values of this Option as a String array or an empty array if there are no values.
-
getValueSeparator
public char getValueSeparator()
Gets the value separator character.- Returns:
- the value separator character.
-
getValuesList
public java.util.List<java.lang.String> getValuesList()
Gets the values of this Option as a List. Will return an empty list if there are no values.- Returns:
- the values of this Option as a List or an empty List if there are no values.
-
hasArg
public boolean hasArg()
Tests whether this Option requires an argument.- Returns:
- boolean flag indicating if an argument is required.
-
hasArgName
public boolean hasArgName()
Tests whether the display name for the argument value has been set.- Returns:
- if the display name for the argument value has been set.
-
hasArgs
public boolean hasArgs()
Tests whether this Option can take many values.- Returns:
- boolean flag indicating if multiple values are allowed.
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
hasLongOpt
public boolean hasLongOpt()
Tests whether this Option has a long name.- Returns:
- boolean flag indicating existence of a long name.
-
hasOptionalArg
public boolean hasOptionalArg()
Tests whether this Option can have an optional argument.- Returns:
- whether this Option can have an optional argument.
-
hasValueSeparator
public boolean hasValueSeparator()
Tests whether this Option has specified a value separator.- Returns:
- whether this Option has specified a value separator.
- Since:
- 1.1
-
isDeprecated
public boolean isDeprecated()
Tests whether this Option is deprecated.- Returns:
- boolean flag indicating whether this Option is deprecated.
- Since:
- 1.7.0
-
isRequired
public boolean isRequired()
Tests whether this Option is required.- Returns:
- boolean flag indicating whether this Option is required.
-
setArgName
public void setArgName(java.lang.String argName)
Sets the display name for the argument value.- Parameters:
argName- the display name for the argument value.
-
setArgs
public void setArgs(int num)
Sets the number of argument values this Option can take.- Parameters:
num- the number of argument values.
-
setConverter
public void setConverter(Converter<?,?> converter)
Sets the value to type converter.- Parameters:
converter- The converter to convert the string value to the type.- Since:
- 1.7.0
-
setDescription
public void setDescription(java.lang.String description)
Sets the self-documenting description of this Option.- Parameters:
description- The description of this option.- Since:
- 1.1
-
setLongOpt
public void setLongOpt(java.lang.String longOpt)
Sets the long name of this Option.- Parameters:
longOpt- the long name of this Option.
-
setOptionalArg
public void setOptionalArg(boolean optionalArg)
Sets whether this Option can have an optional argument.- Parameters:
optionalArg- specifies whether the Option can have an optional argument.
-
setRequired
public void setRequired(boolean required)
Sets whether this Option is mandatory.- Parameters:
required- specifies whether this Option is mandatory.
-
setType
public void setType(java.lang.Class<?> type)
Sets the type of this Option.- Parameters:
type- the type of this Option.- Since:
- 1.3
-
setType
@Deprecated public void setType(java.lang.Object type)
Deprecated.since 1.3, usesetType(Class)instead.Sets the type of this Option.Note: this method is kept for binary compatibility and the input type is supposed to be a
Classobject.- Parameters:
type- the type of this Option.
-
setValueSeparator
public void setValueSeparator(char valueSeparator)
Sets the value separator. For example if the argument value was a Java property, the value separator would be '='.- Parameters:
valueSeparator- The value separator.
-
toString
public java.lang.String toString()
Creates a String suitable for debugging.- Overrides:
toStringin classjava.lang.Object- Returns:
- a String suitable for debugging.
-
-