Class CommandLine.RunLast
- java.lang.Object
-
- org.apache.logging.log4j.core.tools.picocli.CommandLine.RunLast
-
- All Implemented Interfaces:
CommandLine.IParseResultHandler
- Enclosing class:
- CommandLine
public static class CommandLine.RunLast extends Object implements CommandLine.IParseResultHandler
Command line parse result handler that prints help if requested, and otherwise executes the most specificRunnable
orCallable
subcommand. For use in theparseWithHandler
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 Description RunLast()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description List<Object>
handleParseResult(List<CommandLine> parsedCommands, PrintStream out, CommandLine.Help.Ansi ansi)
Prints help if requested, and otherwise executes the most specificRunnable
orCallable
subcommand.
-
-
-
Method Detail
-
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. If the last (sub)command does not implement eitherRunnable
orCallable
, aExecutionException
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.ExecutionException
- if a problem occurred while processing the parse results; useCommandLine.ExecutionException.getCommandLine()
to get the command or subcommand where processing failed
-
-