net.sf.mmm.util.cli.api
Class AbstractMain

java.lang.Object
  extended by net.sf.mmm.util.component.base.AbstractLoggableObject
      extended by net.sf.mmm.util.cli.api.AbstractMain
Direct Known Subclasses:
AbstractVersionedMain

@CliClass
@CliModes(value=@CliMode(id="help",title="help",usage="Print help about this program."))
public abstract class AbstractMain
extends AbstractLoggableObject

This is the abstract base class for a main-program.

Since:
1.0.0
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Field Summary
protected static int EXIT_CODE_CONSTRAINT_VIOLATION
          The exit-code on constraint error (illegal command-line value).
protected static int EXIT_CODE_ILLEGAL_SYNTAX
          The exit-code on syntax error (illegal command-line arguments).
protected static int EXIT_CODE_OK
          The exit-code on success.
protected static int EXIT_CODE_UNEXPECTED
          The exit-code if an unexpected error occurred.
private  boolean help
          Option to show the usage.
private  CliOutputSettings outputSettings
           
private  CliParserBuilder parserBuilder
           
private  PrintWriter standardError
           
private  PrintWriter standardOutput
           
private  StreamUtil streamUtil
           
 
Constructor Summary
AbstractMain()
          The constructor.
 
Method Summary
protected  IocContainer getIocContainer()
          This method gets the IocContainer used to manage components with their implementation.
 CliOutputSettings getOutputSettings()
          This method gets the output settings used for information and error messages.
protected  CliParserBuilder getParserBuilder()
          This method gets the CliParserBuilder used to build the CliParser.
 Appendable getStandardError()
          This method gets the standard error where to print errors for the end-user.
 PrintWriter getStandardOutput()
          This method gets the standard output where to print information for the end-user.
protected  StreamUtil getStreamUtil()
          This method gets the StreamUtil to use.
protected  int handleError(Exception exception, CliParser parser)
          This method is invoked if an Exception occurred.
protected  void printHelp(CliParser parser)
          This method prints the help output with the program usage.
protected abstract  int run(CliModeObject mode)
          This method is called after the options are parsed and injected.
 int run(String... args)
          This method should be invoked from the static main-method.
 void setStandardError(Appendable error)
          This method sets the standard error.
 void setStandardOutput(Appendable output)
          This method sets the standard output.
protected  void validate(CliModeObject mode)
          This method is called after the command-line arguments are checked for syntactically correctness and applied to this class in order to perform complex validations.
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractLoggableObject
getLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EXIT_CODE_OK

protected static final int EXIT_CODE_OK
The exit-code on success.

See Also:
Constant Field Values

EXIT_CODE_ILLEGAL_SYNTAX

protected static final int EXIT_CODE_ILLEGAL_SYNTAX
The exit-code on syntax error (illegal command-line arguments).

See Also:
Constant Field Values

EXIT_CODE_CONSTRAINT_VIOLATION

protected static final int EXIT_CODE_CONSTRAINT_VIOLATION
The exit-code on constraint error (illegal command-line value).

See Also:
CliParameterContainer.getValidator(), Constant Field Values

EXIT_CODE_UNEXPECTED

protected static final int EXIT_CODE_UNEXPECTED
The exit-code if an unexpected error occurred.

See Also:
Constant Field Values

outputSettings

private final CliOutputSettings outputSettings
See Also:
getOutputSettings()

standardOutput

private PrintWriter standardOutput
See Also:
getStandardOutput()

standardError

private PrintWriter standardError
See Also:
getStandardError()

parserBuilder

private CliParserBuilder parserBuilder
See Also:
getParserBuilder()

streamUtil

private StreamUtil streamUtil
See Also:
getStreamUtil()

help

@CliOption(name="--help",
           aliases="-h",
           required=true,
           usage="Print this help.",
           mode="help")
private boolean help
Option to show the usage.

Constructor Detail

AbstractMain

public AbstractMain()
The constructor.

Method Detail

run

protected abstract int run(CliModeObject mode)
                    throws Exception
This method is called after the options are parsed and injected. It has to be implemented and should do the actual job.

Parameters:
mode - is the mode of the invocation.
Returns:
the error-code or EXIT_CODE_OK on success.
Throws:
Exception - in case of an unexpected error.

handleError

protected int handleError(Exception exception,
                          CliParser parser)
This method is invoked if an Exception occurred. It implements how to handle the error. You may override to add a custom handling.

Parameters:
exception - is the actual error that occurred.
parser - is the CliParser.
Returns:
the error-code (NOT 0).

getIocContainer

protected IocContainer getIocContainer()
This method gets the IocContainer used to manage components with their implementation. It should be created and initialized on the first call of this method.
This default implementation simply returns null to avoid dependencies on a IocContainer implementation. Override this method to use proper component management.

Returns:
the IocContainer.
See Also:
SpringContainer

getParserBuilder

protected CliParserBuilder getParserBuilder()
This method gets the CliParserBuilder used to build the CliParser.
To extend with custom functionality you should use an IoC container in advance to overriding this method.

Returns:
the CliParserBuilder.

getStreamUtil

protected StreamUtil getStreamUtil()
This method gets the StreamUtil to use.

Returns:
the StreamUtil to use.

getOutputSettings

public CliOutputSettings getOutputSettings()
This method gets the output settings used for information and error messages.

Returns:
the outputSettings.

getStandardOutput

public final PrintWriter getStandardOutput()
This method gets the standard output where to print information for the end-user.
Default is System.out.

Returns:
the standard output.

setStandardOutput

public final void setStandardOutput(Appendable output)
This method sets the standard output. It may be used by sub-classes or tests to redirect output.

Parameters:
output - is the output to set

getStandardError

public final Appendable getStandardError()
This method gets the standard error where to print errors for the end-user.
Default is System.err.

Returns:
the output stream.

setStandardError

public final void setStandardError(Appendable error)
This method sets the standard error. It may be used by sub-classes or tests to redirect errors.

Parameters:
error - is the error to set.

printHelp

protected void printHelp(CliParser parser)
This method prints the help output with the program usage.

Parameters:
parser - is the CliParser.

validate

protected void validate(CliModeObject mode)
                 throws RuntimeException
This method is called after the command-line arguments are checked for syntactically correctness and applied to this class in order to perform complex validations.

Parameters:
mode - is the mode of the invocation.
Throws:
RuntimeException - if the state is invalid.

run

public int run(String... args)
This method should be invoked from the static main-method.

Parameters:
args - are the commandline-arguments.
Returns:
the exit code or 0 on success.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.