Package picocli

Class CommandLine.RunLast

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

    public static class CommandLine.RunLast
    extends CommandLine.AbstractParseResultHandler<List<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
         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");
         }
         if (hasExitCode()) { System.exit(exitCode()); }
         return Arrays.asList(result);
     

    From picocli v2.0, RunLast is used to implement the run and call convenience methods.

    Since:
    2.0