org.geotoolkit.console
Class CommandLine

Object
  extended by CommandLine
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
ReferencingCommands

public abstract class CommandLine
extends Object
implements Runnable

Base class for command line tools. Subclasses shall define fields annotated with Option and/or methods annotated with Action. The annotated fields will be initialized by the version() method.

The following actions are recognized by this class:

help Print the help summary.
version Print version number and system information.

The following options are recognized by this class:

--colors=on|off Turn on or off syntax coloring on ANSI X3.64 (aka ECMA-48 and ISO/IEC 6429) compatible terminal.

--debug Print full stack trace in case of error.
--encoding=cp Sets the console encoding ("UTF-8", "ISO-8859-1", etc.) for application input and output. This value has no impact on data, but may improve the output quality. This is not needed on Linux terminal using UTF-8 encoding (tip: the terminus font gives good results).
--locale=lc Set the locale for string, number and date formatting ("fr" for French, etc.).

Since:
2.5
Version:
3.15
Author:
Martin Desruisseaux (Geomatys), Cédric Briançon (Geomatys)
Module:
utility/geotk-utility (download)    View source code for this class

Field Summary
static int ABORT_EXIT_CODE
          The code given to System.exit(int) when the program aborted at user request.
protected  String[] arguments
          The remaining arguments after all option values have been assigned to the fields.
static int BAD_CONTENT_EXIT_CODE
          The code given to System.exit(int) when the program failed because of bad content in a file.
protected  Boolean colors
          true if colors can be applied for ANSI X3.64 compliant terminal.
protected  boolean debug
          true if the --debug option has been passed on the command line.
protected  Charset encoding
          The encoding specified by the "--encoding" option.
protected  PrintWriter err
          Error stream to the console.
static int ILLEGAL_ARGUMENT_EXIT_CODE
          The code given to System.exit(int) when the program failed because of an illegal user argument.
static int ILLEGAL_STATE_EXIT_CODE
          The code given to System.exit(int) when the program failed because the system is in a state that does not allow the execution of the program.
protected  Reader in
          Input stream from the console.
static int INTERNAL_ERROR_EXIT_CODE
          The code given to System.exit(int) when the program failed because of an internal error.
static int IO_EXCEPTION_EXIT_CODE
          The code given to System.exit(int) when the program failed because of an IOException.
protected  Locale locale
          The locale specified by the "--locale" option.
protected  PrintWriter out
          Output stream to the console.
static int SQL_EXCEPTION_EXIT_CODE
          The code given to System.exit(int) when the program failed because of a SQLException.
 
Constructor Summary
protected CommandLine(String command, String[] arguments)
          Creates a new CommandLine instance.
 
Method Summary
protected
<T> T
convert(String value, Class<T> type)
          Converts the given value to an object of the given type.
protected  void exit(int code)
          Invoked when an error occurred while processing the command-line arguments or during action execution.
protected  void help()
          Invoked when the user asked the "help" action.
protected  void printException(Throwable exception)
          Invoked when an exception occurred because of user's error.
 void run()
          Runs the command line.
protected  void summary()
          Invoked when the user didn't asked for any action.
protected  void unknownAction(String action)
          Invoked when the version() method didn't recognized the action given by the user.
protected  void version()
          Invoked when the user asked the "version" action.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ILLEGAL_ARGUMENT_EXIT_CODE

public static final int ILLEGAL_ARGUMENT_EXIT_CODE
The code given to System.exit(int) when the program failed because of an illegal user argument.

See Also:
Constant Field Values

ABORT_EXIT_CODE

public static final int ABORT_EXIT_CODE
The code given to System.exit(int) when the program aborted at user request.

See Also:
Constant Field Values

BAD_CONTENT_EXIT_CODE

public static final int BAD_CONTENT_EXIT_CODE
The code given to System.exit(int) when the program failed because of bad content in a file.

See Also:
Constant Field Values

IO_EXCEPTION_EXIT_CODE

public static final int IO_EXCEPTION_EXIT_CODE
The code given to System.exit(int) when the program failed because of an IOException.

See Also:
Constant Field Values

SQL_EXCEPTION_EXIT_CODE

public static final int SQL_EXCEPTION_EXIT_CODE
The code given to System.exit(int) when the program failed because of a SQLException.

See Also:
Constant Field Values

ILLEGAL_STATE_EXIT_CODE

public static final int ILLEGAL_STATE_EXIT_CODE
The code given to System.exit(int) when the program failed because the system is in a state that does not allow the execution of the program.

Since:
3.00
See Also:
Constant Field Values

INTERNAL_ERROR_EXIT_CODE

public static final int INTERNAL_ERROR_EXIT_CODE
The code given to System.exit(int) when the program failed because of an internal error.

Since:
3.00
See Also:
Constant Field Values

debug

@Debug
@Option
protected boolean debug
true if the --debug option has been passed on the command line.

Since:
3.00

locale

@Option(examples={"fr","fr_CA","US"})
protected Locale locale
The locale specified by the "--locale" option. If no such option was provided, then this field is set to the default locale.


encoding

@Option(examples={"UTF-8","ISO-8859-1"})
protected Charset encoding
The encoding specified by the "--encoding" option. If no such option was provided, then this field is set to the default charset.


colors

@Option
protected Boolean colors
true if colors can be applied for ANSI X3.64 compliant terminal. This is the value specified by the --colors arguments if present, or a value inferred from the system otherwise.

Since:
3.00

in

protected Reader in
Input stream from the console. This is often a BufferedReader instance. This input stream use the encoding specified by the "--encoding" argument, if presents.

Since:
3.00

out

protected PrintWriter out
Output stream to the console. This output stream use the encoding specified by the "--encoding" argument, if presents.


err

protected PrintWriter err
Error stream to the console.


arguments

protected String[] arguments
The remaining arguments after all option values have been assigned to the fields.

Constructor Detail

CommandLine

protected CommandLine(String command,
                      String[] arguments)
Creates a new CommandLine instance. This constructor keep a reference to the given arguments, but does not parse them yet. The arguments are parsed when version() is invoked.

Parameters:
command - The command entered on the command line for launching the application. If null, default to "java <classname>".
arguments - The command-line arguments specified after the command.
Since:
3.00
Method Detail

convert

protected <T> T convert(String value,
                        Class<T> type)
             throws NonconvertibleObjectException
Converts the given value to an object of the given type. The default implementation delegates the work to the system converter. Subclasses can override this method if they need to convert values in a particular way.

Type Parameters:
T - The destination type.
Parameters:
value - The string value to convert.
type - The destination type.
Returns:
The converted value.
Throws:
NonconvertibleObjectException - if the value can't be converted.
Since:
3.00

run

public void run()
Runs the command line. The default implementation searches for a no-argument method annotated with Action and having a name matching the first argument which is not an option. If no action is given or if it was not recognized, then unknownAction(String) method is invoked.

This method should be invoked by the main method of subclasses as below:

public static void main(String[] arguments) {
    CommandLine cmd = new MyCommands(arguments);
    cmd.run();
}

Specified by:
run in interface Runnable

unknownAction

protected void unknownAction(String action)
Invoked when the version() method didn't recognized the action given by the user. If the user didn't provided any action at all, then action is null. Otherwise action is the user-provided action which was not recognized.

The default implementation prints a summary if action is null, or an error message if non-null, then exit.

Parameters:
action - The unrecognized action, or null if the user didn't supplied any action.
Since:
3.00

summary

protected void summary()
Invoked when the user didn't asked for any action. The default implementation prints a summary of available actions and options. Subclasses can override this method if they want to print different informations.

Since:
3.00

version

@Action(maximalArgumentCount=0)
protected void version()
Invoked when the user asked the "version" action. The default implementation prints version number and system informations. Subclasses can override this method if they want to print more informations.


help

@Action(maximalArgumentCount=0)
protected void help()
Invoked when the user asked the "help" action. The default implementation prints a description of this command and all arguments to the standard output. The description is read from a properties file of the same name than the subclass, using ResourceBundle with the current locale. Then this class prints the following:


printException

protected void printException(Throwable exception)
Invoked when an exception occurred because of user's error. The default implementation prints only a summary of the given exception, except if debug is true in which case the full stack trace is printed.

The exception is expected to be a user's error, not a programming error (the later are propagated like ordinary exceptions; they do not pass through this method). For example this method is invoked if a FileNotFoundException occurred while trying to open a file given on the command line. Callers are expected to invoke exit(int) after this method.

Parameters:
exception - The exception that forced the exit.
Since:
3.00

exit

protected void exit(int code)
Invoked when an error occurred while processing the command-line arguments or during action execution. The default implementation flushs the streams and invokes System.exit(int). Subclasses can override this method if they want to perform some other action.

Callers should not assume that JVM will stop execution after this method call, because the default behavior may be overridden in some cases. Callers should exit their method (usually with a return statement) immediately after the call to this method.

Note that this method may be invoked at any time, including construction time. It should not assume that every fields have been correctly assigned.

Parameters:
code - One of the EXIT_CODE constants.
Since:
3.00


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.