Package io.github.qishr.cascara.common.util
Class AbstractHandler
java.lang.Object
io.github.qishr.cascara.common.util.CommandLine.AbstractHandler
Enclosing Class:
io.github.qishr.cascara.common.util.CommandLine
public static abstract class AbstractHandler
Abstract superclass for IParseResultHandler2 and IExceptionHandler2 implementations.
Note that AbstractHandler is a generic type. This, along with the abstract self method,
allows method chaining to work properly in subclasses, without the need for casts. An example subclass can look like this:
` class MyResultHandler extends AbstractHandler
public MyReturnType handleParseResult(ParseResult parseResult) { ... }
protected MyResultHandler self() { return this; }
} `
Constructor Summary
| Constructor | Description |
|---|---|
| AbstractHandler() |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
| public PrintStream | out() | Returns the stream to print command output to. |
| public PrintStream | err() | Returns the stream to print diagnostic messages to. |
| public Ansi | ansi() | Returns the ANSI style to use. |
| public ColorScheme | colorScheme() | Returns the ColorScheme to use. |
| public Integer | exitCode() | Returns the exit code to use as the termination status, or null (the default) if the handler should not call System.exit after processing completes. |
| public boolean | hasExitCode() | Returns true if an exit code was set with andExit.andExit, or false (the default) if the handler should not call System.exit after processing completes. |
| protected R | returnResultOrExit(R result) | Convenience method for subclasses that returns the specified result object if no exit code was set, or otherwise, if an exit code andExit.andExit, calls System.exit with the configured exit code to terminate the currently running Java virtual machine. |
| protected R | throwOrExit(ExecutionException ex) | Convenience method for subclasses that throws the specified ExecutionException if no exit code was set, or otherwise, if an exit code andExit.andExit, prints the stacktrace of the specified exception to the diagnostic error stream and calls System.exit with the configured exit code to terminate the currently running Java virtual machine. |
| protected void | exit(int exitCode) | Calls System.exit(int) with the specified exit code. |
| protected abstract T | self() | Returns this to allow method chaining when calling the setters for a fluent API. |
| public T | useOut(PrintStream out) | Sets the stream to print command output to. |
| public T | useErr(PrintStream err) | Sets the stream to print diagnostic messages to. |
| public T | useAnsi(Ansi ansi) | Sets the ANSI style to use and resets the color scheme to the default. |
| public T | andExit(int exitCode) | Indicates that the handler should call System.exit after processing completes and sets the exit code to use as the termination status. |
Method Details
out
public PrintStream out()
Returns the stream to print command output to.Defaults to System.out, unless useOut.useOut
was called with a different stream.
IParseResultHandler2 implementations should use this stream.
By convention, when the user requests
help with a --help or similar option, the usage help message is printed to the standard output stream so that it can be easily searched and paged.
err
public PrintStream err()
Returns the stream to print diagnostic messages to.Defaults to System.err, unless useErr.useErr
was called with a different stream.
IExceptionHandler2 implementations should use this stream to print error
messages (which may include a usage help message) when an unexpected error occurs.
ansi
public Ansi ansi()
Returns the ANSI style to use.Defaults to Help.Ansi.AUTO, unless useAnsi.useAnsi was called with a different setting.
Deprecation
use colorScheme.colorScheme instead
colorScheme
public ColorScheme colorScheme()
Returns the ColorScheme to use.Defaults to Help#defaultColorScheme(Help.Ansi.AUTO).
Since:
4.0
exitCode
public Integer exitCode()
Returns the exit code to use as the termination status, or null (the default) if the handler should
not call System.exit after processing completes.
See Also:
hasExitCode
public boolean hasExitCode()
Returns true if an exit code was set with andExit.andExit, or false (the default) if
the handler should not call System.exit after processing completes.
returnResultOrExit
protected R returnResultOrExit(R result)
Convenience method for subclasses that returns the specified result object if no exit code was set,
or otherwise, if an exit code andExit.andExit, calls System.exit with the configured
exit code to terminate the currently running Java virtual machine.
throwOrExit
protected R throwOrExit(ExecutionException ex)
Convenience method for subclasses that throws the specified ExecutionException if no exit code was set,
or otherwise, if an exit code andExit.andExit, prints the stacktrace of the specified exception
to the diagnostic error stream and calls System.exit with the configured
exit code to terminate the currently running Java virtual machine.
exit
protected void exit(int exitCode)
Calls System.exit(int) with the specified exit code.
self
protected abstract T self()
Returns this to allow method chaining when calling the setters for a fluent API.
useOut
public T useOut(PrintStream out)
Sets the stream to print command output to.
Deprecation
use CommandLine#setOut(PrintWriter) and CommandLine#execute(String...) instead
useErr
public T useErr(PrintStream err)
Sets the stream to print diagnostic messages to.
Deprecation
use CommandLine#setErr(PrintWriter) and CommandLine#execute(String...) instead
useAnsi
public T useAnsi(Ansi ansi)
Sets the ANSI style to use and resets the color scheme to the default.
Deprecation
use CommandLine#setColorScheme(Help.ColorScheme) and CommandLine#execute(String...) instead
See Also:
andExit
public T andExit(int exitCode)
Indicates that the handler should call System.exit after processing completes and sets the exit code to use as the termination status.
Deprecation
use CommandLine#execute(String...) instead, and call System.exit() in the application.