Skip to content

Commit

Permalink
Allow acceptUnknownOption with variableArity (#555)
Browse files Browse the repository at this point in the history
* Allow acceptUnknownOption with variableArity

VariableArity makes it difficult to decide whether a
token belongs to a preceding variableArity Parameter
or is a new unknown parameter.

This commits makes a variableArity Parameter consume
all tokens until it encounters a known option.

Fixes: #377

* Change test to use '=' separator only for new case
  • Loading branch information
hsudhof authored Oct 12, 2023
1 parent a215799 commit fbff072
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,10 @@ private boolean matchArg(String arg, IKey key) {
}

private boolean isOption(String passedArg) {
if (options.acceptUnknownOptions) return true;
return (options.acceptUnknownOptions) || isNamedOption(passedArg);
}

private boolean isNamedOption(String passedArg) {

String arg = options.caseSensitiveOptions ? passedArg : passedArg.toLowerCase();

Expand Down Expand Up @@ -876,7 +879,8 @@ private class DefaultVariableArity implements IVariableArity {
@Override
public int processVariableArity(String optionName, String[] options) {
int i = 0;
while (i < options.length && !isOption(options[i])) {
// For variableArity we consume everything until we hit a known parameter
while (i < options.length && !isNamedOption(options[i])) {
i++;
}
return i;
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/beust/jcommander/VariableArityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,31 @@ public void verifyVariableArity() {
Assert.assertEquals(config.j, Arrays.asList("--compilation_level", "WHITESPACE_ONLY", "--language_in=ECMASCRIPT5", "-bar", "baz", "-faz", "--more-options"));
}

@Parameters(separators = "=")
public static class EqualsModelGenerationConfig extends ModelGenerationConfig {
}

@Test
public void verifyVariableArity_unknownOptions() {
String[] input =
{"-m", "foo", "--seed", "1024", "-c=foo", "bar", "-f", "foo", "-o=out.txt", "--extra"};
EqualsModelGenerationConfig config = new EqualsModelGenerationConfig();
JCommander com = new JCommander(config);
com.setProgramName("modelgen");
com.setAcceptUnknownOptions(true);

com.parse(input);

Assert.assertNotEquals(config.seed, 0);
Assert.assertEquals(config.modelMatrixFile, Arrays.asList("foo"));
Assert.assertEquals(config.featureFile, Arrays.asList("foo"));
Assert.assertEquals(config.seed, 1024);
Assert.assertEquals(config.outputFile, "out.txt");
Assert.assertEquals(config.configFile, Arrays.asList("foo", "bar"));
}

public static void main(String[] args) {
new VariableArityTest().verifyVariableArity();
new VariableArityTest().verifyVariableArity_unknownOptions();
}
}

0 comments on commit fbff072

Please sign in to comment.