org.geotools.resources
Class Arguments

java.lang.Object
  extended by org.geotools.resources.Arguments

public class Arguments
extends java.lang.Object

A helper class for parsing command-line arguments. Instance of this class are usually created inside main methods. For example:

 public static void main(String[] args) {
     Arguments arguments = new Arguments(args);
 }
 
Then, method likes 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
Version:
$Id: Arguments.java 30742 2008-06-16 19:48:45Z desruisseaux $
Author:
Martin Desruisseaux (IRD)

Field Summary
 java.io.PrintWriter err
          Error stream to the console.
static int ILLEGAL_ARGUMENT_EXIT_CODE
          Deprecated. Moved to CommandLine.
 java.util.Locale locale
          The locale.
 java.io.PrintWriter out
          Output stream to the console.
 
Constructor Summary
Arguments(java.lang.String[] args)
          Constructs a set of arguments.
 
Method Summary
 boolean getFlag(java.lang.String name)
          Returns true if the specified flag is set on the command line.
 java.lang.Boolean getOptionalBoolean(java.lang.String name)
          Returns an optional boolean value from the command line.
 java.lang.Double getOptionalDouble(java.lang.String name)
          Returns an optional floating-point value from the command line.
 java.lang.Integer getOptionalInteger(java.lang.String name)
          Returns an optional integer value from the command line.
 java.lang.String getOptionalString(java.lang.String name)
          Returns an optional string value from the command line.
static java.io.PrintWriter getPrintWriter(java.io.PrintStream out)
          Gets a print writer for the specified print stream.
static java.io.Reader getReader(java.io.InputStream in)
          Gets a reader for the specified input stream.
 java.lang.String[] getRemainingArguments(int max)
          Returns the list of unprocessed arguments.
 java.lang.String[] getRemainingArguments(int max, char forbiddenPrefix)
          Returns the list of unprocessed arguments, which should not begin by the specified prefix.
 boolean getRequiredBoolean(java.lang.String name)
          Returns a required boolean value from the command line.
 double getRequiredDouble(java.lang.String name)
          Returns a required floating-point value from the command line.
 int getRequiredInteger(java.lang.String name)
          Returns a required integer value from the command line.
 java.lang.String getRequiredString(java.lang.String name)
          Returns an required string value from the command line.
static java.io.Writer getWriter(java.io.OutputStream out)
          Gets a writer for the specified output stream.
protected  void illegalArgument(java.lang.Exception exception)
          Invoked when an the user has specified an illegal parameter.
 void printSummary(java.lang.Exception exception)
          Prints a summary of the specified exception, without stack trace.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ILLEGAL_ARGUMENT_EXIT_CODE

@Deprecated
public static final int ILLEGAL_ARGUMENT_EXIT_CODE
Deprecated. Moved to CommandLine.
The code given to System.exit(int) when this class exits because of an illegal user argument.

See Also:
Constant Field Values

out

public final java.io.PrintWriter out
Output stream to the console. This output stream may use encoding specified in the "-encoding" argument, if presents.


err

public final java.io.PrintWriter err
Error stream to the console.


locale

public final java.util.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(java.lang.String[] args)
Constructs a set of arguments.

Parameters:
args - Command line arguments. Arguments "-encoding" and "-locale" will be automatically parsed.
Method Detail

getOptionalString

public java.lang.String getOptionalString(java.lang.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 returns 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.

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 java.lang.String getRequiredString(java.lang.String name)
Returns an required string value from the command line. This method works like getOptionalString(java.lang.String), except that it will invokes illegalArgument(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 java.lang.Integer getOptionalInteger(java.lang.String name)
Returns an optional integer value from the command line. Numbers are parsed as of the 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.

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(java.lang.String name)
Returns a required integer value from the command line. Numbers are parsed as of the 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.

Parameters:
name - The parameter name. Name are case-insensitive.
Returns:
The parameter value.

getOptionalDouble

public java.lang.Double getOptionalDouble(java.lang.String name)
Returns an optional floating-point value from the command line. Numbers are parsed as of the 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.

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(java.lang.String name)
Returns a required floating-point value from the command line. Numbers are parsed as of the 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.

Parameters:
name - The parameter name. Name are case-insensitive.
Returns:
The parameter value.

getOptionalBoolean

public java.lang.Boolean getOptionalBoolean(java.lang.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(java.lang.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(java.lang.String name)
Returns 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).

Parameters:
name - The flag name.
Returns:
true if this flag appears on the command line, or false otherwise.

getReader

public static java.io.Reader getReader(java.io.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 java.io.Writer getWriter(java.io.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 java.io.PrintWriter getPrintWriter(java.io.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 java.lang.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 invokes illegalArgument(java.lang.Exception).

Parameters:
max - Maximum remaining arguments autorized.
Returns:
An array of remaining arguments. Will never be longer than max.

getRemainingArguments

public java.lang.String[] getRemainingArguments(int max,
                                                char forbiddenPrefix)
Returns the list of unprocessed arguments, which should not begin by the specified prefix. This method invokes 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.

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(java.lang.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 example FileNotFoundException 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(java.lang.Exception exception)
Invoked when an the user has specified an illegal parameter. The default implementation prints the localized error message to the standard output 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.

Parameters:
exception - An exception with a message describing the user's error.


Copyright © 1996-2010 Geotools. All Rights Reserved.