Skip to content

Commit

Permalink
[#1042] Bugfix: "wrong number of arguments" exception when using inhe…
Browse files Browse the repository at this point in the history
…rited options with `@Command`-annotated methods
  • Loading branch information
remkop committed May 13, 2020
1 parent eea3f2f commit f9d1dc7
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
29 changes: 29 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
# picocli Release Notes

# <a name="4.3.1"></a> Picocli 4.3.1 (UNRELEASED)
The picocli community is slightly emberrassed to announce picocli 4.3.1.

This release fixes a bug where an `IllegalArgumentException: wrong number of arguments` was thrown when the `@Option(scope = INHERIT)` feature is used in a command that has subcommands defined in `@Command`-annotated methods.


This is the sixty-nineth public release.
Picocli follows [semantic versioning](http://semver.org/).

## <a name="4.3.1-toc"></a> Table of Contents
* [New and noteworthy](#4.3.1-new)
* [Fixed issues](#4.3.1-fixes)
* [Deprecations](#4.3.1-deprecated)
* [Potential breaking changes](#4.3.1-breaking-changes)

## <a name="4.3.1-new"></a> New and Noteworthy


## <a name="4.3.1-fixes"></a> Fixed issues
[#1042] Bugfix: "wrong number of arguments" exception when using inherited options with `@Command`-annotated methods. Thanks to [Garret Wilson](https://github.com/garretwilson) for raising this.

## <a name="4.3.1-deprecated"></a> Deprecations
No features were deprecated in this release.

## <a name="4.3.1-breaking-changes"></a> Potential breaking changes
This release has no breaking changes.



# <a name="4.3.0"></a> Picocli 4.3.0
The picocli community is pleased to announce picocli 4.3.0.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package picocli.examples.inheritedoptions;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import java.util.Arrays;

import static picocli.CommandLine.ScopeType.INHERIT;

@Command(name = "top")
public class Top implements Runnable {

/**
* Enables or disables debug mode, which is disabled by default.
* @param debug The new state of debug mode.
*/
@Option(names = {"--debug", "-d"}, description = "Turns on debug level logging.",
scope = INHERIT)
protected void setDebug(final boolean debug) {
System.out.println("Debug has been set to " + debug);
}

@Override
public void run() {
System.out.println("Running " + this);
}

@Command
void sub() {
System.out.println("Running sub");
}

public static void main(String[] args) {
String[][] input = {
{"--debug"},
{"sub", "--debug"}
};
if (args.length > 0) {
new CommandLine(new Top()).execute(args);
} else {
for (int i = 0; i < input.length; i++) {
System.out.printf("Executing test input %d: %s%n", i, Arrays.toString(input[i]));
new CommandLine(new Top()).execute(input[i]);
}
}
}
}
5 changes: 0 additions & 5 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6424,11 +6424,6 @@ public Set<String> names() {
* @return an immutable list of all options and positional parameters for this command. */
public List<ArgSpec> args() { return Collections.unmodifiableList(args); }
Object[] commandMethodParamValues() {
if (mixins.isEmpty() && groups.isEmpty()) {
Object[] values = new Object[args.size()];
for (int i = 0; i < values.length; i++) { values[i] = args.get(i).getValue(); }
return values;
}
Object[] values = new Object[methodParams.length];
int argIndex = mixins.containsKey(AutoHelpMixin.KEY) ? 2 : 0;
for (int i = 0; i < methodParams.length; i++) {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/picocli/InheritedOptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,31 @@ public void testInheritedRequiredArgsDeepNesting() {
assertEquals("Missing required options and parameters: '-x=<x>', '<p0>', '<p1>'", ex.getMessage());
}
}

@Test
public void testIssue1042InheritedOptionsWithCommandMethods() {
@Command(name = "issue1042")
class Issue1042 implements Runnable {
boolean debug;
boolean run;
boolean subRun;

@Option(names = {"--debug", "-d"}, scope = INHERIT)
protected void setDebug(boolean debug) {
this.debug = debug;
}

public void run() { run = true; }

@Command
void sub() { subRun = true; }
}

Issue1042 bean = new Issue1042();
new CommandLine(bean).execute("sub", "--debug");
assertTrue(bean.debug);
assertTrue(bean.subRun);
assertFalse(bean.run);
}

}

0 comments on commit f9d1dc7

Please sign in to comment.