Class CommandLine.RunLast

  • All Implemented Interfaces:
    CommandLine.IExecutionStrategy, CommandLine.IParseResultHandler, CommandLine.IParseResultHandler2<java.util.List<java.lang.Object>>
    Enclosing class:
    CommandLine

    public static class CommandLine.RunLast
    extends CommandLine.AbstractParseResultHandler<java.util.List<java.lang.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.

    Since:
    2.0