public class Arguments extends Object
main
methods. For example:
Then, method likespublic static void main(String[] args) { Arguments arguments = new Arguments(args); }
getRequiredString(java.lang.String)
or getOptionalString(java.lang.String)
can be used. If a
parameter is badly formatted or if a required parameter is not presents, then the method illegalArgument(java.lang.Exception)
will be invoked with a message that describes the error. The default
implementation print the localized error message to standard output out
and exits the
virtual machine with a call to System.exit(int)
with error code 1.Modifier and Type | Field and Description |
---|---|
PrintWriter |
err
Error stream to the console.
|
static int |
ILLEGAL_ARGUMENT_EXIT_CODE
Deprecated.
Moved to
CommandLine . |
Locale |
locale
The locale.
|
PrintWriter |
out
Output stream to the console.
|
Constructor and Description |
---|
Arguments(String[] args)
Constructs a set of arguments.
|
Modifier and Type | Method and Description |
---|---|
boolean |
getFlag(String name)
Returns
true if the specified flag is set on the command line. |
Boolean |
getOptionalBoolean(String name)
Returns an optional boolean value from the command line.
|
Double |
getOptionalDouble(String name)
Returns an optional floating-point value from the command line.
|
Integer |
getOptionalInteger(String name)
Returns an optional integer value from the command line.
|
String |
getOptionalString(String name)
Returns an optional string value from the command line.
|
static PrintWriter |
getPrintWriter(PrintStream out)
Gets a print writer for the specified print stream.
|
static Reader |
getReader(InputStream in)
Gets a reader for the specified input stream.
|
String[] |
getRemainingArguments(int max)
Returns the list of unprocessed arguments.
|
String[] |
getRemainingArguments(int max,
char forbiddenPrefix)
Returns the list of unprocessed arguments, which should not begin by the specified prefix.
|
boolean |
getRequiredBoolean(String name)
Returns a required boolean value from the command line.
|
double |
getRequiredDouble(String name)
Returns a required floating-point value from the command line.
|
int |
getRequiredInteger(String name)
Returns a required integer value from the command line.
|
String |
getRequiredString(String name)
Returns an required string value from the command line.
|
static Writer |
getWriter(OutputStream out)
Gets a writer for the specified output stream.
|
protected void |
illegalArgument(Exception exception)
Invoked when an the user has specified an illegal parameter.
|
void |
printSummary(Exception exception)
Prints a summary of the specified exception, without stack trace.
|
@Deprecated public static final int ILLEGAL_ARGUMENT_EXIT_CODE
CommandLine
.System.exit(int)
when this class exits because of an illegal user
argument.public final PrintWriter out
"-encoding"
argument, if presents.public final PrintWriter err
public final Locale locale
"-locale"
argument, if presents. Otherwise,
the default locale will be used.public Arguments(String[] args)
args
- Command line arguments. Arguments "-encoding"
and "-locale"
will
be automatically parsed.public String getOptionalString(String name)
null
,
unless the same parameter appears many times on the command line.
Paramater may be instructions like "-encoding cp850" or "-encoding=cp850". Both forms (with or without "=") are accepted. Spaces around the '=' character, if any, are ignored.
name
- The parameter name (e.g. "-encoding"). Name are case-insensitive.null
if there is no parameter given for the specified
name.public String getRequiredString(String name)
getOptionalString(java.lang.String)
, except that it will invokes illegalArgument(java.lang.Exception)
if the specified
parameter was not given on the command line.name
- The parameter name. Name are case-insensitive.public Integer getOptionalInteger(String name)
Integer.parseInt(String)
method, which means that the parsing is locale-insensitive. Locale
insensitive parsing is required in order to use arguments in portable scripts.name
- The parameter name. Name are case-insensitive.null
if there is no parameter given for the specified
name.public int getRequiredInteger(String name)
Integer.parseInt(String)
method, which means that the parsing is locale-insensitive. Locale
insensitive parsing is required in order to use arguments in portable scripts.name
- The parameter name. Name are case-insensitive.public Double getOptionalDouble(String name)
Double.parseDouble(String)
method, which means that the parsing is
locale-insensitive. Locale insensitive parsing is required in order to use arguments in
portable scripts.name
- The parameter name. Name are case-insensitive.null
if there is no parameter given for the specified
name.public double getRequiredDouble(String name)
Double.parseDouble(String)
method, which means that the parsing is
locale-insensitive. Locale insensitive parsing is required in order to use arguments in
portable scripts.name
- The parameter name. Name are case-insensitive.public Boolean getOptionalBoolean(String name)
name
- The parameter name. Name are case-insensitive.null
if there is no parameter given for the specified
name.public boolean getRequiredBoolean(String name)
name
- The parameter name. Name are case-insensitive.public boolean getFlag(String name)
true
if the specified flag is set on the command line. This method should be
called exactly once for each flag. Second invocation for the same flag will returns false
(unless the same flag appears many times on the command line).name
- The flag name.true
if this flag appears on the command line, or false
otherwise.public static Reader getReader(InputStream in)
in
- The input stream to wrap.Reader
wrapping the specified input stream.public static Writer getWriter(OutputStream out)
out
- The output stream to wrap.Writer
wrapping the specified output stream.public static PrintWriter getPrintWriter(PrintStream out)
out
- The print stream to wrap.PrintWriter
wrapping the specified print stream.public String[] getRemainingArguments(int max)
illegalArgument(java.lang.Exception)
.max
- Maximum remaining arguments autorized.max
.public String[] getRemainingArguments(int max, char forbiddenPrefix)
getRemainingArguments(max)
and verifies that none of the remaining arguments start with forbiddenPrefix
.
The forbidden prefix is usually '-'
, the character used for options as in "-locale
", etc.max
- Maximum remaining arguments autorized.forbiddenPrefix
- The forbidden prefix, usually '-'
.max
.public void printSummary(Exception exception)
FileNotFoundException
when the file were specified in argument.exception
- An exception with a message describing the user's error.protected void illegalArgument(Exception exception)
out
, and then exit the
virtual machine. User may override this method if they want a different behavior.
This method is not invoked when an anormal error occured (for example an
unexpected NullPointerException
in some of developper's module). If such an error
occurs, the normal exception mechanism will be used.
exception
- An exception with a message describing the user's error.Copyright © 1996–2019 Geotools. All rights reserved.