Skip to content

Positional parameters bug with late-resolved arity variable #2060

@daisukeoto

Description

@daisukeoto

Observed behaviour

For positional parameters, defining arity with a variable, like this: @Parameters(arity = "${xxx:-1..*}") has a bug:
regardless of the value of the system property xxx, the positional parameter is never mandatory/required.

Expected behaviour

if system property xxx is not defined, the default arity of 1..* means that at least one value is required. But this is not enforced.

How to reproduce

@Test(expected = CommandLine.MissingParameterException.class)
public void testRequiredPositionalWithUndefinedVariable() {
    class Cmd {
        @Parameters(arity = "${xxx1:-1..*}") List<String> positional;
    }
    new CommandLine(new Cmd()).parseArgs(); // should fail with missing param but doesn't
}

Analysis:

ArgSpec.required is derived from arity.min > 0, without considering whether the arity Range object has unresolved variables.
Any variables in the ArgSpec's arity are not resolved until the ArgSpec is added to its CommandSpec.
At this point, the ArgSpec's arity is replaced by a new Range object with valid values, but the ArgSpec's required attribute is not updated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions