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.
- Version:
- $Id: CommandLine.java 1786144 2017-03-09 11:34:57Z britter $
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
A nested builder class to createCommandLine
instance using descriptive methods. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Add left-over unrecognized option/argument.protected void
Add an option to the command line.Retrieve any left-over non-recognized options and argumentsString[]
getArgs()
Retrieve any left-over non-recognized options and argumentsgetOptionObject
(char opt) Return theObject
type of thisOption
.getOptionObject
(String opt) Deprecated.due to System.err message.Retrieve the map of values associated to the option.Option[]
Returns an array of the processedOption
s.getOptionValue
(char opt) Retrieve the first argument, if any, of this option.getOptionValue
(char opt, String defaultValue) Retrieve the argument, if any, of an option.getOptionValue
(String opt) Retrieve the first argument, if any, of this option.getOptionValue
(String opt, String defaultValue) Retrieve the first argument, if any, of an option.String[]
getOptionValues
(char opt) Retrieves the array of values, if any, of an option.String[]
getOptionValues
(String opt) Retrieves the array of values, if any, of an option.Return a version of thisOption
converted to a particular type.boolean
hasOption
(char opt) Query to see if an option has been set.boolean
Query to see if an option has been set.iterator()
Returns an iterator over the Option members of CommandLine.private Option
resolveOption
(String opt) Retrieves the option object given the long or short option as a String
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDThe serial version UID.- See Also:
-
args
the unrecognized options/arguments -
options
the processed options
-
-
Constructor Details
-
CommandLine
protected CommandLine()Creates a command line.
-
-
Method Details
-
hasOption
Query to see if an option has been set.- Parameters:
opt
- Short name of the option- Returns:
- true if set, false if not
-
hasOption
public boolean hasOption(char opt) Query to see if an option has been set.- Parameters:
opt
- character name of the option- Returns:
- true if set, false if not
-
getOptionObject
Deprecated.due to System.err message. Instead use getParsedOptionValue(String)Return theObject
type of thisOption
.- Parameters:
opt
- the name of the option- Returns:
- the type of this
Option
-
getParsedOptionValue
Return 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:
-
getOptionObject
Return theObject
type of thisOption
.- Parameters:
opt
- the name of the option- Returns:
- the type of opt
-
getOptionValue
Retrieve 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
Retrieve 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.
-
getOptionValues
Retrieves 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.
-
resolveOption
Retrieves the option object given the long or short option as a String- Parameters:
opt
- short or long name of the option- Returns:
- Canonicalized option
-
getOptionValues
Retrieves 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.
-
getOptionValue
Retrieve the first argument, if any, of an option.- Parameters:
opt
- 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
Retrieve 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
.
-
getOptionProperties
Retrieve 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
-
getArgs
Retrieve any left-over non-recognized options and arguments- Returns:
- remaining items passed in but not parsed as an array
-
getArgList
Retrieve any left-over non-recognized options and arguments- Returns:
- remaining items passed in but not parsed as a
List
.
-
addArg
Add left-over unrecognized option/argument.- Parameters:
arg
- the unrecognized option/argument.
-
addOption
Add an option to the command line. The values of the option are stored.- Parameters:
opt
- the processed option
-
iterator
Returns an iterator over the Option members of CommandLine.- Returns:
- an
Iterator
over the processedOption
members of thisCommandLine
-
getOptions
Returns an array of the processedOption
s.- Returns:
- an array of the processed
Option
s.
-