public static class CommandLine.RunLast extends CommandLine.AbstractParseResultHandler<List<Object>> implements CommandLine.IParseResultHandler
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
and call convenience methods.
| Constructor and Description |
|---|
RunLast() |
| Modifier and Type | Method and Description |
|---|---|
protected List<CommandLine.IExitCodeGenerator> |
extractExitCodeGenerators(CommandLine.ParseResult parseResult) |
protected List<Object> |
handle(CommandLine.ParseResult parseResult)
Executes the most specific
Runnable or Callable subcommand. |
List<Object> |
handleParseResult(List<CommandLine> parsedCommands,
PrintStream out,
CommandLine.Help.Ansi ansi)
Prints help if requested, and otherwise executes the most specific
Runnable or Callable subcommand. |
protected CommandLine.RunLast |
self()
Returns
this to allow method chaining when calling the setters for a fluent API. |
execute, handleParseResultandExit, ansi, colorScheme, err, exit, exitCode, hasExitCode, out, returnResultOrExit, throwOrExit, useAnsi, useErr, useOutpublic List<Object> handleParseResult(List<CommandLine> parsedCommands, PrintStream out, CommandLine.Help.Ansi ansi)
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.
handleParseResult in interface CommandLine.IParseResultHandlerparsedCommands - the CommandLine objects that resulted from successfully parsing the command line argumentsout - the PrintStream to print help to if requestedansi - for printing help messages using ANSI styles and colorsCallable, or a null element if the last (sub)command was a RunnableCommandLine.ParameterException - if the HelpCommand was invoked for an unknown subcommand. Any ParameterExceptions
thrown from this method are treated as if this exception was thrown during parsing and passed to the CommandLine.IExceptionHandlerCommandLine.ExecutionException - if a problem occurred while processing the parse results; use
CommandLine.ExecutionException.getCommandLine() to get the command or subcommand where processing failedprotected List<Object> handle(CommandLine.ParseResult parseResult) throws CommandLine.ExecutionException
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.
handle in class CommandLine.AbstractParseResultHandler<List<Object>>parseResult - the ParseResult that resulted from successfully parsing the command line argumentsCallable, or a null element if the last (sub)command was a RunnableCommandLine.ExecutionException - if a problem occurred while processing the parse results; use
CommandLine.ExecutionException.getCommandLine() to get the command or subcommand where processing failedprotected List<CommandLine.IExitCodeGenerator> extractExitCodeGenerators(CommandLine.ParseResult parseResult)
extractExitCodeGenerators in class CommandLine.AbstractParseResultHandler<List<Object>>protected CommandLine.RunLast self()
CommandLine.AbstractHandlerthis to allow method chaining when calling the setters for a fluent API.self in class CommandLine.AbstractHandler<List<Object>,CommandLine.AbstractParseResultHandler<List<Object>>>