Skip to content

Commit

Permalink
[remkop#1319] Bugfix: Avoid DuplicateOptionAnnotationsException whe…
Browse files Browse the repository at this point in the history
…n parent has inherited mixed-in help options and the built-in `HelpCommand` subcommand.

Closes remkop#1319
  • Loading branch information
remkop authored and MarkoMackic committed Oct 17, 2021
1 parent ab03e80 commit 1a95bf2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Picocli follows [semantic versioning](http://semver.org/).
* [#1303] Bugfix: Prevent `IllegalArgumentException: argument type mismatch` error in method subcommands with inherited mixed-in standard help options. Thanks to [Andreas Deininger](https://github.com/deining) for raising this.
* [#1300] Bugfix: Avoid spurious warning "Could not set initial value for field boolean" when reusing `CommandLine` with ArgGroup. Thanks to [Yashodhan Ghadge](https://github.com/codexetreme) for raising this.
* [#1316] Bugfix: Avoid `DuplicateOptionAnnotationsException` thrown on `mixinStandardHelpOptions` for subcommands when parent has `scope = INHERIT` by `picocli-codegen` annotation processor. Thanks to [Philippe Charles](https://github.com/charphi) for raising this.
* [#1319] Bugfix: Avoid `DuplicateOptionAnnotationsException` when parent has inherited mixed-in help options and the built-in `HelpCommand` subcommand. Thanks to [Andreas Deininger](https://github.com/deining) for raising this.
* [#1320][#1321] Bugfix/Enhancement: Use system properties `sun.stdout.encoding` and `sun.stderr.encoding` when creating the `PrintWriters` returned by `CommandLine::getOut` and `CommandLine::getErr`. Thanks to [Philippe Charles](https://github.com/charphi) for the investigation and the pull request.
* [#1296] DOC: add Kotlin code samples to user manual; other user manual improvements. Thanks to [Andreas Deininger](https://github.com/deining) for the pull request.
* [#1299] DOC: Link to `IParameterPreprocessor` from `IParameterConsumer` javadoc. Thanks to [Andreas Deininger](https://github.com/deining) for the pull request.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package picocli.annotation.processing.tests;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import org.junit.Test;

import javax.annotation.processing.Processor;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;

public class Issue1319Test {
@Test
public void testIssue1319() {
Processor processor = new AnnotatedCommandSourceGeneratorProcessor();
Compilation compilation =
javac()
.withProcessors(processor)
.compile(JavaFileObjects.forResource(
"picocli/issue1319/InheritHelpApp.java"));

assertThat(compilation).succeeded();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package picocli.issue1319;

import picocli.CommandLine;

@CommandLine.Command(scope = CommandLine.ScopeType.INHERIT
, mixinStandardHelpOptions = true
, subcommands = { CommandLine.HelpCommand.class }
)
class InheritHelpApp {
int subFoo;

@CommandLine.Command()
void sub(@CommandLine.Option(names = "-foo") int foo) {
subFoo = foo;
}
}
8 changes: 6 additions & 2 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -7156,8 +7156,12 @@ public CommandSpec negatableOptionTransformer(INegatableOptionTransformer newVal
* @see Command#mixinStandardHelpOptions() */
public CommandSpec mixinStandardHelpOptions(boolean newValue) {
if (newValue) {
if (!mixins.containsKey(AutoHelpMixin.KEY)) { // #1316 avoid DuplicateOptionAnnotationsException
CommandSpec mixin = CommandSpec.forAnnotatedObject(new AutoHelpMixin(), new DefaultFactory());
CommandSpec mixin = CommandSpec.forAnnotatedObject(new AutoHelpMixin(), new DefaultFactory());
boolean overlap = false;
for (String key : mixin.optionsMap().keySet()) {
if (optionsMap().containsKey(key)) { overlap = true; break; }
}
if (!overlap) { // #1316, 1319 avoid DuplicateOptionAnnotationsException
mixin.inherited = this.inherited();
addMixin(AutoHelpMixin.KEY, mixin);
}
Expand Down
1 change: 0 additions & 1 deletion src/test/java/picocli/InheritedOptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,6 @@ class Example { }
new CommandLine(new Example()); // succeeds without error
}

@Ignore("Needs fix for #1319")
@Test
public void testIssue1319() {
@Command(scope = CommandLine.ScopeType.INHERIT
Expand Down

0 comments on commit 1a95bf2

Please sign in to comment.