@CliClass @CliModes(value=@CliMode(id="help",title="help",usage="Print help about this program.")) public abstract class AbstractMain extends AbstractLoggableObject
AbstractVersionedMain
. For an example read the
package javadoc
.Modifier and Type | Field and Description |
---|---|
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
|
private boolean |
help
Option to show the
usage . |
private CliOutputSettings |
outputSettings |
private CliParserBuilder |
parserBuilder |
private PrintWriter |
standardError |
private PrintWriter |
standardOutput |
private StreamUtil |
streamUtil |
Constructor and Description |
---|
AbstractMain()
The constructor.
|
Modifier and Type | Method and Description |
---|---|
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()
|
PrintWriter |
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.
|
protected void |
runAndExit(String... args)
This method delegates to
run(String...) and then calls System.exit(int) with the returned exit
code. |
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.
|
getLogger
protected static final int EXIT_CODE_OK
exit-code
on success.protected static final int EXIT_CODE_ILLEGAL_SYNTAX
exit-code
on syntax error (illegal command-line arguments).protected static final int EXIT_CODE_CONSTRAINT_VIOLATION
exit-code
on constraint error (illegal command-line value).protected static final int EXIT_CODE_UNEXPECTED
private final CliOutputSettings outputSettings
private PrintWriter standardOutput
private PrintWriter standardError
private CliParserBuilder parserBuilder
private StreamUtil streamUtil
protected abstract int run(CliModeObject mode) throws Exception
mode
- is the mode
of the invocation.EXIT_CODE_OK
on success.Exception
- in case of an unexpected error.protected int handleError(Exception exception, CliParser parser)
Exception
occurred. It implements how to handle the error. You may override to
add a custom handling.exception
- is the actual error that occurred.parser
- is the CliParser
.0
).protected IocContainer getIocContainer()
IocContainer
used to manage components with their implementation. It should be created
and initialized on the first call of this method. null
to avoid dependencies on a IocContainer
implementation. Override this method to use proper component management.IocContainer
.SpringContainer
protected CliParserBuilder getParserBuilder()
CliParserBuilder
used to build
the
CliParser
. IoC container
in advance to
overriding this method.CliParserBuilder
.protected StreamUtil getStreamUtil()
StreamUtil
to use.StreamUtil
to use.public CliOutputSettings getOutputSettings()
output settings
used for information and error messages.public final PrintWriter getStandardOutput()
print
information for the end-user.
System.out
.public final void setStandardOutput(Appendable output)
standard output
. It may be used by sub-classes or tests to
redirect output.output
- is the output to setpublic final PrintWriter getStandardError()
public final void setStandardError(Appendable error)
standard error
. It may be used by sub-classes or tests to redirect
errors.error
- is the error to set.protected void printHelp(CliParser parser)
parser
- is the CliParser
.protected void validate(CliModeObject mode) throws RuntimeException
mode
- is the mode
of the invocation.RuntimeException
- if the state is invalid.public int run(String... args)
args
- are the commandline-arguments.0
on success.protected void runAndExit(String... args)
run(String...)
and then calls System.exit(int)
with the returned exit
code. You can easily call this additional statement from your regular main
method, but this allows us to
concentrate the "evil" System.out
statement at a single place.args
- are the commandline arguments.Copyright © 2001–2016 mmm-Team. All rights reserved.