Skip to content
Open
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
11 changes: 6 additions & 5 deletions src/main/java/org/apache/commons/cli/DefaultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,16 +518,17 @@ private void handleProperties(final Properties properties) throws ParseException
if (!cmd.hasOption(option) && !selected) {
// get the value from the properties
final String value = properties.getProperty(option);

if (opt.hasArg()) {
if (opt.isValuesEmpty()) {
opt.processValue(stripLeadingAndTrailingQuotesDefaultOff(value));
// the Options belong to the caller and outlive this parse, so hold the value on a copy
final Option copy = (Option) opt.clone();
if (copy.hasArg()) {
if (copy.isValuesEmpty()) {
copy.processValue(stripLeadingAndTrailingQuotesDefaultOff(value));
Comment on lines +521 to +525
}
} else if (!("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {
// if the value is not yes, true or 1 then don't add the option to the CommandLine
continue;
}
handleOption(opt);
handleOption(copy);
currentOption = null;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/org/apache/commons/cli/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,12 @@ protected void processProperties(final Properties properties) throws ParseExcept
if (!cmd.hasOption(option) && !selected) {
// get the value from the properties instance
final String value = properties.getProperty(option);
if (opt.hasArg()) {
if (opt.isValuesEmpty()) {
// the Options belong to the caller and outlive this parse, so hold the value on a copy
final Option copy = (Option) opt.clone();
if (copy.hasArg()) {
if (copy.isValuesEmpty()) {
Comment on lines +287 to +290
try {
opt.processValue(value);
copy.processValue(value);
} catch (final RuntimeException exp) { // NOPMD
// if we cannot add the value don't worry about it
}
Expand All @@ -297,8 +299,8 @@ protected void processProperties(final Properties properties) throws ParseExcept
// option to the CommandLine
continue;
}
cmd.addOption(opt);
updateRequiredOptions(opt);
cmd.addOption(copy);
updateRequiredOptions(copy);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/apache/commons/cli/AbstractParserTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,26 @@ void testPropertyOptionRequired() throws Exception {
assertTrue(cmd.hasOption("f"));
}

@Test
void testPropertyOptionReusedOptions() throws Exception {
final Options options = new Options();
options.addOption(Option.builder("k").hasArg().get());

final Properties first = new Properties();
first.setProperty("k", "one");
final CommandLine firstCmd = parse(parser, options, null, first);
assertEquals("one", firstCmd.getOptionValue('k'));

final Properties second = new Properties();
second.setProperty("k", "two");
final CommandLine secondCmd = parse(parser, options, null, second);
assertEquals("two", secondCmd.getOptionValue('k'));

// the second parse must not reach back into the first result, and the Options must be left alone
assertEquals("one", firstCmd.getOptionValue('k'));
assertNull(options.getOption("k").getValue());
}

@Test
void testPropertyOptionSingularValue() throws Exception {
final Options options = new Options();
Expand Down
Loading