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

Class RunFirst

src/main/java/picocli/CommandLine.java:2337–2384  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

2335 * For use by the {@link #execute(String...) execute} method.
2336 * @since 2.0 */
2337 public static class RunFirst extends AbstractParseResultHandler<List<Object>> implements IParseResultHandler {
2338 /** {@inheritDoc} */
2339 public int execute(ParseResult parseResult) throws ExecutionException { return super.execute(parseResult); }
2340
2341 /** Prints help if requested, and otherwise executes the top-level {@code Runnable} or {@code Callable} command.
2342 * Finally, either a list of result objects is returned, or the JVM is terminated if an exit code {@linkplain #andExit(int) was set}.
2343 * If the top-level command does not implement either {@code Runnable} or {@code Callable}, an {@code ExecutionException}
2344 * is thrown detailing the problem and capturing the offending {@code CommandLine} object.
2345 *
2346 * @param parsedCommands the {@code CommandLine} objects that resulted from successfully parsing the command line arguments
2347 * @param out the {@code PrintStream} to print help to if requested
2348 * @param ansi for printing help messages using ANSI styles and colors
2349 * @return an empty list if help was requested, or a list containing a single element: the result of calling the
2350 * {@code Callable}, or a {@code null} element if the top-level command was a {@code Runnable}
2351 * @throws ParameterException if the {@link HelpCommand HelpCommand} was invoked for an unknown subcommand. Any {@code ParameterExceptions}
2352 * thrown from this method are treated as if this exception was thrown during parsing and passed to the {@link IExceptionHandler}
2353 * @throws ExecutionException if a problem occurred while processing the parse results; use
2354 * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed
2355 */
2356 public List<Object> handleParseResult(List<CommandLine> parsedCommands, PrintStream out, Help.Ansi ansi) {
2357 if (printHelpIfRequested(parsedCommands, out, err(), ansi)) { return returnResultOrExit(Collections.emptyList()); }
2358 return returnResultOrExit(executeUserObject(parsedCommands.get(0), new ArrayList<Object>()));
2359 }
2360 /** Executes the top-level {@code Runnable} or {@code Callable} subcommand.
2361 * If the top-level command does not implement either {@code Runnable} or {@code Callable} and is not a {@code Method}, an {@code ExecutionException}
2362 * is thrown detailing the problem and capturing the offending {@code CommandLine} object.
2363 *
2364 * @param parseResult the {@code ParseResult} that resulted from successfully parsing the command line arguments
2365 * @return an empty list if help was requested, or a list containing a single element: the result of calling the
2366 * {@code Callable}, or a {@code null} element if the last (sub)command was a {@code Runnable}
2367 * @throws ExecutionException if a problem occurred while processing the parse results; use
2368 * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed
2369 * @since 3.0 */
2370 protected List<Object> handle(ParseResult parseResult) throws ExecutionException {
2371 Tracer t = CommandLine.tracer();
2372 t.debug("RunFirst: executing user object for '%s'...", parseResult.commandSpec().qualifiedName());
2373 return executeUserObject(parseResult.commandSpec().commandLine(), new ArrayList<Object>()); // first
2374 }
2375
2376 protected List<IExitCodeGenerator> extractExitCodeGenerators(ParseResult parseResult) {
2377 if (parseResult.commandSpec().userObject() instanceof IExitCodeGenerator) {
2378 return Collections.singletonList((IExitCodeGenerator) parseResult.commandSpec().userObject());
2379 }
2380 return Collections.emptyList();
2381 }
2382
2383 @Override protected RunFirst self() { return this; }
2384 }
2385 /**
2386 * Command line {@linkplain IExecutionStrategy execution strategy} that prints help if requested, and otherwise executes the most specific
2387 * {@code Runnable} or {@code Callable} subcommand.

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…