Package org.geotools.util
Class Arguments
- Object
-
- Arguments
-
public class Arguments extends Object
A helper class for parsing command-line arguments. Instance of this class are usually created insidemain
methods. For example:public static void main(String[] args) { Arguments arguments = new Arguments(args); }
getRequiredString(java.lang.String)
orgetOptionalString(java.lang.String)
can be used. If a parameter is badly formatted or if a required parameter is not presents, then the methodillegalArgument(java.lang.Exception)
will be invoked with a message that describes the error. The default implementation print the localized error message to standard outputout
and exits the virtual machine with a call toSystem.exit(int)
with error code 1.- Since:
- 2.0
- Author:
- Martin Desruisseaux (IRD)
-
-
Field Summary
Fields Modifier and Type Field Description PrintWriter
err
Error stream to the console.Locale
locale
The locale.PrintWriter
out
Output stream to the console.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
getFlag(String name)
Returnstrue
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.
-
-
-
Field Detail
-
out
public final PrintWriter out
Output stream to the console. This output stream may use encoding specified in the"-encoding"
argument, if presents.
-
err
public final PrintWriter err
Error stream to the console.
-
locale
public final Locale locale
The locale. Locale will be fetch from the"-locale"
argument, if presents. Otherwise, the default locale will be used.
-
-
Constructor Detail
-
Arguments
public Arguments(String... args)
Constructs a set of arguments.- Parameters:
args
- Command line arguments. Arguments"-encoding"
and"-locale"
will be automatically parsed.
-
-
Method Detail
-
getOptionalString
public String getOptionalString(String name)
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
public String getRequiredString(String name)
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
public Integer getOptionalInteger(String name)
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
public int getRequiredInteger(String name)
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
public Double getOptionalDouble(String name)
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
public double getRequiredDouble(String name)
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
public Boolean getOptionalBoolean(String name)
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
public boolean getRequiredBoolean(String name)
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
public boolean getFlag(String name)
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
public static Reader getReader(InputStream in)
Gets a reader for the specified input stream.- Parameters:
in
- The input stream to wrap.- Returns:
- A
Reader
wrapping the specified input stream.
-
getWriter
public static Writer getWriter(OutputStream out)
Gets a writer for the specified output stream.- Parameters:
out
- The output stream to wrap.- Returns:
- A
Writer
wrapping the specified output stream.
-
getPrintWriter
public static PrintWriter getPrintWriter(PrintStream out)
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
public String[] getRemainingArguments(int max)
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
public String[] getRemainingArguments(int max, char forbiddenPrefix)
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
public void printSummary(Exception exception)
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
protected void illegalArgument(Exception exception)
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.
-
-