public class Option extends Object implements Cloneable, Serializable
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 Option has been added to an instance of Options, its required flag cannot be changed.
Options,
CommandLine,
Serialized Form| Modifier and Type | Class | Description |
|---|---|---|
static class |
Option.Builder |
Builds
Option instances using descriptive methods. |
| Modifier and Type | Field | Description |
|---|---|---|
static int |
UNINITIALIZED |
Specifies the number of argument values has not been specified.
|
static int |
UNLIMITED_VALUES |
Specifies the number of argument values is infinite.
|
| Constructor | Description |
|---|---|
Option(String option,
boolean hasArg,
String description) |
Creates an Option using the specified parameters.
|
Option(String option,
String description) |
Creates an Option using the specified parameters.
|
Option(String option,
String longOption,
boolean hasArg,
String description) |
Creates an Option using the specified parameters.
|
| Modifier and Type | Method | Description |
|---|---|---|
boolean |
addValue(String value) |
Deprecated.
Unused.
|
static Option.Builder |
builder() |
Returns a
Option.Builder to create an Option using descriptive methods. |
static Option.Builder |
builder(String option) |
Returns a
Option.Builder to create an Option using descriptive methods. |
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.
|
boolean |
equals(Object obj) |
|
String |
getArgName() |
Gets the display name for the argument value.
|
int |
getArgs() |
Gets the number of argument values this Option can take.
|
Converter<?,?> |
getConverter() |
Gets the value to type converter.
|
DeprecatedAttributes |
getDeprecated() |
Gets deprecated attributes if any.
|
String |
getDescription() |
Gets the self-documenting description of this Option.
|
int |
getId() |
Gets the id of this Option.
|
String |
getKey() |
Gets the 'unique' Option identifier.
|
String |
getLongOpt() |
Gets the long name of this Option.
|
String |
getOpt() |
Gets the name of this Option.
|
String |
getSince() |
Gets the version when this option was added.
|
Object |
getType() |
Gets the type of this Option.
|
String |
getValue() |
Gets the specified value of this Option or
null if there is no value. |
String |
getValue(int index) |
Gets the specified value of this Option or
null if there is no value. |
String |
getValue(String defaultValue) |
Gets the value/first value of this Option or the
defaultValue if there is no value. |
String[] |
getValues() |
Gets the values of this Option as a String array or an empty array if there are no values.
|
char |
getValueSeparator() |
Gets the value separator character.
|
List<String> |
getValuesList() |
Gets the values of this Option as a List.
|
boolean |
hasArg() |
Tests whether this Option requires an argument.
|
boolean |
hasArgName() |
Tests whether the display name for the argument value has been set.
|
boolean |
hasArgs() |
Tests whether this Option can take many values.
|
int |
hashCode() |
|
boolean |
hasLongOpt() |
Tests whether this Option has a long name.
|
boolean |
hasOptionalArg() |
Tests whether this Option can have an optional argument.
|
boolean |
hasValueSeparator() |
Tests whether this Option has specified a value separator.
|
boolean |
isDeprecated() |
Tests whether this Option is deprecated.
|
boolean |
isRequired() |
Tests whether this Option is required.
|
void |
setArgName(String argName) |
Sets the display name for the argument value.
|
void |
setArgs(int num) |
Sets the number of argument values this Option can take.
|
void |
setConverter(Converter<?,?> converter) |
Sets the value to type converter.
|
void |
setDescription(String description) |
Sets the self-documenting description of this Option.
|
void |
setLongOpt(String longOpt) |
Sets the long name of this Option.
|
void |
setOptionalArg(boolean optionalArg) |
Sets whether this Option can have an optional argument.
|
void |
setRequired(boolean required) |
Sets whether this Option is mandatory.
|
void |
setType(Class<?> type) |
Sets the type of this Option.
|
void |
setType(Object type) |
Deprecated.
Since 1.3, use
setType(Class) instead. |
void |
setValueSeparator(char valueSeparator) |
Sets the value separator.
|
String |
toString() |
Creates a String suitable for debugging.
|
public static final int UNINITIALIZED
public static final int UNLIMITED_VALUES
public Option(String option, boolean hasArg, String description) throws IllegalArgumentException
option - short representation of the option.hasArg - specifies whether the Option takes an argument or not.description - describes the function of the option.IllegalArgumentException - if there are any non valid Option characters in opt.public Option(String option, String description) throws IllegalArgumentException
option - short representation of the option.description - describes the function of the option.IllegalArgumentException - if there are any non valid Option characters in opt.public Option(String option, String longOption, boolean hasArg, String description) throws IllegalArgumentException
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.IllegalArgumentException - if there are any non valid Option characters in opt.public static Option.Builder builder()
Option.Builder to create an Option using descriptive methods.Option.Builder instance.public static Option.Builder builder(String option)
Option.Builder to create an Option using descriptive methods.option - short representation of the option.Option.Builder instance.IllegalArgumentException - if there are any non valid Option characters in opt.@Deprecated public boolean addValue(String value)
value - the value to add.UnsupportedOperationException.UnsupportedOperationException - always.public Object clone()
clone in class ObjectRuntimeException - if a CloneNotSupportedException has been thrown by super.clone().public String getArgName()
public int getArgs()
A value equal to the constant UNINITIALIZED (= -1) indicates the number of arguments has not been specified. A value equal to the constant
UNLIMITED_VALUES (= -2) indicates that this options takes an unlimited amount of values.
UNINITIALIZED,
UNLIMITED_VALUESpublic Converter<?,?> getConverter()
public DeprecatedAttributes getDeprecated()
public String getDescription()
public int getId()
public String getKey()
public String getLongOpt()
public String getOpt()
CommandLine.hasOption(String opt) and CommandLine.getOptionValue(String opt) to check for
existence and argument.public String getSince()
null if not set.public String getValue()
null if there is no value.null if there is no value.public String getValue(int index) throws IndexOutOfBoundsException
null if there is no value.index - The index of the value to be returned.null if there is no value.IndexOutOfBoundsException - if index is less than 1 or greater than the number of the values for this Option.public String getValue(String defaultValue)
defaultValue if there is no value.defaultValue - The value to be returned if there is no value.defaultValue if there are no values.public String[] getValues()
public char getValueSeparator()
public List<String> getValuesList()
public boolean hasArg()
public boolean hasArgName()
public boolean hasArgs()
public boolean hasLongOpt()
public boolean hasOptionalArg()
public boolean hasValueSeparator()
public boolean isDeprecated()
public boolean isRequired()
public void setArgName(String argName)
argName - the display name for the argument value.public void setArgs(int num)
num - the number of argument values.public void setConverter(Converter<?,?> converter)
converter - The converter to convert the string value to the type.public void setDescription(String description)
description - The description of this option.public void setLongOpt(String longOpt)
longOpt - the long name of this Option.public void setOptionalArg(boolean optionalArg)
optionalArg - specifies whether the Option can have an optional argument.public void setRequired(boolean required)
required - specifies whether this Option is mandatory.public void setType(Class<?> type)
type - the type of this Option.@Deprecated public void setType(Object type)
setType(Class) instead.
Note: this method is kept for binary compatibility and the input type is supposed to be a Class object.
type - the type of this Option.public void setValueSeparator(char valueSeparator)
valueSeparator - The value separator.Copyright © 2002-2025 The Apache Software Foundation. All rights reserved.Apache Commons CLI | Issue management | Source repository