@@ -351,8 +351,9 @@ public String[] getOptionValues(final String opt) {
351351 * @throws ParseException if there are problems turning the option value into the desired type
352352 * @see PatternOptionBuilder
353353 * @since 1.5.0
354+ * @param <T> The return type for the method.
354355 */
355- public Object getParsedOptionValue (final char opt ) throws ParseException {
356+ public < T > T getParsedOptionValue (final char opt ) throws ParseException {
356357 return getParsedOptionValue (String .valueOf (opt ));
357358 }
358359
@@ -364,29 +365,77 @@ public Object getParsedOptionValue(final char opt) throws ParseException {
364365 * @throws ParseException if there are problems turning the option value into the desired type
365366 * @see PatternOptionBuilder
366367 * @since 1.5.0
368+ * @param <T> The return type for the method.
367369 */
368- public Object getParsedOptionValue (final Option option ) throws ParseException {
370+ public <T > T getParsedOptionValue (final Option option ) throws ParseException {
371+ return getParsedOptionValue (option , null );
372+ }
373+
374+ /**
375+ * Gets a version of this {@code Option} converted to a particular type.
376+ *
377+ * @param opt the name of the option.
378+ * @return the value parsed into a particular object.
379+ * @throws ParseException if there are problems turning the option value into the desired type
380+ * @see PatternOptionBuilder
381+ * @since 1.2
382+ * @param <T> The return type for the method.
383+ */
384+ public <T > T getParsedOptionValue (final String opt ) throws ParseException {
385+ return getParsedOptionValue (resolveOption (opt ));
386+ }
387+
388+ /**
389+ * Gets a version of this {@code Option} converted to a particular type.
390+ *
391+ * @param opt the name of the option.
392+ * @param defaultValue the default value to return if opt is not set.
393+ * @return the value parsed into a particular object.
394+ * @throws ParseException if there are problems turning the option value into the desired type
395+ * @see PatternOptionBuilder
396+ * @since 1.7
397+ * @param <T> The return type for the method.
398+ */
399+ public <T > T getParsedOptionValue (final char opt , T defaultValue ) throws ParseException {
400+ return getParsedOptionValue (String .valueOf (opt ), defaultValue );
401+ }
402+
403+ /**
404+ * Gets a version of this {@code Option} converted to a particular type.
405+ *
406+ * @param option the name of the option.
407+ * @param defaultValue the default value to return if opt is not set.
408+ * @return the value parsed into a particular object.
409+ * @throws ParseException if there are problems turning the option value into the desired type
410+ * @see PatternOptionBuilder
411+ * @since 1.7
412+ * @param <T> The return type for the method.
413+ */
414+ @ SuppressWarnings ("unchecked" )
415+ public <T > T getParsedOptionValue (final Option option , T defaultValue ) throws ParseException {
369416 if (option == null ) {
370417 return null ;
371418 }
372419 final String res = getOptionValue (option );
373420 if (res == null ) {
374- return null ;
421+ return defaultValue ;
375422 }
376- return TypeHandler .createValue (res , option .getType ());
423+ return ( T ) TypeHandler .createValue (res , ( Class <?>) option .getType ());
377424 }
378425
379426 /**
380427 * Gets a version of this {@code Option} converted to a particular type.
381428 *
382429 * @param opt the name of the option.
430+ * @param defaultValue the default value to return if opt is not set.
383431 * @return the value parsed into a particular object.
384432 * @throws ParseException if there are problems turning the option value into the desired type
385433 * @see PatternOptionBuilder
386- * @since 1.2
434+ * @since 1.7
435+ * @param <T> The return type for the method.
387436 */
388- public Object getParsedOptionValue (final String opt ) throws ParseException {
389- return getParsedOptionValue (resolveOption (opt ));
437+ public < T > T getParsedOptionValue (final String opt , T defaultValue ) throws ParseException {
438+ return getParsedOptionValue (resolveOption (opt ), defaultValue );
390439 }
391440
392441 /**
0 commit comments