Skip to content

Commit 51384fc

Browse files
committed
Defer the Option clone until after the no-arg skip check
Signed-off-by: farkhalit rida <farkhlait@sproutxp.com>
1 parent 8bbdbc9 commit 51384fc

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,16 +518,15 @@ private void handleProperties(final Properties properties) throws ParseException
518518
if (!cmd.hasOption(option) && !selected) {
519519
// get the value from the properties
520520
final String value = properties.getProperty(option);
521-
// the Options belong to the caller and outlive this parse, so hold the value on a copy
522-
final Option copy = (Option) opt.clone();
523-
if (copy.hasArg()) {
524-
if (copy.isValuesEmpty()) {
525-
copy.processValue(stripLeadingAndTrailingQuotesDefaultOff(value));
526-
}
527-
} else if (!("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {
521+
if (!opt.hasArg() && !("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {
528522
// if the value is not yes, true or 1 then don't add the option to the CommandLine
529523
continue;
530524
}
525+
// the Options belong to the caller and outlive this parse, so hold the value on a copy
526+
final Option copy = (Option) opt.clone();
527+
if (copy.hasArg() && copy.isValuesEmpty()) {
528+
copy.processValue(stripLeadingAndTrailingQuotesDefaultOff(value));
529+
}
531530
handleOption(copy);
532531
currentOption = null;
533532
}

‎src/main/java/org/apache/commons/cli/Parser.java‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,21 +284,20 @@ protected void processProperties(final Properties properties) throws ParseExcept
284284
if (!cmd.hasOption(option) && !selected) {
285285
// get the value from the properties instance
286286
final String value = properties.getProperty(option);
287-
// the Options belong to the caller and outlive this parse, so hold the value on a copy
288-
final Option copy = (Option) opt.clone();
289-
if (copy.hasArg()) {
290-
if (copy.isValuesEmpty()) {
291-
try {
292-
copy.processValue(value);
293-
} catch (final RuntimeException exp) { // NOPMD
294-
// if we cannot add the value don't worry about it
295-
}
296-
}
297-
} else if (!("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {
287+
if (!opt.hasArg() && !("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {
298288
// if the value is not yes, true or 1 then don't add the
299289
// option to the CommandLine
300290
continue;
301291
}
292+
// the Options belong to the caller and outlive this parse, so hold the value on a copy
293+
final Option copy = (Option) opt.clone();
294+
if (copy.hasArg() && copy.isValuesEmpty()) {
295+
try {
296+
copy.processValue(value);
297+
} catch (final RuntimeException exp) { // NOPMD
298+
// if we cannot add the value don't worry about it
299+
}
300+
}
302301
cmd.addOption(copy);
303302
updateRequiredOptions(copy);
304303
}

0 commit comments

Comments
 (0)