Skip to content

Commit fbd0194

Browse files
authored
CLI-320: Awkward behavior of Option.builder() for multiple optional args (apache#206)
1 parent ae71470 commit fbd0194

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

src/main/java/org/apache/commons/cli/Option.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ public Builder option(final String option) throws IllegalArgumentException {
216216
* @return this builder, to allow method chaining
217217
*/
218218
public Builder optionalArg(final boolean optionalArg) {
219-
this.argCount = optionalArg ? 1 : UNINITIALIZED;
219+
if (optionalArg && this.argCount == UNINITIALIZED) {
220+
this.argCount = 1;
221+
}
220222
this.optionalArg = optionalArg;
221223
return this;
222224
}

src/test/java/org/apache/commons/cli/AbstractParserTestCase.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertFalse;
2323
import static org.junit.Assert.assertNotNull;
24+
import static org.junit.Assert.assertNull;
2425
import static org.junit.Assert.assertTrue;
2526
import static org.junit.Assert.fail;
2627
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -737,6 +738,54 @@ public void testPropertyOverrideValues() throws Exception {
737738
assertFalse(cmd.hasOption("fake"));
738739
}
739740

741+
@Test
742+
public void testOptionalArgsOptionBuilder() throws Exception {
743+
final Options opts = new Options();
744+
opts.addOption(OptionBuilder.hasOptionalArgs(2).create('i'));
745+
final Properties properties = new Properties();
746+
747+
CommandLine cmd = parse(parser, opts, new String[]{"-i"}, properties);
748+
assertTrue(cmd.hasOption("i"));
749+
assertNull(null, cmd.getOptionValues("i"));
750+
751+
cmd = parse(parser, opts, new String[]{"-i", "paper"}, properties);
752+
assertTrue(cmd.hasOption("i"));
753+
assertArrayEquals(new String[]{"paper"}, cmd.getOptionValues("i"));
754+
755+
cmd = parse(parser, opts, new String[]{"-i", "paper", "scissors"}, properties);
756+
assertTrue(cmd.hasOption("i"));
757+
assertArrayEquals(new String[]{"paper", "scissors"}, cmd.getOptionValues("i"));
758+
759+
cmd = parse(parser, opts, new String[]{"-i", "paper", "scissors", "rock"}, properties);
760+
assertTrue(cmd.hasOption("i"));
761+
assertArrayEquals(new String[]{"paper", "scissors"}, cmd.getOptionValues("i"));
762+
assertArrayEquals(new String[]{"rock"}, cmd.getArgs());
763+
}
764+
765+
@Test
766+
public void testOptionalArgsOptionDotBuilder() throws Exception {
767+
final Options opts = new Options();
768+
opts.addOption(Option.builder("i").numberOfArgs(2).optionalArg(true).build());
769+
final Properties properties = new Properties();
770+
771+
CommandLine cmd = parse(parser, opts, new String[]{"-i"}, properties);
772+
assertTrue(cmd.hasOption("i"));
773+
assertNull(null, cmd.getOptionValues("i"));
774+
775+
cmd = parse(parser, opts, new String[]{"-i", "paper"}, properties);
776+
assertTrue(cmd.hasOption("i"));
777+
assertArrayEquals(new String[]{"paper"}, cmd.getOptionValues("i"));
778+
779+
cmd = parse(parser, opts, new String[]{"-i", "paper", "scissors"}, properties);
780+
assertTrue(cmd.hasOption("i"));
781+
assertArrayEquals(new String[]{"paper", "scissors"}, cmd.getOptionValues("i"));
782+
783+
cmd = parse(parser, opts, new String[]{"-i", "paper", "scissors", "rock"}, properties);
784+
assertTrue(cmd.hasOption("i"));
785+
assertArrayEquals(new String[]{"paper", "scissors"}, cmd.getOptionValues("i"));
786+
assertArrayEquals(new String[]{"rock"}, cmd.getArgs());
787+
}
788+
740789
@Test
741790
public void testReuseOptionsTwice() throws Exception {
742791
final Options opts = new Options();

0 commit comments

Comments
 (0)