public class Commandline
extends java.lang.Object
addOption
to add options to the command
line. Then call parse
to parse a command line. The
retrieve command line paramters with getValues
.Modifier and Type | Field and Description |
---|---|
java.lang.String |
ProgramName |
Constructor and Description |
---|
Commandline(java.lang.String ProgramName,
java.lang.String usage)
creates a
Commandline object which does not allow
non option arguments. |
Commandline(java.lang.String ProgramName,
java.lang.String usage,
java.lang.String restName,
java.lang.String restInfo,
int cmin,
int cmax)
creates a
Commandline object which allows non option
arguments. |
Modifier and Type | Method and Description |
---|---|
Commandline |
addOption(Option option) |
boolean |
available(java.lang.String opt)
returns true if the option was found on the command line or has default
values.
|
long |
getLongValue(java.lang.String opt)
returns the first element of the vector returned by
getValues(String) and casted and converted to long . |
long[] |
getLongValues(java.lang.String opt) |
java.lang.String |
getStringValue(java.lang.String opt)
returns the first element of the vector returned by
getValues(String) and casted to String . |
java.lang.String[] |
getStringValues(java.lang.String opt) |
java.lang.Object |
getValue(java.lang.String opt)
returns the first element of the vector returned by
getValues . |
java.util.List<java.lang.Object> |
getValues(java.lang.String opt)
returns the arguments found on the command line or set as a
default for this option.
|
void |
parse(java.lang.String[] argv)
parses the given command line.
|
void |
showParsed() |
public Commandline(java.lang.String ProgramName, java.lang.String usage, java.lang.String restName, java.lang.String restInfo, int cmin, int cmax)
Commandline
object which allows non option
arguments.ProgramName
- is used as the name of the program in the
usage message generated when unknown options are used. Typically
the string System.getProperty("argv0")
is passed here.usage
- a one-liner summarizing the function of the program.restName
- name used to denote non option arguments in the
usage messagerestInfo
- one liner describing the meaning of non option
argumentscmin
- minimum number of non option arguments requiredcmax
- maximum number of non option arguments allowed. Use
Integer.MAX_VALUE
to define that (almost) any number of
arguments is allowed.public Commandline(java.lang.String ProgramName, java.lang.String usage)
Commandline
object which does not allow
non option arguments. This is a convenience constructor which
calls the 6 parameter constructor with suitable parameters.public Commandline addOption(Option option)
public java.util.List<java.lang.Object> getValues(java.lang.String opt)
returns the arguments found on the command line or set as a
default for this option. May be null
. The elements
of the resulting vector are of type String
or
whatever was passed in as the defalt
parameter of
the constructor. For subclasses of Option
, different
types may be returned. To access any arguments which were not
assigned to an option, call this function with
"--"
.
public java.lang.String[] getStringValues(java.lang.String opt)
public long[] getLongValues(java.lang.String opt)
public java.lang.Object getValue(java.lang.String opt)
getValues
. This is a convenience method normally to
be used only for options with exactly one parameter. If the
option is indeed "optional", a call to available()
should preceed this call.public java.lang.String getStringValue(java.lang.String opt)
returns the first element of the vector returned by
getValues(String)
and casted to String
. Use this
method only if you are sure that the requested value indeed exists (by
definition and/or checking with available(String)
.
public long getLongValue(java.lang.String opt)
returns the first element of the vector returned by
getValues(String)
and casted and converted to long
.
Use this method only if you are sure that the requested value indeed exists
(by definition and/or checking with available(String)
.
public boolean available(java.lang.String opt)
returns true if the option was found on the command line or has default values.
java.lang.NullPointerException
- if opt
does not denote a defined optionpublic void parse(java.lang.String[] argv) throws CommandlineException
parses the given command line. Results are stored internally
and can be retrieved with getValues()
. If an
error occurs during parsing, the message of the resulting CommandlineException
makes a good usage message. A typical
application looks like
try { cmd.parse(argv); } catch( CommandlineException e ) { System.err.println(e.getMessage()); System.exit(1); }
CommandlineException
public void showParsed()