Skip to content

Commit

Permalink
[#2060] Fix positional parameters bug with late-resolved arity variable
Browse files Browse the repository at this point in the history
Closes #2060
  • Loading branch information
remkop committed Aug 5, 2023
1 parent a13408a commit 7ebcf1a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
34 changes: 33 additions & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
# picocli Release Notes

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

This release includes bugfixes and enhancements.

Many thanks to the picocli community for raising these issues and providing the pull requests to address them!

This is the eighty-forth public release.
Picocli follows [semantic versioning](https://semver.org/).
Artifacts in this release are signed by Remko Popma (6601 E5C0 8DCC BB96).

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

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



## <a name="4.7.5-fixes"></a> Fixed issues
* [#2060] Bugfix: Fix positional parameters bug with late-resolved arity variable. Thanks to [daisukeoto](https://github.com/daisukeoto) for raising this.
* [#2074][#2075] Bugfix: Don't generate auto-complete for hidden attributes in `picocli.shell.jline3.PicoCommand`. Thanks to [clebertsuconic](https://github.com/clebertsuconic) for the pull request.

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

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



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

Expand Down Expand Up @@ -31,7 +64,6 @@ I cheated on the semantic versioning in this release: a public setter method was
Bartosz Spyrko-Smietanko](https://github.com/spyrkob) for the pull request.
* [#2035][#2036] Bugfix: Option "mapFallbackValue" ignored when inherited to subcommand. Thanks to [Dan Ziemba](https://github.com/zman0900) for the pull request.
* [#1848][#2030] Bugfix: fix issue with required options in `ArgGroup` becoming optional when combined with `DefaultValueProvider`. Thanks to [Ruud Senden](https://github.com/rsenden) and [Mike Snowden](https://github.com/wtfacoconut) for the pull request.
* [#2074][#2075] Don't generate auto-complete for hidden attributes in `picocli.shell.jline3.PicoCommand`. Thanks to [clebertsuconic](https://github.com/clebertsuconic) for the pull request.
* [#2020] DEP: Bump step-security/harden-runner from 2.3.0 to 2.4.0
* [#2033] DEP: Bump github/codeql-action from 2.2.12 to 2.3.5
* [#2015] DEP: Bump junit5Version from 5.9.2 to 5.9.3
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6887,6 +6887,9 @@ private CommandSpec addArg(ArgSpec arg) {
arg.commandSpec = this;
if (arg.arity().isUnresolved()) {
arg.arity = Range.valueOf(interpolator.interpolate(arg.arity().originalValue));
if (arg.isPositional()) { // #2060 fix bug with late-resolved arity variable
arg.required = arg.arity.min > 0; // Builder may have set `required` flag before arity was resolved
}
}
// do this last: arg.required() needs to resolve variables in arg.defaultValue()
if (arg.required() && arg.group() == null && !arg.inherited()) { requiredArgs.add(arg); }
Expand Down Expand Up @@ -9582,7 +9585,7 @@ abstract static class Builder<T extends Builder<T>> {
Builder(Parameters parameters, IAnnotatedElement annotatedElement, IFactory factory) {
this(annotatedElement);
arity = Range.parameterArity(annotatedElement);
required = arity.min > 0;
required = arity.min > 0; //but arity may still be unresolved...

// method parameters may be positional parameters without @Parameters annotation
if (parameters == null) {
Expand Down
39 changes: 38 additions & 1 deletion src/test/java/picocli/InterpolatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import org.junit.rules.TestRule;
import picocli.CommandLine.Model.Interpolator;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Parameters;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
Expand Down Expand Up @@ -71,7 +73,7 @@ public void interpolateSystemPropertyDefault() {
System.clearProperty("myProp");
assertEquals(expected, interpolator.interpolate(original));
}

@Test
public void issue676interpolateReturnsNullIfNotFound() {
CommandSpec hierarchy = createTestSpec();
Expand Down Expand Up @@ -251,5 +253,40 @@ public Enumeration<String> getKeys() {
}
};
}

@Test(expected = CommandLine.MissingParameterException.class)
public void testRequiredPositionalWithoutVariable() {
class Cmd {
@Parameters(arity = "1..*") List<String> positional;
}
new CommandLine(new Cmd()).parseArgs(); // missing params
}

@Test(expected = CommandLine.MissingParameterException.class)
public void testRequiredPositionalWithUndefinedVariable() {
class Cmd {
@Parameters(arity = "${xxx1:-1..*}") List<String> positional;
}
new CommandLine(new Cmd()).parseArgs(); // missing params
System.out.println();
}

@Test(expected = CommandLine.MissingParameterException.class)
public void testRequiredPositionalWithDefinedVariable() {
class Cmd {
@Parameters(arity = "${xxx2:-1..*}") List<String> positional;
}
System.setProperty("xxx2", "2..3");
new CommandLine(new Cmd()).parseArgs(); // missing params
}

@Test()
public void testOptionalPositionalWithDefinedVariable() {
class Cmd {
@Parameters(arity = "${xxx3:-1..*}") List<String> positional;
}
System.setProperty("xxx3", "0..*");
new CommandLine(new Cmd()).parseArgs();
}
}

0 comments on commit 7ebcf1a

Please sign in to comment.