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
14 changes: 7 additions & 7 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,16 @@ 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));
}
} else if (!("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {
if (!opt.hasArg() && !("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);
// 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() && copy.isValuesEmpty()) {
copy.processValue(stripLeadingAndTrailingQuotesDefaultOff(value));
}
handleOption(copy);
currentOption = null;
}
}
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/org/apache/commons/cli/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,21 +284,22 @@ 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()) {
try {
opt.processValue(value);
} catch (final RuntimeException exp) { // NOPMD
// if we cannot add the value don't worry about it
}
}
} else if (!("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {
if (!opt.hasArg() && !("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;
}
cmd.addOption(opt);
updateRequiredOptions(opt);
// 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() && copy.isValuesEmpty()) {
try {
copy.processValue(value);
} catch (final RuntimeException exp) { // NOPMD
// if we cannot add the value don't worry about it
}
}
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