Skip to content

Commit

Permalink
[#1303] Bugfix: Prevent `IllegalArgumentException: argument type mism…
Browse files Browse the repository at this point in the history
…atch` error in method subcommands with inherited mixed-in standard help options

Closes #1303
  • Loading branch information
remkop committed Jan 11, 2021
1 parent 07c5971 commit 77cb05d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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.
* [#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
4 changes: 3 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6937,7 +6937,8 @@ public Set<String> names() {
public List<ArgSpec> args() { return Collections.unmodifiableList(args); }
Object[] commandMethodParamValues() {
Object[] values = new Object[methodParams.length];
int argIndex = mixins.containsKey(AutoHelpMixin.KEY) ? 2 : 0;
CommandSpec autoHelpMixin = mixins.get(AutoHelpMixin.KEY);
int argIndex = autoHelpMixin == null || autoHelpMixin.inherited() ? 0 : 2;
for (int i = 0; i < methodParams.length; i++) {
if (methodParams[i].isAnnotationPresent(Mixin.class)) {
String name = methodParams[i].getAnnotation(Mixin.class).name();
Expand Down Expand Up @@ -7156,6 +7157,7 @@ public CommandSpec negatableOptionTransformer(INegatableOptionTransformer newVal
public CommandSpec mixinStandardHelpOptions(boolean newValue) {
if (newValue) {
CommandSpec mixin = CommandSpec.forAnnotatedObject(new AutoHelpMixin(), new DefaultFactory());
mixin.inherited = this.inherited();
addMixin(AutoHelpMixin.KEY, mixin);
} else {
CommandSpec helpMixin = mixins.remove(AutoHelpMixin.KEY);
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/picocli/InheritedOptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,22 @@ public void testIssue1159ParseResult() {
assertFalse("Not matched on the parent command",
parseResult.hasMatchedOption("--xxx-yyy"));
}


@Test
public void testIssue1303InheritCommandWithMethodSubcommands() {
@Command(scope = INHERIT, mixinStandardHelpOptions = true)
class InheritApp {
int subCalled;

@Command()
void sub(@Option(names = "-foo") int foo) {
subCalled = foo;
//System.out.printf("Foo: %d", foo);
}
}
InheritApp app = new InheritApp();
new CommandLine(app).execute("sub", "-foo", "42" );
assertEquals(42, app.subCalled);
}
}

0 comments on commit 77cb05d

Please sign in to comment.