Package org.apache.commons.cli
Class CommandLine
java.lang.Object
org.apache.commons.cli.CommandLine
- All Implemented Interfaces:
Serializable
Represents list of arguments parsed against a
Options
descriptor.
It allows querying of a boolean hasOption(String optionName)
, in addition to retrieving the
getOptionValue(String optionName)
for options requiring arguments.
Additionally, any left-over or unrecognized arguments, are available for further processing.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
A nested builder class to createCommandLine
instance using descriptive methods. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Adds left-over unrecognized option/argument.protected void
Adds an option to the command line.static CommandLine.Builder
builder()
Creates a new builder.Gets any left-over non-recognized options and argumentsString[]
getArgs()
Gets any left-over non-recognized options and argumentsgetOptionObject
(char optionChar) Deprecated.due to System.err message.getOptionObject
(String optionName) Deprecated.due to System.err message.getOptionProperties
(String optionName) Gets the map of values associated to the option.getOptionProperties
(Option option) Gets the map of values associated to the option.Option[]
Gets an array of the processedOption
s.getOptionValue
(char optionChar) Gets the first argument, if any, of this option.getOptionValue
(char optionChar, String defaultValue) Gets the argument, if any, of an option.getOptionValue
(char optionChar, Supplier<String> defaultValue) Gets the argument, if any, of an option.getOptionValue
(String optionName) Gets the first argument, if any, of this option.getOptionValue
(String optionName, String defaultValue) Gets the first argument, if any, of an option.getOptionValue
(String optionName, Supplier<String> defaultValue) Gets the first argument, if any, of an option.getOptionValue
(Option option) Gets the first argument, if any, of this option.getOptionValue
(OptionGroup optionGroup) Gets the first argument, if any, of this option group.getOptionValue
(OptionGroup optionGroup, String defaultValue) Gets the first argument, if any, of an option group.getOptionValue
(OptionGroup optionGroup, Supplier<String> defaultValue) Gets the first argument, if any, of an option group.getOptionValue
(Option option, String defaultValue) Gets the first argument, if any, of an option.getOptionValue
(Option option, Supplier<String> defaultValue) Gets the first argument, if any, of an option.String[]
getOptionValues
(char optionChar) Gets the array of values, if any, of an option.String[]
getOptionValues
(String optionName) Gets the array of values, if any, of an option.String[]
getOptionValues
(Option option) Gets the array of values, if any, of an option.String[]
getOptionValues
(OptionGroup optionGroup) Gets the array of values, if any, of an option group.<T> T
getParsedOptionValue
(char optionChar) Gets a version of thisOption
converted to a particular type.<T> T
getParsedOptionValue
(char optionChar, Supplier<T> defaultValue) Gets a version of thisOption
converted to a particular type.<T> T
getParsedOptionValue
(char optionChar, T defaultValue) Gets a version of thisOption
converted to a particular type.<T> T
getParsedOptionValue
(String optionName) Gets a version of thisOption
converted to a particular type.<T> T
getParsedOptionValue
(String optionName, Supplier<T> defaultValue) Gets a version of thisOption
converted to a particular type.<T> T
getParsedOptionValue
(String optionName, T defaultValue) Gets a version of thisOption
converted to a particular type.<T> T
getParsedOptionValue
(Option option) Gets a version of thisOption
converted to a particular type.<T> T
getParsedOptionValue
(OptionGroup optionGroup) Gets a version of thisOptionGroup
converted to a particular type.<T> T
getParsedOptionValue
(OptionGroup optionGroup, Supplier<T> defaultValue) Gets a version of thisOptionGroup
converted to a particular type.<T> T
getParsedOptionValue
(OptionGroup optionGroup, T defaultValue) Gets a version of thisOptionGroup
converted to a particular type.<T> T
getParsedOptionValue
(Option option, Supplier<T> defaultValue) Gets a version of thisOption
converted to a particular type.<T> T
getParsedOptionValue
(Option option, T defaultValue) Gets a version of thisOption
converted to a particular type.<T> T[]
getParsedOptionValues
(char optionChar) Gets a version of thisOption
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(char optionChar, Supplier<T[]> defaultValue) Gets a version of thisOption
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(char optionChar, T[] defaultValue) Gets a version of thisOption
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(String optionName) Gets a version of thisOption
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(String optionName, Supplier<T[]> defaultValue) Gets a version of thisOption
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(String optionName, T[] defaultValue) Gets a version of thisOption
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(Option option) Gets a version of thisOption
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(OptionGroup optionGroup) Gets a version of thisOptionGroup
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(OptionGroup optionGroup, Supplier<T[]> defaultValue) Gets a version of thisOptionGroup
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(OptionGroup optionGroup, T[] defaultValue) Gets a version of thisOptionGroup
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(Option option, Supplier<T[]> defaultValue) Gets a version of thisOption
converted to an array of a particular type.<T> T[]
getParsedOptionValues
(Option option, T[] defaultValue) Gets a version of thisOption
converted to an array of a particular type.boolean
hasOption
(char optionChar) Tests to see if an option has been set.boolean
Tests to see if an option has been set.boolean
Tests to see if an option has been set.boolean
hasOption
(OptionGroup optionGroup) Tests to see if an option has been set.iterator()
Returns an iterator over the Option members of CommandLine.
-
Constructor Details
-
CommandLine
protected CommandLine()Creates a command line.
-
-
Method Details
-
builder
Creates a new builder.- Returns:
- a new builder.
- Since:
- 1.7.0
-
addArg
Adds left-over unrecognized option/argument.- Parameters:
arg
- the unrecognized option/argument.
-
addOption
Adds an option to the command line. The values of the option are stored.- Parameters:
option
- the processed option.
-
getArgList
Gets any left-over non-recognized options and arguments- Returns:
- remaining items passed in but not parsed as a
List
.
-
getArgs
Gets any left-over non-recognized options and arguments- Returns:
- remaining items passed in but not parsed as an array.
-
getOptionObject
Deprecated.due to System.err message. Instead use getParsedOptionValue(char)Gets theObject
type of thisOption
.- Parameters:
optionChar
- the name of the option.- Returns:
- the type of opt.
-
getOptionObject
Deprecated.due to System.err message. Instead use getParsedOptionValue(String)Gets theObject
type of thisOption
.- Parameters:
optionName
- the name of the option.- Returns:
- the type of this
Option
.
-
getOptionProperties
Gets the map of values associated to the option. This is convenient for options specifying Java properties like-Dparam1=value1 -Dparam2=value2
. All odd numbered values are property keys and even numbered values are property values. If there are an odd number of values the last value is assumed to be a boolean flag and the value is "true".- Parameters:
option
- name of the option.- Returns:
- The Properties mapped by the option, never
null
even if the option doesn't exists. - Since:
- 1.5.0
-
getOptionProperties
Gets the map of values associated to the option. This is convenient for options specifying Java properties like-Dparam1=value1 -Dparam2=value2
. The first argument of the option is the key, and the 2nd argument is the value. If the option has only one argument (-Dfoo
) it is considered as a boolean flag and the value is"true"
.- Parameters:
optionName
- name of the option.- Returns:
- The Properties mapped by the option, never
null
even if the option doesn't exists. - Since:
- 1.2
-
getOptions
Gets an array of the processedOption
s.- Returns:
- an array of the processed
Option
s.
-
getOptionValue
Gets the first argument, if any, of this option.- Parameters:
optionChar
- the character name of the option.- Returns:
- Value of the argument if option is set, and has an argument, otherwise null.
-
getOptionValue
Gets the argument, if any, of an option.- Parameters:
optionChar
- character name of the optiondefaultValue
- is the default value to be returned if the option is not specified.- Returns:
- Value of the argument if option is set, and has an argument, otherwise
defaultValue
.
-
getOptionValue
Gets the argument, if any, of an option.- Parameters:
optionChar
- character name of the optiondefaultValue
- is a supplier for the default value to be returned if the option is not specified.- Returns:
- Value of the argument if option is set, and has an argument, otherwise
defaultValue
. - Since:
- 1.7.0
-
getOptionValue
Gets the first argument, if any, of this option.- Parameters:
option
- the option.- Returns:
- Value of the argument if option is set, and has an argument, otherwise null.
- Since:
- 1.5.0
-
getOptionValue
Gets the first argument, if any, of an option.- Parameters:
option
- the option.defaultValue
- is the default value to be returned if the option is not specified.- Returns:
- Value of the argument if option is set, and has an argument, otherwise
defaultValue
. - Since:
- 1.5.0
-
getOptionValue
Gets the first argument, if any, of an option.- Parameters:
option
- the option.defaultValue
- is a supplier for the default value to be returned if the option is not specified.- Returns:
- Value of the argument if option is set, and has an argument, otherwise
defaultValue
. - Since:
- 1.7.0
-
getOptionValue
Gets the first argument, if any, of this option group.- Parameters:
optionGroup
- the option group.- Returns:
- Value of the argument if option group is selected, and has an argument, otherwise null.
- Since:
- 1.9.0
-
getOptionValue
Gets the first argument, if any, of an option group.- Parameters:
optionGroup
- the option group.defaultValue
- is the default value to be returned if the option group is not selected.- Returns:
- Value of the argument if option group is selected, and has an argument, otherwise
defaultValue
. - Since:
- 1.9.0
-
getOptionValue
Gets the first argument, if any, of an option group.- Parameters:
optionGroup
- the option group.defaultValue
- is a supplier for the default value to be returned if the option group is not selected.- Returns:
- Value of the argument if option group is selected, and has an argument, otherwise
defaultValue
. - Since:
- 1.9.0
-
getOptionValue
Gets the first argument, if any, of this option.- Parameters:
optionName
- the name of the option.- Returns:
- Value of the argument if option is set, and has an argument, otherwise null.
-
getOptionValue
Gets the first argument, if any, of an option.- Parameters:
optionName
- name of the option.defaultValue
- is the default value to be returned if the option is not specified.- Returns:
- Value of the argument if option is set, and has an argument, otherwise
defaultValue
.
-
getOptionValue
Gets the first argument, if any, of an option.- Parameters:
optionName
- name of the option.defaultValue
- is a supplier for the default value to be returned if the option is not specified.- Returns:
- Value of the argument if option is set, and has an argument, otherwise
defaultValue
. - Since:
- 1.7.0
-
getOptionValues
Gets the array of values, if any, of an option.- Parameters:
optionChar
- character name of the option.- Returns:
- Values of the argument if option is set, and has an argument, otherwise null.
-
getOptionValues
Gets the array of values, if any, of an option.- Parameters:
option
- the option.- Returns:
- Values of the argument if option is set, and has an argument, otherwise null.
- Since:
- 1.5.0
-
getOptionValues
Gets the array of values, if any, of an option group.- Parameters:
optionGroup
- the option group.- Returns:
- Values of the argument if option group is selected, and has an argument, otherwise null.
- Since:
- 1.9.0
-
getOptionValues
Gets the array of values, if any, of an option.- Parameters:
optionName
- string name of the option.- Returns:
- Values of the argument if option is set, and has an argument, otherwise null.
-
getParsedOptionValue
Gets a version of thisOption
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
optionChar
- the name of the option.- Returns:
- the value parsed into a particular object or null if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.5.0
- See Also:
-
getParsedOptionValue
Gets a version of thisOption
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
optionChar
- the name of the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the value parsed into a particular object or the defaultValue if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.7.0
- See Also:
-
getParsedOptionValue
Gets a version of thisOption
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
optionChar
- the name of the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the value parsed into a particular object or the defaultValue if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.7.0
- See Also:
-
getParsedOptionValue
Gets a version of thisOption
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
option
- the option.- Returns:
- the value parsed into a particular object or null if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.5.0
- See Also:
-
getParsedOptionValue
Gets a version of thisOption
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
option
- the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the value parsed into a particular object or the defaultValue if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.7.0
- See Also:
-
getParsedOptionValue
Gets a version of thisOption
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
option
- the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the value parsed into a particular object or the defaultValue if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.7.0
- See Also:
-
getParsedOptionValue
Gets a version of thisOptionGroup
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
optionGroup
- the option group.- Returns:
- the value parsed into a particular object or null if no option in the OptionGroup is set.
- Throws:
ParseException
- if there are problems turning the selected option value into the desired type- Since:
- 1.9.0
- See Also:
-
getParsedOptionValue
public <T> T getParsedOptionValue(OptionGroup optionGroup, Supplier<T> defaultValue) throws ParseException Gets a version of thisOptionGroup
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
optionGroup
- the option group.defaultValue
- the default value to return if opt is not set.- Returns:
- the value parsed into a particular object or the defaultValue if no option in the OptionGroup is set.
- Throws:
ParseException
- if there are problems turning the selected option value into the desired type- Since:
- 1.9.0
- See Also:
-
getParsedOptionValue
Gets a version of thisOptionGroup
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
optionGroup
- the option group.defaultValue
- the default value to return if an option is not selected.- Returns:
- the value parsed into a particular object or the defaultValue if no option in the OptionGroup is set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.9.0
- See Also:
-
getParsedOptionValue
Gets a version of thisOption
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
optionName
- the name of the option.- Returns:
- the value parsed into a particular object or null if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.2
- See Also:
-
getParsedOptionValue
public <T> T getParsedOptionValue(String optionName, Supplier<T> defaultValue) throws ParseException Gets a version of thisOption
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
optionName
- the name of the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the value parsed into a particular object or the defaultValue if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.7.0
- See Also:
-
getParsedOptionValue
Gets a version of thisOption
converted to a particular type.- Type Parameters:
T
- The return type for the method.- Parameters:
optionName
- the name of the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the value parsed into a particular object or the defaultValue if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.7.0
- See Also:
-
getParsedOptionValues
Gets a version of thisOption
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
optionChar
- the name of the option.- Returns:
- the values parsed into an array of objects or null if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
public <T> T[] getParsedOptionValues(char optionChar, Supplier<T[]> defaultValue) throws ParseException Gets a version of thisOption
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
optionChar
- the name of the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the values parsed into an array of objects or the defaultValue if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
Gets a version of thisOption
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
optionChar
- the name of the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the values parsed into an array of objects or the defaultValue if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
Gets a version of thisOption
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
option
- the option.- Returns:
- the values parsed into an array of objects or null if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
public <T> T[] getParsedOptionValues(Option option, Supplier<T[]> defaultValue) throws ParseException Gets a version of thisOption
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
option
- the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the values parsed into an array of objects or the defaultValue if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
Gets a version of thisOption
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
option
- the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the values parsed into an array of objects or the defaultValue if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
Gets a version of thisOptionGroup
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
optionGroup
- the option group.- Returns:
- the values parsed into an array of objects or null if no option in the OptionGroup is set.
- Throws:
ParseException
- if there are problems turning the selected option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
public <T> T[] getParsedOptionValues(OptionGroup optionGroup, Supplier<T[]> defaultValue) throws ParseException Gets a version of thisOptionGroup
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
optionGroup
- the option group.defaultValue
- the default value to return if opt is not set.- Returns:
- the values parsed into an array of objects or null if no option in the OptionGroup is set.
- Throws:
ParseException
- if there are problems turning the selected option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
public <T> T[] getParsedOptionValues(OptionGroup optionGroup, T[] defaultValue) throws ParseException Gets a version of thisOptionGroup
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
optionGroup
- the option group.defaultValue
- the default value to return if an option is not selected.- Returns:
- the values parsed into an array of objects or null if no option in the OptionGroup is set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
Gets a version of thisOption
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
optionName
- the name of the option.- Returns:
- the values parsed into an array of objects or null if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
public <T> T[] getParsedOptionValues(String optionName, Supplier<T[]> defaultValue) throws ParseException Gets a version of thisOption
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
optionName
- the name of the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the values parsed into an array of objects or defaultValues if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.10.0
- See Also:
-
getParsedOptionValues
Gets a version of thisOption
converted to an array of a particular type.- Type Parameters:
T
- The array type for the return value.- Parameters:
optionName
- the name of the option.defaultValue
- the default value to return if opt is not set.- Returns:
- the values parsed into an array of objects or defaultValues if the option is not set.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.10.0
- See Also:
-
hasOption
Tests to see if an option has been set.- Parameters:
optionChar
- character name of the option.- Returns:
- true if set, false if not.
-
hasOption
Tests to see if an option has been set.- Parameters:
option
- the option to check.- Returns:
- true if set, false if not.
- Since:
- 1.5.0
-
hasOption
Tests to see if an option has been set.- Parameters:
optionGroup
- the option group to check.- Returns:
- true if set, false if not.
- Since:
- 1.9.0
-
hasOption
Tests to see if an option has been set.- Parameters:
optionName
- Short name of the option.- Returns:
- true if set, false if not.
-
iterator
Returns an iterator over the Option members of CommandLine.- Returns:
- an
Iterator
over the processedOption
members of thisCommandLine
.
-