MCPcopy
hub / github.com/remkop/picocli / RunAll

Class RunAll

src/main/java/picocli/CommandLine.java:2500–2561  ·  view source on GitHub ↗

Command line plain IExecutionStrategy execution strategy that prints help if requested, and otherwise executes the top-level command and all subcommands as Runnable, Callable or Method. For use by the #execute(String...) execute method. @since 2.0

Source from the content-addressed store, hash-verified

2498 * For use by the {@link #execute(String...) execute} method.
2499 * @since 2.0 */
2500 public static class RunAll extends AbstractParseResultHandler<List<Object>> implements IParseResultHandler {
2501 /** {@inheritDoc} */
2502 public int execute(ParseResult parseResult) throws ExecutionException { return super.execute(parseResult); }
2503
2504 /** Prints help if requested, and otherwise executes the top-level command and all subcommands as {@code Runnable},
2505 * {@code Callable} or {@code Method}. Finally, either a list of result objects is returned, or the JVM is terminated if an exit
2506 * code {@linkplain #andExit(int) was set}. If any of the {@code CommandLine} commands does not implement either
2507 * {@code Runnable} or {@code Callable}, an {@code ExecutionException}
2508 * is thrown detailing the problem and capturing the offending {@code CommandLine} object.
2509 *
2510 * @param parsedCommands the {@code CommandLine} objects that resulted from successfully parsing the command line arguments
2511 * @param out the {@code PrintStream} to print help to if requested
2512 * @param ansi for printing help messages using ANSI styles and colors
2513 * @return an empty list if help was requested, or a list containing the result of executing all commands:
2514 * the return values from calling the {@code Callable} commands, {@code null} elements for commands that implement {@code Runnable}
2515 * @throws ParameterException if the {@link HelpCommand HelpCommand} was invoked for an unknown subcommand. Any {@code ParameterExceptions}
2516 * thrown from this method are treated as if this exception was thrown during parsing and passed to the {@link IExceptionHandler}
2517 * @throws ExecutionException if a problem occurred while processing the parse results; use
2518 * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed
2519 */
2520 public List<Object> handleParseResult(List<CommandLine> parsedCommands, PrintStream out, Help.Ansi ansi) {
2521 if (printHelpIfRequested(parsedCommands, out, err(), ansi)) { return returnResultOrExit(Collections.emptyList()); }
2522 List<Object> result = new ArrayList<Object>();
2523 for (CommandLine parsed : parsedCommands) {
2524 executeUserObject(parsed, result);
2525 }
2526 return returnResultOrExit(result);
2527 }
2528 /** Executes the top-level command and all subcommands as {@code Runnable} or {@code Callable}.
2529 * If any of the {@code CommandLine} commands does not implement either {@code Runnable} or {@code Callable} and is not a {@code Method}, an {@code ExecutionException}
2530 * is thrown detailing the problem and capturing the offending {@code CommandLine} object.
2531 *
2532 * @param parseResult the {@code ParseResult} that resulted from successfully parsing the command line arguments
2533 * @return an empty list if help was requested, or a list containing the result of executing all commands:
2534 * the return values from calling the {@code Callable} commands, {@code null} elements for commands that implement {@code Runnable}
2535 * @throws ExecutionException if a problem occurred while processing the parse results; use
2536 * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed
2537 * @since 3.0 */
2538 protected List<Object> handle(ParseResult parseResult) throws ExecutionException {
2539 Tracer t = CommandLine.tracer();
2540 return returnResultOrExit(recursivelyExecuteUserObject(parseResult, new ArrayList<Object>(), t));
2541 }
2542 private List<Object> recursivelyExecuteUserObject(ParseResult parseResult, List<Object> result, Tracer t) throws ExecutionException {
2543 t.debug("%s: executing user object for '%s'...", getClass().getSimpleName(), parseResult.commandSpec.qualifiedName());
2544 executeUserObject(parseResult.commandSpec().commandLine(), result);
2545 for (ParseResult pr : parseResult.subcommands()) {
2546 recursivelyExecuteUserObject(pr, result, t);
2547 }
2548 return result;
2549 }
2550 protected List<IExitCodeGenerator> extractExitCodeGenerators(ParseResult parseResult) {
2551 return recursivelyExtractExitCodeGenerators(parseResult, new ArrayList<IExitCodeGenerator>());
2552 }
2553 private List<IExitCodeGenerator> recursivelyExtractExitCodeGenerators(ParseResult parseResult, List<IExitCodeGenerator> result) throws ExecutionException {
2554 if (parseResult.commandSpec().userObject() instanceof IExitCodeGenerator) { result.add((IExitCodeGenerator) parseResult.commandSpec().userObject()); }
2555 for (ParseResult pr : parseResult.subcommands()) {
2556 recursivelyExtractExitCodeGenerators(pr, result);
2557 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…