Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/org/apache/commons/cli/DefaultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,10 @@ private void handleShortAndLongOption(final String hyphenToken) throws ParseExce
// equal sign found (-xxx=yyy)
final String opt = token.substring(0, pos);
final String value = token.substring(pos + 1);
if (opt.length() == 1) {
// -S=V
// Prefer an exact short-option match (including multi-character short opts; CLI-300)
// before falling through to Java-property or long-option handling.
if (options.hasShortOption(opt)) {
// -S=V or -short=V
final Option option = options.getOption(opt);
if (option != null && option.acceptsArg()) {
handleOption(option);
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/apache/commons/cli/DefaultParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,30 @@ void testParseSkipNonHappyPath() throws ParseException {
assertTrue(e.getMessage().contains("-d"));
}

/**
* CLI-300: multi-character short options must accept {@code -opt=value} the same way
* single-character short options do.
*/
@Test
void testMultiCharShortOptionWithEqual() throws Exception {
final Options options = new Options();
options.addOption(Option.builder("foo").hasArg().build());
final CommandLine cmdLine = parser.parse(options, new String[] {"-foo=bar"});
assertEquals("bar", cmdLine.getOptionValue("foo"));
}

/**
* CLI-300: multi-character short option without an equals sign already worked via
* {@code hasShortOption}; keep a paired regression for the non-equals form.
*/
@Test
void testMultiCharShortOptionWithoutEqual() throws Exception {
final Options options = new Options();
options.addOption(Option.builder("foo").hasArg().build());
final CommandLine cmdLine = parser.parse(options, new String[] {"-foo", "bar"});
assertEquals("bar", cmdLine.getOptionValue("foo"));
}

@Override
@Test
@Disabled("Test case handled in the parameterized tests as \"DEFAULT behavior\"")
Expand Down