Skip to content

Commit 32f148e

Browse files
committed
Added Supplier<T> defaults for getParsedOptionValue
1 parent bcfcdde commit 32f148e

1 file changed

Lines changed: 47 additions & 2 deletions

File tree

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,21 @@ public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws P
412412
return getParsedOptionValue(String.valueOf(opt), defaultValue);
413413
}
414414

415+
/**
416+
* Gets a version of this {@code Option} converted to a particular type.
417+
*
418+
* @param opt the name of the option.
419+
* @param defaultValue the default value to return if opt is not set.
420+
* @param <T> The return type for the method.
421+
* @return the value parsed into a particular object.
422+
* @throws ParseException if there are problems turning the option value into the desired type
423+
* @see PatternOptionBuilder
424+
* @since 1.7.0
425+
*/
426+
public <T> T getParsedOptionValue(final char opt, final Supplier<T> defaultValue) throws ParseException {
427+
return getParsedOptionValue(String.valueOf(opt), defaultValue);
428+
}
429+
415430
/**
416431
* Gets a version of this {@code Option} converted to a particular type.
417432
*
@@ -437,15 +452,30 @@ public <T> T getParsedOptionValue(final Option option) throws ParseException {
437452
* @see PatternOptionBuilder
438453
* @since 1.7.0
439454
*/
440-
@SuppressWarnings("unchecked")
441455
public <T> T getParsedOptionValue(final Option option, final T defaultValue) throws ParseException {
456+
return getParsedOptionValue(option, () -> defaultValue);
457+
}
458+
459+
/**
460+
* Gets a version of this {@code Option} converted to a particular type.
461+
*
462+
* @param option the name of the option.
463+
* @param defaultValue the default value to return if opt is not set.
464+
* @param <T> The return type for the method.
465+
* @return the value parsed into a particular object.
466+
* @throws ParseException if there are problems turning the option value into the desired type
467+
* @see PatternOptionBuilder
468+
* @since 1.7.0
469+
*/
470+
@SuppressWarnings("unchecked")
471+
public <T> T getParsedOptionValue(final Option option, final Supplier<T> defaultValue) throws ParseException {
442472
if (option == null) {
443473
return null;
444474
}
445475
final String res = getOptionValue(option);
446476

447477
try {
448-
return res == null ? defaultValue : (T) option.getConverter().apply(res);
478+
return res == null ? defaultValue.get() : (T) option.getConverter().apply(res);
449479
} catch (final Throwable e) {
450480
throw ParseException.wrap(e);
451481
}
@@ -480,6 +510,21 @@ public <T> T getParsedOptionValue(final String opt, final T defaultValue) throws
480510
return getParsedOptionValue(resolveOption(opt), defaultValue);
481511
}
482512

513+
/**
514+
* Gets a version of this {@code Option} converted to a particular type.
515+
*
516+
* @param opt the name of the option.
517+
* @param defaultValue the default value to return if opt is not set.
518+
* @param <T> The return type for the method.
519+
* @return the value parsed into a particular object.
520+
* @throws ParseException if there are problems turning the option value into the desired type
521+
* @see PatternOptionBuilder
522+
* @since 1.7.0
523+
*/
524+
public <T> T getParsedOptionValue(final String opt, final Supplier<T> defaultValue) throws ParseException {
525+
return getParsedOptionValue(resolveOption(opt), defaultValue);
526+
}
527+
483528
/**
484529
* jkeyes - commented out until it is implemented properly
485530
* <p>

0 commit comments

Comments
 (0)