org.geotools.resources
Class Arguments

java.lang.Object
  extended byorg.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 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 17672 2006-01-19 00:25:55Z desruisseaux $
Author:
Martin Desruisseaux

Field Summary
 java.io.PrintWriter err
          Error stream to the console.
 java.util.Locale locale
          The locale.
 java.io.PrintWriter out
          Output stream to the console.
 
Constructor Summary
Arguments(java.lang.String[] args)
          Construct a console.
 
Method Summary
 boolean getFlag(java.lang.String name)
          Returns 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.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.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

out

public final java.io.PrintWriter out
Output stream to the console. This output stream will use encoding specified in the "-encoding" argument, if present. Otherwise, encoding will be fetch from user's preference.


err

public final java.io.PrintWriter err
Error stream to the console. This output stream will use encoding specified in the "-encoding" argument, if present. Otherwise, encoding will be fetch from user's preference.


locale

public final java.util.Locale locale
The locale. Locale will be fetch from the "-locale" argument, if present. Otherwise, the default locale will be used.

Constructor Detail

Arguments

public Arguments(java.lang.String[] args)
Construct a console.

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 , 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 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 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 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 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 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 (unless the same flag appears many times on the command line).

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

getReader

public static java.io.Reader getReader(java.io.InputStream in)
Gets a reader for the specified input stream. If the user specified an encoding in some previous run of Arguments, then this encoding will be used.

Parameters:
in - The input stream to wrap.
Returns:
A Reader wrapping the specified input stream with the user's prefered encoding.

getWriter

public static java.io.Writer getWriter(java.io.OutputStream out)
Gets a writer for the specified output stream. If the user specified an encoding in some previous run of Arguments, then this encoding will be used.

Parameters:
out - The output stream to wrap.
Returns:
A Writer wrapping the specified output stream with the user's prefered encoding.

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 .

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 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 © GeoTools. All Rights Reserved.