Package org.apache.commons.cli
Class CommandLine
- java.lang.Object
-
- org.apache.commons.cli.CommandLine
-
- All Implemented Interfaces:
java.io.Serializable
public class CommandLine extends java.lang.Object implements java.io.SerializableRepresents list of arguments parsed against aOptionsdescriptor.It allows querying of a boolean
hasOption(String opt), in addition to retrieving thegetOptionValue(String opt)for options requiring arguments.Additionally, any left-over or unrecognized arguments, are available for further processing.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCommandLine.BuilderA nested builder class to createCommandLineinstance using descriptive methods.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<java.lang.String>argsThe unrecognized options/argumentsprivate java.util.List<Option>optionsThe processed optionsprivate static longserialVersionUIDThe serial version UID.
-
Constructor Summary
Constructors Modifier Constructor Description protectedCommandLine()Creates a command line.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidaddArg(java.lang.String arg)Add left-over unrecognized option/argument.protected voidaddOption(Option opt)Add an option to the command line.java.util.List<java.lang.String>getArgList()Retrieve any left-over non-recognized options and argumentsjava.lang.String[]getArgs()Retrieve any left-over non-recognized options and argumentsjava.lang.ObjectgetOptionObject(char opt)Deprecated.due to System.err message.java.lang.ObjectgetOptionObject(java.lang.String opt)Deprecated.due to System.err message.java.util.PropertiesgetOptionProperties(java.lang.String opt)Retrieve the map of values associated to the option.java.util.PropertiesgetOptionProperties(Option option)Retrieve the map of values associated to the option.Option[]getOptions()Gets an array of the processedOptions.java.lang.StringgetOptionValue(char opt)Retrieve the first argument, if any, of this option.java.lang.StringgetOptionValue(char opt, java.lang.String defaultValue)Retrieve the argument, if any, of an option.java.lang.StringgetOptionValue(java.lang.String opt)Retrieve the first argument, if any, of this option.java.lang.StringgetOptionValue(java.lang.String opt, java.lang.String defaultValue)Retrieve the first argument, if any, of an option.java.lang.StringgetOptionValue(Option option)Retrieve the first argument, if any, of this option.java.lang.StringgetOptionValue(Option option, java.lang.String defaultValue)Retrieve the first argument, if any, of an option.java.lang.String[]getOptionValues(char opt)Retrieves the array of values, if any, of an option.java.lang.String[]getOptionValues(java.lang.String opt)Retrieves the array of values, if any, of an option.java.lang.String[]getOptionValues(Option option)Retrieves the array of values, if any, of an option.java.lang.ObjectgetParsedOptionValue(char opt)Return a version of thisOptionconverted to a particular type.java.lang.ObjectgetParsedOptionValue(java.lang.String opt)Return a version of thisOptionconverted to a particular type.java.lang.ObjectgetParsedOptionValue(Option option)Return a version of thisOptionconverted to a particular type.booleanhasOption(char opt)Tests to see if an option has been set.booleanhasOption(java.lang.String opt)Tests to see if an option has been set.booleanhasOption(Option opt)Tests to see if an option has been set.java.util.Iterator<Option>iterator()Returns an iterator over the Option members of CommandLine.private OptionresolveOption(java.lang.String opt)Retrieves the option object given the long or short option as a String
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
The serial version UID.- See Also:
- Constant Field Values
-
args
private final java.util.List<java.lang.String> args
The unrecognized options/arguments
-
options
private final java.util.List<Option> options
The processed options
-
-
Method Detail
-
addArg
protected void addArg(java.lang.String arg)
Add left-over unrecognized option/argument.- Parameters:
arg- the unrecognized option/argument.
-
addOption
protected void addOption(Option opt)
Add an option to the command line. The values of the option are stored.- Parameters:
opt- the processed option.
-
getArgList
public java.util.List<java.lang.String> getArgList()
Retrieve any left-over non-recognized options and arguments- Returns:
- remaining items passed in but not parsed as a
List.
-
getArgs
public java.lang.String[] getArgs()
Retrieve any left-over non-recognized options and arguments- Returns:
- remaining items passed in but not parsed as an array.
-
getOptionObject
@Deprecated public java.lang.Object getOptionObject(char opt)
Deprecated.due to System.err message. Instead use getParsedOptionValue(char)Return theObjecttype of thisOption.- Parameters:
opt- the name of the option.- Returns:
- the type of opt.
-
getOptionObject
@Deprecated public java.lang.Object getOptionObject(java.lang.String opt)
Deprecated.due to System.err message. Instead use getParsedOptionValue(String)Return theObjecttype of thisOption.- Parameters:
opt- the name of the option.- Returns:
- the type of this
Option.
-
getOptionProperties
public java.util.Properties getOptionProperties(Option option)
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:
option- name of the option.- Returns:
- The Properties mapped by the option, never
nulleven if the option doesn't exists. - Since:
- 1.5.0
-
getOptionProperties
public java.util.Properties getOptionProperties(java.lang.String opt)
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
nulleven if the option doesn't exists. - Since:
- 1.2
-
getOptions
public Option[] getOptions()
Gets an array of the processedOptions.- Returns:
- an array of the processed
Options.
-
getOptionValue
public java.lang.String getOptionValue(char opt)
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.
-
getOptionValue
public java.lang.String getOptionValue(char opt, java.lang.String defaultValue)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.
-
getOptionValue
public java.lang.String getOptionValue(Option option)
Retrieve 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
public java.lang.String getOptionValue(Option option, java.lang.String defaultValue)
Retrieve 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
public java.lang.String getOptionValue(java.lang.String opt)
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
public java.lang.String getOptionValue(java.lang.String opt, java.lang.String defaultValue)Retrieve 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
public java.lang.String[] getOptionValues(char opt)
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.
-
getOptionValues
public java.lang.String[] getOptionValues(Option option)
Retrieves 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
public java.lang.String[] getOptionValues(java.lang.String opt)
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.
-
getParsedOptionValue
public java.lang.Object getParsedOptionValue(char opt) throws ParseExceptionReturn a version of thisOptionconverted 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:
PatternOptionBuilder
-
getParsedOptionValue
public java.lang.Object getParsedOptionValue(Option option) throws ParseException
Return a version of thisOptionconverted 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:
PatternOptionBuilder
-
getParsedOptionValue
public java.lang.Object getParsedOptionValue(java.lang.String opt) throws ParseExceptionReturn a version of thisOptionconverted 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:
PatternOptionBuilder
-
hasOption
public boolean hasOption(char opt)
Tests to see if an option has been set.- Parameters:
opt- character name of the option.- Returns:
- true if set, false if not.
-
hasOption
public boolean hasOption(Option opt)
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
public boolean hasOption(java.lang.String opt)
Tests to see if an option has been set.- Parameters:
opt- Short name of the option.- Returns:
- true if set, false if not.
-
iterator
public java.util.Iterator<Option> iterator()
Returns an iterator over the Option members of CommandLine.- Returns:
- an
Iteratorover the processedOptionmembers of thisCommandLine.
-
resolveOption
private Option resolveOption(java.lang.String opt)
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.
-
-