From 52e6a91e8da6e51c83869d78b85a3ddbe82e616a Mon Sep 17 00:00:00 2001 From: Remko Popma Date: Tue, 27 Mar 2018 21:39:16 +0900 Subject: [PATCH] [#312] Remove `AbstractSimpleParseResultHandler` class and `parseWithSimpleHandlers` method. Closes #312. --- RELEASE-NOTES.md | 12 ++- .../model/ResultHandlerWithReturnValue.java | 8 +- ...SimpleResultHandlerWithoutReturnValue.java | 58 ----------- src/main/java/picocli/CommandLine.java | 95 ------------------- 4 files changed, 14 insertions(+), 159 deletions(-) delete mode 100644 examples/src/main/java/picocli/examples/model/SimpleResultHandlerWithoutReturnValue.java diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 1b1336826..a2d798a72 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,8 +1,16 @@ # picocli Release Notes # Picocli 3.0.0-alpha-2 (UNRELEASED) +The picocli community is pleased to announce picocli 3.0.0-alpha-2. + +This release includes some bug fixes and small enhancements. See [3.0.0-alpha-1](#3.0.0-alpha-1) for recent functional changes. + +This is the twenty-second public release. +Picocli follows [semantic versioning](http://semver.org/). + ## Fixed issues +- [#312] Enhancement and API change: Remove `AbstractSimpleParseResultHandler` class and `parseWithSimpleHandlers` method. - [#311] Enhancement and API change: Simplify parseWithHandlers: removed prototypeReturnValue parameter. - [#307] Enhancement: Provide CommandLine.usage(PrintWriter) method for testing and to facilitate [GROOVY-8520](https://issues.apache.org/jira/browse/GROOVY-8520) migration from commons-cli to picocli. - [#306] Enhancement: Support generating autocompletion scripts for non-public @Command classes. Thanks to [cbeams](https://github.com/cbeams) for the request. @@ -11,12 +19,12 @@ - [#309] Bugfix: Tests were failing on environments that support ANSI colors. ## Deprecations -See [3.0.0-alpha-1]()#3.0.0-alpha-1-deprecated) +See [3.0.0-alpha-1](#3.0.0-alpha-1-deprecated) ## Potential breaking changes - [#311] This is an API change from 3.0.0-alpha-1: the `parseWithHandlers` methods signature changed: removed the `prototypeReturnValue` parameter. -See [3.0.0-alpha-1]()#3.0.0-alpha-1-breaking-changes) +See [3.0.0-alpha-1](#3.0.0-alpha-1-breaking-changes) # Picocli 3.0.0-alpha-1 The picocli community is pleased to announce picocli 3.0.0-alpha-1. diff --git a/examples/src/main/java/picocli/examples/model/ResultHandlerWithReturnValue.java b/examples/src/main/java/picocli/examples/model/ResultHandlerWithReturnValue.java index dae30f1fa..eff4bbc74 100644 --- a/examples/src/main/java/picocli/examples/model/ResultHandlerWithReturnValue.java +++ b/examples/src/main/java/picocli/examples/model/ResultHandlerWithReturnValue.java @@ -42,8 +42,8 @@ public static void main(final String[] args) { .description("The files to process").build()); CommandLine commandLine = new CommandLine(spec); - class Handler extends AbstractParseResultHandler { - public Void handle(ParseResult pr) { + class Handler extends AbstractParseResultHandler { + public Integer handle(ParseResult pr) { int count = pr.optionValue('c', 1); List files = pr.positionalValue(pr.positionalParams().get(0), Collections.emptyList()); for (File f : files) { @@ -51,11 +51,11 @@ public Void handle(ParseResult pr) { System.out.println(i + " " + f.getName()); } } - return null; + return files.size(); } protected Handler self() { return this; } } - commandLine.parseWithHandler(new Handler(), args); + int processed = commandLine.parseWithHandler(new Handler(), args); } } diff --git a/examples/src/main/java/picocli/examples/model/SimpleResultHandlerWithoutReturnValue.java b/examples/src/main/java/picocli/examples/model/SimpleResultHandlerWithoutReturnValue.java deleted file mode 100644 index a94f1bd01..000000000 --- a/examples/src/main/java/picocli/examples/model/SimpleResultHandlerWithoutReturnValue.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - Copyright 2017 Remko Popma - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - */ -package picocli.examples.model; - -import picocli.CommandLine; -import picocli.CommandLine.AbstractSimpleParseResultHandler; -import picocli.CommandLine.DefaultExceptionHandler; -import picocli.CommandLine.Model.CommandSpec; -import picocli.CommandLine.Model.OptionSpec; -import picocli.CommandLine.Model.PositionalParamSpec; -import picocli.CommandLine.ParseResult; - -import java.io.File; -import java.util.Collections; -import java.util.List; - -public class SimpleResultHandlerWithoutReturnValue { - public static void main(final String[] args) { - - CommandSpec spec = CommandSpec.create(); - spec.mixinStandardHelpOptions(true); - spec.addOption(OptionSpec.builder("-c", "--count") - .paramLabel("COUNT") - .type(int.class) - .description("number of times to execute").build()); - spec.addPositional(PositionalParamSpec.builder() - .paramLabel("FILES") - .type(List.class) - .auxiliaryTypes(File.class) - .description("The files to process").build()); - CommandLine commandLine = new CommandLine(spec); - - commandLine.parseWithSimpleHandlers(new AbstractSimpleParseResultHandler() { - public void handle(ParseResult pr) { - int count = pr.optionValue('c', 1); - List files = pr.positionalValue(0, Collections.emptyList()); - for (int i = 0; i < count; i++) { - for (File f : files) { - System.out.printf("%d: %s%n", i, f); - } - } - } - }.useOut(System.out).andExit(123), new DefaultExceptionHandler().andExit(567), args); - } -} diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index 7da1caf1d..5cd371553 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -849,45 +849,6 @@ public R handleParseResult(ParseResult parseResult) throws ExecutionException { */ protected abstract R handle(ParseResult parseResult) throws ExecutionException; } - /** Command line parse result handler that does not return a result. Prints help if requested, and otherwise calls - * {@link #handle(CommandLine.ParseResult)} with the parse result. Facilitates implementation of the {@link IParseResultHandler2} interface. - *

An example subclass can look like this:

- *
{@code
-     * class MyResultHandler extends AbstractSimpleParseResultHandler {
-     *     protected void handle(ParseResult parseResult) throws ExecutionException { ... }
-     * }
-     * }
- * @since 3.0 */ - public abstract static class AbstractSimpleParseResultHandler extends AbstractHandler implements IParseResultHandler2 { - /** 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 specified return value - * @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 Void handleParseResult(ParseResult parseResult) throws ExecutionException { - if (printHelpIfRequested(parseResult.asCommandLineList(), out(), err(), ansi())) { - return returnResultOrExit(null); - } - handle(parseResult); - return returnResultOrExit(null); - } - /** Processes the specified {@code ParseResult}. - * Implementations are responsible for catching any exceptions thrown in the {@code handle} method, and - * rethrowing an {@code ExecutionException} that details the problem and captures the offending {@code CommandLine} object. - * - * @param parseResult the {@code ParseResult} that resulted from successfully parsing the command line arguments - * @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 - */ - protected abstract void handle(ParseResult parseResult) throws ExecutionException; - @Override protected AbstractSimpleParseResultHandler self() { return this; } - } - /** * Command line parse result handler that prints help if requested, and otherwise executes the top-level * {@code Runnable} or {@code Callable} command. @@ -1098,26 +1059,6 @@ protected List handle(ParseResult parseResult) throws ExecutionException public R parseWithHandler(IParseResultHandler2 handler, String[] args) { return parseWithHandlers(handler, new DefaultExceptionHandler(), args); } - /** - * Calls {@link #parseWithSimpleHandlers(AbstractSimpleParseResultHandler, IExceptionHandler2, String...)} with - * a new {@link DefaultExceptionHandler} in addition to the specified parse result handler and the specified command line arguments. - *

Calling this method roughly expands to:

- *
{@code
-     * try {
-     *     ParseResult parseResult = parseArgs(args);
-     *     handler.handleParseResult(parseResult);
-     * } catch (ParameterException ex) {
-     *     new DefaultExceptionHandler().handleParseException(ex, null, (String[]) args);
-     * }
-     * }
- * @param handler the function that will handle the result of successfully parsing the command line arguments - * @param args the command line arguments - * @throws ExecutionException if the command line arguments were parsed successfully but a problem occurred while processing the - * parse results; use {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed - * @since 3.0 */ - public void parseWithSimpleHandler(AbstractSimpleParseResultHandler handler, String... args) { - parseWithSimpleHandlers(handler, new DefaultExceptionHandler(), args); - } /** @deprecated use {@link #parseWithHandlers(IParseResultHandler2, IExceptionHandler2, String...)} instead * @since 2.0 */ @@ -1184,42 +1125,6 @@ public R parseWithHandlers(IParseResultHandler2 handler, IExceptionHandle return exceptionHandler.handleExecutionException(ex, parseResult); } } - /** - * Tries to {@linkplain #parseArgs(String...) parse} the specified command line arguments, and if successful, delegates - * the processing of the resulting {@code ParseResult} object to the specified {@linkplain AbstractSimpleParseResultHandler handler}. - * If the command line arguments were invalid, the {@code ParameterException} thrown from the {@code parse} method - * is caught and passed to the specified {@link IExceptionHandler2}. - *

Calling this method roughly expands to:

- *
-     * ParseResult parseResult = null;
-     * try {
-     *     parseResult = parseArgs(args);
-     *     handleParseResult(parseResult);
-     * } catch (ParameterException ex) {
-     *     exceptionHandler.handleParseException(ex, null, (String[]) args);
-     * } catch (ExecutionException ex) {
-     *     exceptionHandler.handleExecutionException(ex, null, parseResult);
-     * }
-     * 
- * - * @param handler the function that will handle the result of successfully parsing the command line arguments - * @param exceptionHandler the function that can handle the {@code ParameterException} thrown when the command line arguments are invalid - * @param args the command line arguments - * @throws ExecutionException if the command line arguments were parsed successfully but a problem occurred while processing the parse - * result {@code ParseResult} object; use {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed - * @see DefaultExceptionHandler - * @since 3.0 */ - public void parseWithSimpleHandlers(AbstractSimpleParseResultHandler handler, IExceptionHandler2 exceptionHandler, String... args) { - ParseResult parseResult = null; - try { - parseResult = parseArgs(args); - handler.handleParseResult(parseResult); - } catch (ParameterException ex) { - exceptionHandler.handleParseException(ex, (String[]) args); - } catch (ExecutionException ex) { - exceptionHandler.handleExecutionException(ex, parseResult); - } - } /** * Equivalent to {@code new CommandLine(command).usage(out)}. See {@link #usage(PrintStream)} for details. * @param command the object annotated with {@link Command}, {@link Option} and {@link Parameters}