/** Executes the most specific {@code Runnable} or {@code Callable} subcommand. * If the last (sub)command does not implement either {@code Runnable} or {@code Callable}, an {@code ExecutionException} * is thrown detailing the problem and capturing the offending {@code CommandLine} object. * * @param parseResult the {@code ParseResult} that resulted from successfully parsing the command line arguments * @return an empty list if help was requested, or a list containing a single element: the result of calling the * {@code Callable}, or a {@code null} element if the last (sub)command was a {@code Runnable} * @throws ExecutionException if a problem occurred while processing the parse results; use * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed * @since 3.0 */ protected List<Object> handle(ParseResult parseResult) throws ExecutionException { List<CommandLine> parsedCommands = parseResult.asCommandLineList(); return execute(parsedCommands.get(parsedCommands.size() - 1), new ArrayList<Object>()); } @Override protected RunLast self() { return this; }
/** Executes the most specific {@code Runnable} or {@code Callable} subcommand. * If the last (sub)command does not implement either {@code Runnable} or {@code Callable}, an {@code ExecutionException} * is thrown detailing the problem and capturing the offending {@code CommandLine} object. * * @param parseResult the {@code ParseResult} that resulted from successfully parsing the command line arguments * @return an empty list if help was requested, or a list containing a single element: the result of calling the * {@code Callable}, or a {@code null} element if the last (sub)command was a {@code Runnable} * @throws ExecutionException if a problem occurred while processing the parse results; use * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed * @since 3.0 */ protected List<Object> handle(ParseResult parseResult) throws ExecutionException { List<CommandLine> parsedCommands = parseResult.asCommandLineList(); return execute(parsedCommands.get(parsedCommands.size() - 1), new ArrayList<Object>()); } @Override protected RunLast self() { return this; }
@Before public void initMocks() { final List<CommandLine> commandLines = new ArrayList<>(); commandLines.add(mockCommandLine); when(mockParseResult.asCommandLineList()).thenReturn(commandLines); final List<String> originalArgs = new ArrayList<>(); originalArgs.add(CONFIG_FILE_OPTION_NAME); when(mockParseResult.originalArgs()).thenReturn(originalArgs); when(mockParseResult.matchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(mockConfigOptionSpec); when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(true); when(mockConfigOptionSpec.getter()).thenReturn(mockConfigOptionGetter); }
@Override protected List<Object> handle(final ParseResult parseResult) throws ExecutionException { final CommandLine commandLine = parseResult.asCommandLineList().get(0); if (parseResult.hasMatchedOption(configFileOptionName)) { final OptionSpec configFileOption = parseResult.matchedOption(configFileOptionName); final File configFile; try { configFile = configFileOption.getter().get(); } catch (final Exception e) { throw new ExecutionException(commandLine, e.getMessage(), e); } final TomlConfigFileDefaultProvider tomlConfigFileDefaultProvider = new TomlConfigFileDefaultProvider(commandLine, configFile); commandLine.setDefaultValueProvider(tomlConfigFileDefaultProvider); } else if (isDocker) { final File configFile = new File(DOCKER_CONFIG_LOCATION); if (configFile.exists()) { final TomlConfigFileDefaultProvider tomlConfigFileDefaultProvider = new TomlConfigFileDefaultProvider(commandLine, configFile); commandLine.setDefaultValueProvider(tomlConfigFileDefaultProvider); } } commandLine.parseWithHandlers( resultHandler, exceptionHandler, parseResult.originalArgs().toArray(new String[0])); return new ArrayList<>(); }
/** Prints help if requested, and otherwise calls {@link #handle(CommandLine.ParseResult)}. * Finally, either a list of result objects is returned, or the JVM is terminated if an exit code {@linkplain #andExit(int) was set}. * * @param parseResult the {@code ParseResult} that resulted from successfully parsing the command line arguments * @return the result of {@link #handle(ParseResult) processing parse results} * @throws ParameterException if the {@link HelpCommand HelpCommand} was invoked for an unknown subcommand. Any {@code ParameterExceptions} * thrown from this method are treated as if this exception was thrown during parsing and passed to the {@link IExceptionHandler2} * @throws ExecutionException if a problem occurred while processing the parse results; client code can use * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed */ public R handleParseResult(ParseResult parseResult) throws ExecutionException { if (printHelpIfRequested(parseResult.asCommandLineList(), out(), err(), ansi())) { return returnResultOrExit(null); } return returnResultOrExit(handle(parseResult)); }
/** Delegates to {@link #printHelpIfRequested(List, PrintStream, PrintStream, Help.Ansi)} with * {@code parseResult.asCommandLineList(), System.out, System.err, Help.Ansi.AUTO}. * @since 3.0 */ public static boolean printHelpIfRequested(ParseResult parseResult) { return printHelpIfRequested(parseResult.asCommandLineList(), System.out, System.err, Help.Ansi.AUTO); } /**
/** Prints help if requested, and otherwise calls {@link #handle(CommandLine.ParseResult)}. * Finally, either a list of result objects is returned, or the JVM is terminated if an exit code {@linkplain #andExit(int) was set}. * * @param parseResult the {@code ParseResult} that resulted from successfully parsing the command line arguments * @return the result of {@link #handle(ParseResult) processing parse results} * @throws ParameterException if the {@link HelpCommand HelpCommand} was invoked for an unknown subcommand. Any {@code ParameterExceptions} * thrown from this method are treated as if this exception was thrown during parsing and passed to the {@link IExceptionHandler2} * @throws ExecutionException if a problem occurred while processing the parse results; client code can use * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed */ public R handleParseResult(ParseResult parseResult) throws ExecutionException { if (printHelpIfRequested(parseResult.asCommandLineList(), out(), err(), ansi())) { return returnResultOrExit(null); } return returnResultOrExit(handle(parseResult)); }
/** Delegates to {@link #printHelpIfRequested(List, PrintStream, PrintStream, Help.Ansi)} with * {@code parseResult.asCommandLineList(), System.out, System.err, Help.Ansi.AUTO}. * @since 3.0 */ public static boolean printHelpIfRequested(ParseResult parseResult) { return printHelpIfRequested(parseResult.asCommandLineList(), System.out, System.err, Help.Ansi.AUTO); } /**