Skip to content

Commit ac2a1c8

Browse files
committed
CLI-265: Optional argument picking up next regular option as its argument. Extend fix to address problems identified by Martin Sandiford who also submitted the extended patch.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1759745 13f79535-47bb-0310-9956-ffa450edef68
1 parent 5764b62 commit ac2a1c8

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<body>
2424

2525
<release version="1.4" date="tba" description="tba">
26-
<action type="fix" dev="britter" issue="CLI-265">
26+
<action type="fix" dev="britter" issue="CLI-265" due-to="Martin Sandiford">
2727
Optional argument picking up next regular option as its argument
2828
</action>
2929
<action type="add" dev="britter" issue="CLI-267" due-to="Ricardo Ribeiro">

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,12 @@ private boolean isShortOption(String token)
307307
// remove leading "-" and "=value"
308308
int pos = token.indexOf("=");
309309
String optName = pos == -1 ? token.substring(1) : token.substring(1, pos);
310-
return options.hasShortOption(optName);
310+
if (options.hasShortOption(optName))
311+
{
312+
return true;
313+
}
314+
// check for several concatenated short options
315+
return optName.length() > 0 && options.hasShortOption(String.valueOf(optName.charAt(0)));
311316
}
312317

313318
/**

src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.junit.Assert.assertEquals;
2828
import static org.junit.Assert.assertFalse;
2929
import static org.junit.Assert.assertNotEquals;
30+
import static org.junit.Assert.assertNull;
3031
import static org.junit.Assert.assertTrue;
3132

3233
/**
@@ -43,10 +44,12 @@ public class BugCLI265Test {
4344
public void setUp() throws Exception {
4445
parser = new DefaultParser();
4546

46-
Option TYPE1 = Option.builder("t1").hasArg().numberOfArgs(1).optionalArg(true).argName("t1_path").build();
47-
Option LAST = Option.builder("last").hasArg(false).build();
47+
Option optionT1 = Option.builder("t1").hasArg().numberOfArgs(1).optionalArg(true).argName("t1_path").build();
48+
Option optionA = Option.builder("a").hasArg(false).build();
49+
Option optionB = Option.builder("b").hasArg(false).build();
50+
Option optionLast = Option.builder("last").hasArg(false).build();
4851

49-
options = new Options().addOption(TYPE1).addOption(LAST);
52+
options = new Options().addOption(optionT1).addOption(optionA).addOption(optionB).addOption(optionLast);
5053
}
5154

5255
@Test
@@ -70,4 +73,16 @@ public void shouldParseShortOptionWithoutValue() throws Exception {
7073
assertTrue("Second option has not been detected", commandLine.hasOption("last"));
7174
}
7275

76+
@Test
77+
public void shouldParseConcatenatedShortOptions() throws Exception {
78+
String[] concatenatedShortOptions = new String[] { "-t1", "-ab" };
79+
80+
final CommandLine commandLine = parser.parse(options, concatenatedShortOptions);
81+
82+
assertTrue(commandLine.hasOption("t1"));
83+
assertNull(commandLine.getOptionValue("t1"));
84+
assertTrue(commandLine.hasOption("a"));
85+
assertTrue(commandLine.hasOption("b"));
86+
assertFalse(commandLine.hasOption("last"));
87+
}
7388
}

0 commit comments

Comments
 (0)