Skip to content

Commit

Permalink
[#312] Remove AbstractSimpleParseResultHandler class and `parseWith…
Browse files Browse the repository at this point in the history
…SimpleHandlers` method.

Closes #312.
  • Loading branch information
remkop committed Mar 27, 2018
1 parent a22c68c commit 52e6a91
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 159 deletions.
12 changes: 10 additions & 2 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# picocli Release Notes

# <a name="3.0.0-alpha-2"></a> 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/).

## <a name="3.0.0-alpha-2-fixes"></a> 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.
Expand All @@ -11,12 +19,12 @@
- [#309] Bugfix: Tests were failing on environments that support ANSI colors.

## <a name="3.0.0-alpha-2-deprecated"></a> 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)

## <a name="3.0.0-alpha-2-breaking-changes"></a> 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)

# <a name="3.0.0-alpha-1"></a> Picocli 3.0.0-alpha-1
The picocli community is pleased to announce picocli 3.0.0-alpha-1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ public static void main(final String[] args) {
.description("The files to process").build());
CommandLine commandLine = new CommandLine(spec);

class Handler extends AbstractParseResultHandler<Void> {
public Void handle(ParseResult pr) {
class Handler extends AbstractParseResultHandler<Integer> {
public Integer handle(ParseResult pr) {
int count = pr.optionValue('c', 1);
List<File> files = pr.positionalValue(pr.positionalParams().get(0), Collections.<File>emptyList());
for (File f : files) {
for (int i = 0; i < count; i++) {
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);
}
}

This file was deleted.

95 changes: 0 additions & 95 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>An example subclass can look like this:</p>
* <pre>{@code
* class MyResultHandler extends AbstractSimpleParseResultHandler {
* protected void handle(ParseResult parseResult) throws ExecutionException { ... }
* }
* }</pre>
* @since 3.0 */
public abstract static class AbstractSimpleParseResultHandler extends AbstractHandler<Void, AbstractSimpleParseResultHandler> implements IParseResultHandler2<Void> {
/** 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.
Expand Down Expand Up @@ -1098,26 +1059,6 @@ protected List<Object> handle(ParseResult parseResult) throws ExecutionException
public <R> R parseWithHandler(IParseResultHandler2<R> handler, String[] args) {
return parseWithHandlers(handler, new DefaultExceptionHandler<R>(), 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.
* <p>Calling this method roughly expands to:</p>
* <pre>{@code
* try {
* ParseResult parseResult = parseArgs(args);
* handler.handleParseResult(parseResult);
* } catch (ParameterException ex) {
* new DefaultExceptionHandler<Void>().handleParseException(ex, null, (String[]) args);
* }
* }</pre>
* @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<Void>(), args);
}

/** @deprecated use {@link #parseWithHandlers(IParseResultHandler2, IExceptionHandler2, String...)} instead
* @since 2.0 */
Expand Down Expand Up @@ -1184,42 +1125,6 @@ public <R> R parseWithHandlers(IParseResultHandler2<R> 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}.
* <p>Calling this method roughly expands to:</p>
* <pre>
* 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);
* }
* </pre>
*
* @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<Void> 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}
Expand Down

0 comments on commit 52e6a91

Please sign in to comment.