Skip to content

Commit

Permalink
[remkop#1316] Bugfix: Avoid DuplicateOptionAnnotationsException thr…
Browse files Browse the repository at this point in the history
…own on `mixinStandardHelpOptions` for subcommands when parent has `scope = INHERIT` by `picocli-codegen` annotation processor.

Closes remkop#1316
  • Loading branch information
remkop authored and MarkoMackic committed Oct 17, 2021
1 parent e1b5848 commit 735f951
Show file tree
Hide file tree
Showing 5 changed files with 57 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 @@ -21,6 +21,7 @@ Picocli follows [semantic versioning](http://semver.org/).
## <a name="4.6.2-fixes"></a> Fixed issues
* [#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.
* [#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.
* [#1304] DOC: Manual, chapter '17.9 Inherited Command Attributes': added Kotlin version of code sample. 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 Issue1316Test {
@Test
public void testIssue1316() {
Processor processor = new AnnotatedCommandSourceGeneratorProcessor();
Compilation compilation =
javac()
.withProcessors(processor)
.compile(JavaFileObjects.forResource(
"picocli/issue1316/Example.java"));

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

import picocli.CommandLine;
import picocli.AutoComplete;

@CommandLine.Command(
name = "example",
mixinStandardHelpOptions = true,
scope = CommandLine.ScopeType.INHERIT,
subcommands = AutoComplete.GenerateCompletion.class
)
public class Example {
}
8 changes: 5 additions & 3 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -7156,9 +7156,11 @@ public CommandSpec negatableOptionTransformer(INegatableOptionTransformer newVal
* @see Command#mixinStandardHelpOptions() */
public CommandSpec mixinStandardHelpOptions(boolean newValue) {
if (newValue) {
CommandSpec mixin = CommandSpec.forAnnotatedObject(new AutoHelpMixin(), new DefaultFactory());
mixin.inherited = this.inherited();
addMixin(AutoHelpMixin.KEY, mixin);
if (!mixins.containsKey(AutoHelpMixin.KEY)) { // #1316 avoid DuplicateOptionAnnotationsException
CommandSpec mixin = CommandSpec.forAnnotatedObject(new AutoHelpMixin(), new DefaultFactory());
mixin.inherited = this.inherited();
addMixin(AutoHelpMixin.KEY, mixin);
}
} else {
CommandSpec helpMixin = mixins.remove(AutoHelpMixin.KEY);
if (helpMixin != null) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/picocli/InheritedOptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,18 @@ void sub(@Option(names = "-foo") int foo) {
new CommandLine(app).execute("sub", "-foo", "42" );
assertEquals(42, app.subCalled);
}

@Test
public void testIssue1316() {
@CommandLine.Command(
name = "example",
mixinStandardHelpOptions = true,
scope = CommandLine.ScopeType.INHERIT,
subcommands = AutoComplete.GenerateCompletion.class
)
class Example { }

// see also picocli-annotation-processing-tests/src/test/java/picocli/annotation/processing/tests/Issue1316Test.java
new CommandLine(new Example()); // succeeds without error
}
}

0 comments on commit 735f951

Please sign in to comment.