Skip to content

Commit 3795588

Browse files
committed
CLI-313: Fixed StringIndexOutOfBoundsException for Java-like property token
1 parent 3790cec commit 3795588

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ private boolean isArgument(final String token) {
580580
* Tests if the specified token is a Java-like property (-Dkey=value).
581581
*/
582582
private boolean isJavaProperty(final String token) {
583-
final String opt = token.substring(0, 1);
583+
final String opt = token.isEmpty() ? null : token.substring(0, 1);
584584
final Option option = options.getOption(opt);
585585

586586
return option != null && (option.getArgs() >= 2 || option.getArgs() == Option.UNLIMITED_VALUES);

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,5 +1020,12 @@ public void testWithRequiredOption() throws Exception {
10201020
assertEquals("Confirm arg of -b", "file", cl.getOptionValue("b"));
10211021
assertTrue("Confirm NO of extra args", cl.getArgList().isEmpty());
10221022
}
1023-
1023+
1024+
@Test(expected = UnrecognizedOptionException.class)
1025+
public void testAmbiguousArgParsing() throws Exception {
1026+
final String[] args = {"-=-"};
1027+
final Options options = new Options();
1028+
1029+
final CommandLine cl = parser.parse(options, args);
1030+
}
10241031
}

0 commit comments

Comments
 (0)