Package io.github.qishr.cascara.common.util
Class RunLast
java.lang.Object
io.github.qishr.cascara.common.util.CommandLine.AbstractHandler<java.util.List<java.lang.Object>
io.github.qishr.cascara.common.util.CommandLine.AbstractParseResultHandler<java.util.List<java.lang.Object>
io.github.qishr.cascara.common.util.CommandLine.RunLast
All Implemented Interfaces:
IParseResultHandler
Enclosing Class:
io.github.qishr.cascara.common.util.CommandLine
public static class RunLast
extends io.github.qishr.cascara.common.util.CommandLine.AbstractParseResultHandler<java.util.List<Object>
Command line IExecutionStrategy that prints help if requested, and otherwise executes the most specific
Runnable or Callable subcommand.For use by the execute method.
Something like this:
// RunLast implementation: print help if requested, otherwise execute the most specific subcommand
List<CommandLine> parsedCommands = parseResult.asCommandLineList();
if (CommandLine.printHelpIfRequested(parsedCommands, out(), err(), ansi())) {
return emptyList();
}
CommandLine last = parsedCommands.get(parsedCommands.size() - 1);
Object command = last.getCommand();
Object result = null;
if (command instanceof Runnable) {
try {
((Runnable) command).run();
} catch (Exception ex) {
throw new ExecutionException(last, "Error in runnable " + command, ex);
}
} else if (command instanceof Callable) {
try {
result = ((Callable) command).call();
} catch (Exception ex) {
throw new ExecutionException(last, "Error in callable " + command, ex);
}
} else {
throw new ExecutionException(last, "Parsed command (" + command + ") is not Runnable or Callable");
}
last.setExecutionResult(result);
return Arrays.asList(result);
From picocli v2.0, RunLast is used to implement the #run(Runnable, PrintStream, PrintStream, Help.Ansi, String...)
and #call(Callable, PrintStream, PrintStream, Help.Ansi, String...) convenience methods.
Constructor Summary
| Constructor | Description |
|---|---|
| RunLast() |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
| public int | execute(ParseResult parseResult) | |
| public List<Object> | handleParseResult(List<CommandLine> parsedCommands, PrintStream out, Ansi ansi) | Prints help if requested, and otherwise executes the most specific Runnable or Callable subcommand. |
| protected List<Object> | handle(ParseResult parseResult) | Executes the most specific Runnable or Callable subcommand. |
| protected List<IExitCodeGenerator> | extractExitCodeGenerators(ParseResult parseResult) | |
| protected RunLast | self() |
Method Details
execute
public int execute(ParseResult parseResult)
Throws:
handleParseResult
public List<Object> handleParseResult(List<CommandLine> parsedCommands, PrintStream out, Ansi ansi)
Prints help if requested, and otherwise executes the most specific Runnable or Callable subcommand.
For repeatable subcommands, this method may execute multiple subcommands: the most deeply nested subcommands that have the same parent command.
Finally, either a list of result objects is returned, or the JVM is terminated if an exit code was set.
If the last (sub)command does not implement either Runnable or Callable, an ExecutionException
is thrown detailing the problem and capturing the offending CommandLine object.
Parameters:
parsedCommands - the CommandLine objects that resulted from successfully parsing the command line arguments
out - the PrintStream to print help to if requested
ansi - for printing help messages using ANSI styles and colors
Returns:
an empty list if help was requested, or a list containing a single element: the result of calling the
Callable, or a null element if the last (sub)command was a Runnable
Specified By:
handle
protected List<Object> handle(ParseResult parseResult)
Executes the most specific Runnable or Callable subcommand.
For repeatable subcommands, this method may execute multiple subcommands: the most deeply nested subcommands that have the same parent command.
If the user object of the executed (sub)command does not implement either Runnable or Callable and is not a Method, an ExecutionException
is thrown detailing the problem and capturing the offending CommandLine object.
Parameters:
parseResult - the ParseResult that resulted from successfully parsing the command line arguments
Returns:
an empty list if help was requested, or a list containing a single element: the result of calling the
Callable, or a null element if the last (sub)command was a Runnable
Throws:
Since:
3.0
extractExitCodeGenerators
protected List<IExitCodeGenerator> extractExitCodeGenerators(ParseResult parseResult)
self
protected RunLast self()
Overrides: