Package org.geotools.util
Class Arguments
Object
Arguments
A helper class for parsing command-line arguments. Instance of this class are usually created inside
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.- Since:
- 2.0
- Author:
- Martin Desruisseaux (IRD)
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal PrintWriter
Error stream to the console.final Locale
The locale.final PrintWriter
Output stream to the console. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
if the specified flag is set on the command line.getOptionalBoolean
(String name) Returns an optional boolean value from the command line.getOptionalDouble
(String name) Returns an optional floating-point value from the command line.getOptionalInteger
(String name) Returns an optional integer value from the command line.getOptionalString
(String name) Returns an optional string value from the command line.static PrintWriter
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.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.
-
Field Details
-
out
Output stream to the console. This output stream may use encoding specified in the"-encoding"
argument, if presents. -
err
Error stream to the console. -
locale
The locale. Locale will be fetch from the"-locale"
argument, if presents. Otherwise, the default locale will be used.
-
-
Constructor Details
-
Arguments
Constructs a set of arguments.- Parameters:
args
- Command line arguments. Arguments"-encoding"
and"-locale"
will be automatically parsed.
-
-
Method Details
-
getOptionalString
Returns an optional string value from the command line. This method should be called exactly once for each parameter. Second invocation for the same parameter will returnsnull
, 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.
- Parameters:
name
- The parameter name (e.g. "-encoding"). Name are case-insensitive.- Returns:
- The parameter value, of
null
if there is no parameter given for the specified name.
-
getRequiredString
Returns an required string value from the command line. This method works likegetOptionalString(java.lang.String)
, except that it will invokesillegalArgument(java.lang.Exception)
if the specified parameter was not given on the command line.- Parameters:
name
- The parameter name. Name are case-insensitive.- Returns:
- The parameter value.
-
getOptionalInteger
Returns an optional integer value from the command line. Numbers are parsed as of theInteger.parseInt(String)
method, which means that the parsing is locale-insensitive. Locale insensitive parsing is required in order to use arguments in portable scripts.- Parameters:
name
- The parameter name. Name are case-insensitive.- Returns:
- The parameter value, of
null
if there is no parameter given for the specified name.
-
getRequiredInteger
Returns a required integer value from the command line. Numbers are parsed as of theInteger.parseInt(String)
method, which means that the parsing is locale-insensitive. Locale insensitive parsing is required in order to use arguments in portable scripts.- Parameters:
name
- The parameter name. Name are case-insensitive.- Returns:
- The parameter value.
-
getOptionalDouble
Returns an optional floating-point value from the command line. Numbers are parsed as of theDouble.parseDouble(String)
method, which means that the parsing is locale-insensitive. Locale insensitive parsing is required in order to use arguments in portable scripts.- Parameters:
name
- The parameter name. Name are case-insensitive.- Returns:
- The parameter value, of
null
if there is no parameter given for the specified name.
-
getRequiredDouble
Returns a required floating-point value from the command line. Numbers are parsed as of theDouble.parseDouble(String)
method, which means that the parsing is locale-insensitive. Locale insensitive parsing is required in order to use arguments in portable scripts.- Parameters:
name
- The parameter name. Name are case-insensitive.- Returns:
- The parameter value.
-
getOptionalBoolean
Returns an optional boolean value from the command line. The value, if defined, must be "true" or "false".- Parameters:
name
- The parameter name. Name are case-insensitive.- Returns:
- The parameter value, of
null
if there is no parameter given for the specified name.
-
getRequiredBoolean
Returns a required boolean value from the command line. The value must be "true" or "false".- Parameters:
name
- The parameter name. Name are case-insensitive.- Returns:
- The parameter value.
-
getFlag
Returnstrue
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 returnsfalse
(unless the same flag appears many times on the command line).- Parameters:
name
- The flag name.- Returns:
true
if this flag appears on the command line, orfalse
otherwise.
-
getReader
Gets a reader for the specified input stream.- Parameters:
in
- The input stream to wrap.- Returns:
- A
Reader
wrapping the specified input stream.
-
getWriter
Gets a writer for the specified output stream.- Parameters:
out
- The output stream to wrap.- Returns:
- A
Writer
wrapping the specified output stream.
-
getPrintWriter
Gets a print writer for the specified print stream.- Parameters:
out
- The print stream to wrap.- Returns:
- A
PrintWriter
wrapping the specified print stream.
-
getRemainingArguments
Returns the list of unprocessed arguments. If the number of remaining arguments is greater than the specified maximum, then this method invokesillegalArgument(java.lang.Exception)
.- Parameters:
max
- Maximum remaining arguments autorized.- Returns:
- An array of remaining arguments. Will never be longer than
max
.
-
getRemainingArguments
Returns the list of unprocessed arguments, which should not begin by the specified prefix. This method invokesgetRemainingArguments(max)
and verifies that none of the remaining arguments start withforbiddenPrefix
. The forbidden prefix is usually'-'
, the character used for options as in "-locale
", etc.- Parameters:
max
- Maximum remaining arguments autorized.forbiddenPrefix
- The forbidden prefix, usually'-'
.- Returns:
- An array of remaining arguments. Will never be longer than
max
. - Since:
- 2.4
-
printSummary
Prints a summary of the specified exception, without stack trace. This method is invoked when a non-fatal (and somewhat expected) error occured, for exampleFileNotFoundException
when the file were specified in argument.- Parameters:
exception
- An exception with a message describing the user's error.- Since:
- 2.3
-
illegalArgument
Invoked when an the user has specified an illegal parameter. The default implementation prints the localized error message to the standard outputout
, 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.- Parameters:
exception
- An exception with a message describing the user's error.
-