CommandLine.RunLast (picocli 2.3.0 API)
Date: 2018-02-13

picocli

## Class CommandLine.RunLast

- java.lang.Object
- - picocli.CommandLine.RunLast

- All Implemented Interfaces:
    CommandLine.IParseResultHandler
  Enclosing class:
    CommandLine

      public static class CommandLine.RunLast
      extends java.lang.Object
      implements CommandLine.IParseResultHandler

  Command line parse result handler that prints help if requested, and otherwise executes the most specific `Runnable` or `Callable` subcommand. For use in the `parseWithHandler` methods.

  Something like this:

           // RunLast implementation: print help if requested, otherwise execute the most specific subcommand
           if (CommandLine.printHelpIfRequested(parsedCommands, System.err, Help.Ansi.AUTO)) {
               return emptyList();
           }
           CommandLine last = parsedCommands.get(parsedCommands.size() - 1);
           Object command = last.getCommand();
           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) {
               Object result;
               try {
                   result = ((Callable) command).call();
               } catch (Exception ex) {
                   throw new ExecutionException(last, "Error in callable " + command, ex);
               }
               // ...do something with result
           } else {
               throw new ExecutionException(last, "Parsed command (" + command + ") is not Runnable or Callable");
           }

  Since:
    2.0

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `CommandLine.RunLast()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `java.util.List<java.lang.Object>` | `handleParseResult(java.util.List<CommandLine> parsedCommands, java.io.PrintStream out, CommandLine.Help.Ansi ansi)` Prints help if requested, and otherwise executes the most specific `Runnable` or `Callable` subcommand.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### CommandLine.RunLast

          public CommandLine.RunLast()

  - ### Method Detail

    - #### handleParseResult

          public java.util.List<java.lang.Object> handleParseResult(java.util.List<CommandLine> parsedCommands,
                                                           java.io.PrintStream out,
                                                           CommandLine.Help.Ansi ansi)

      Prints help if requested, and otherwise executes the most specific `Runnable` or `Callable` subcommand. If the last (sub)command does not implement either `Runnable` or `Callable`, a `ExecutionException` is thrown detailing the problem and capturing the offending `CommandLine` object.
      Specified by:
        `handleParseResult` in interface `CommandLine.IParseResultHandler`
      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`
      Throws:
        `CommandLine.ExecutionException` - if a problem occurred while processing the parse results; use `CommandLine.ExecutionException.getCommandLine()` to get the command or subcommand where processing failed