@@ -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 *
@@ -423,7 +438,7 @@ public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws P
423438 * @since 1.5.0
424439 */
425440 public <T > T getParsedOptionValue (final Option option ) throws ParseException {
426- return getParsedOptionValue (option , null );
441+ return getParsedOptionValue (option , () -> null );
427442 }
428443
429444 /**
@@ -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 {
442- if (option == null ) {
443- return null ;
444- }
445- final String res = getOptionValue (option );
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 {
472+ final String res = option == null ? null : getOptionValue (option );
446473
447474 try {
448- return res == null ? defaultValue : (T ) option .getConverter ().apply (res );
475+ if (res == null ) {
476+ return defaultValue == null ? null : defaultValue .get ();
477+ }
478+ return (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