Package nz.org.riskscape.picocli
Class CommandLine.RunLast
java.lang.Object
nz.org.riskscape.picocli.CommandLine.AbstractHandler<R,CommandLine.AbstractParseResultHandler<R>>
nz.org.riskscape.picocli.CommandLine.AbstractParseResultHandler<List<Object>>
nz.org.riskscape.picocli.CommandLine.RunLast
- All Implemented Interfaces:
CommandLine.IExecutionStrategy
,CommandLine.IParseResultHandler
,CommandLine.IParseResultHandler2<List<Object>>
- Enclosing class:
- CommandLine
public static class CommandLine.RunLast
extends CommandLine.AbstractParseResultHandler<List<Object>>
implements CommandLine.IParseResultHandler
Command line execution strategy 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
and call
convenience methods.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected List<CommandLine.IExitCodeGenerator>
extractExitCodeGenerators
(CommandLine.ParseResult parseResult) handle
(CommandLine.ParseResult parseResult) Executes the most specificRunnable
orCallable
subcommand.handleParseResult
(List<CommandLine> parsedCommands, PrintStream out, CommandLine.Help.Ansi ansi) Prints help if requested, and otherwise executes the most specificRunnable
orCallable
subcommand.protected CommandLine.RunLast
self()
Returnsthis
to allow method chaining when calling the setters for a fluent API.Methods inherited from class nz.org.riskscape.picocli.CommandLine.AbstractParseResultHandler
execute, handleParseResult
Methods inherited from class nz.org.riskscape.picocli.CommandLine.AbstractHandler
andExit, ansi, colorScheme, err, exit, exitCode, hasExitCode, out, returnResultOrExit, throwOrExit, useAnsi, useErr, useOut
-
Constructor Details
-
RunLast
public RunLast()
-
-
Method Details
-
handleParseResult
public List<Object> handleParseResult(List<CommandLine> parsedCommands, PrintStream out, CommandLine.Help.Ansi ansi) Prints help if requested, and otherwise executes the most specificRunnable
orCallable
subcommand. 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 eitherRunnable
orCallable
, anExecutionException
is thrown detailing the problem and capturing the offendingCommandLine
object.- Specified by:
handleParseResult
in interfaceCommandLine.IParseResultHandler
- Parameters:
parsedCommands
- theCommandLine
objects that resulted from successfully parsing the command line argumentsout
- thePrintStream
to print help to if requestedansi
- 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 anull
element if the last (sub)command was aRunnable
- Throws:
CommandLine.ParameterException
- if theHelpCommand
was invoked for an unknown subcommand. AnyParameterExceptions
thrown from this method are treated as if this exception was thrown during parsing and passed to theCommandLine.IExceptionHandler
CommandLine.ExecutionException
- if a problem occurred while processing the parse results; useCommandLine.ExecutionException.getCommandLine()
to get the command or subcommand where processing failed
-
handle
protected List<Object> handle(CommandLine.ParseResult parseResult) throws CommandLine.ExecutionException Executes the most specificRunnable
orCallable
subcommand. If the last (sub)command does not implement eitherRunnable
orCallable
and is not aMethod
, anExecutionException
is thrown detailing the problem and capturing the offendingCommandLine
object.- Specified by:
handle
in classCommandLine.AbstractParseResultHandler<List<Object>>
- Parameters:
parseResult
- theParseResult
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 anull
element if the last (sub)command was aRunnable
- Throws:
CommandLine.ExecutionException
- if a problem occurred while processing the parse results; useCommandLine.ExecutionException.getCommandLine()
to get the command or subcommand where processing failed- Since:
- 3.0
-
extractExitCodeGenerators
protected List<CommandLine.IExitCodeGenerator> extractExitCodeGenerators(CommandLine.ParseResult parseResult) - Overrides:
extractExitCodeGenerators
in classCommandLine.AbstractParseResultHandler<List<Object>>
-
self
Description copied from class:CommandLine.AbstractHandler
Returnsthis
to allow method chaining when calling the setters for a fluent API.- Specified by:
self
in classCommandLine.AbstractHandler<List<Object>,
CommandLine.AbstractParseResultHandler<List<Object>>>
-