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 opt)
, in addition to retrieving the
getOptionValue(String opt)
for options requiring arguments.
Additionally, any left-over or unrecognized arguments, are available for further processing.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
A nested builder class to createCommandLine
instance using descriptive methods. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
Adds left-over unrecognized option/argument.protected void
Adds an option to the command line.Gets any left-over non-recognized options and argumentsString[]
getArgs()
Gets any left-over non-recognized options and argumentsgetOptionObject
(char opt) Deprecated.due to System.err message.getOptionObject
(String opt) Deprecated.due to System.err message.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 opt) Gets the first argument, if any, of this option.getOptionValue
(char opt, String defaultValue) Gets the argument, if any, of an option.getOptionValue
(String opt) Gets the first argument, if any, of this option.getOptionValue
(String opt, String defaultValue) Gets the first argument, if any, of an option.getOptionValue
(Option option) Gets the first argument, if any, of this option.getOptionValue
(Option option, String defaultValue) Gets the first argument, if any, of an option.String[]
getOptionValues
(char opt) Gets the array of values, if any, of an option.String[]
getOptionValues
(String opt) Gets the array of values, if any, of an option.String[]
getOptionValues
(Option option) Gets the array of values, if any, of an option.getParsedOptionValue
(char opt) Gets a version of thisOption
converted to a particular type.Gets a version of thisOption
converted to a particular type.getParsedOptionValue
(Option option) Gets a version of thisOption
converted to a particular type.boolean
hasOption
(char opt) 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.iterator()
Returns an iterator over the Option members of CommandLine.
-
Constructor Details
-
CommandLine
protected CommandLine()Creates a command line.
-
-
Method Details
-
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:
opt
- 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:
opt
- 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:
opt
- 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
. 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:
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:
opt
- 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:
opt
- 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:
opt
- 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 first argument, if any, of this option.- Parameters:
option
- the name of 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
- 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
. - Since:
- 1.5.0
-
getOptionValue
Gets the first argument, if any, of this option.- Parameters:
opt
- 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:
opt
- 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
.
-
getOptionValues
Gets the array of values, if any, of an option.- Parameters:
opt
- 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
- string name of 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.- Parameters:
opt
- 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.- Parameters:
opt
- the name of the option.- Returns:
- the value parsed into a particular object.
- 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.- Parameters:
option
- the name of the option.- Returns:
- the value parsed into a particular object.
- 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.- Parameters:
opt
- the name of the option.- Returns:
- the value parsed into a particular object.
- Throws:
ParseException
- if there are problems turning the option value into the desired type- Since:
- 1.2
- See Also:
-
hasOption
Tests to see if an option has been set.- Parameters:
opt
- character name of the option.- Returns:
- true if set, false if not.
-
hasOption
Tests to see if an option has been set.- Parameters:
opt
- 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:
opt
- 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
.
-