@@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2525import java .util .LinkedList ;
2626import java .util .List ;
2727import java .util .Properties ;
28+ import java .util .function .Supplier ;
2829
2930/**
3031 * Represents list of arguments parsed against a {@link Options} descriptor.
@@ -254,6 +255,18 @@ public String getOptionValue(final char opt) {
254255 * @return Value of the argument if option is set, and has an argument, otherwise {@code defaultValue}.
255256 */
256257 public String getOptionValue (final char opt , final String defaultValue ) {
258+ return getOptionValue (String .valueOf (opt ), () -> defaultValue );
259+ }
260+
261+ /**
262+ * Gets the argument, if any, of an option.
263+ *
264+ * @param opt character name of the option
265+ * @param defaultValue is a supplier for the default value to be returned if the option is not specified.
266+ * @return Value of the argument if option is set, and has an argument, otherwise {@code defaultValue}.
267+ * @since 1.7.0
268+ */
269+ public String getOptionValue (final char opt , final Supplier <String > defaultValue ) {
257270 return getOptionValue (String .valueOf (opt ), defaultValue );
258271 }
259272
@@ -281,10 +294,22 @@ public String getOptionValue(final Option option) {
281294 * @since 1.5.0
282295 */
283296 public String getOptionValue (final Option option , final String defaultValue ) {
284- final String answer = getOptionValue (option );
285- return answer != null ? answer : defaultValue ;
297+ return getOptionValue (option , () -> defaultValue );
286298 }
287299
300+ /**
301+ * Gets the first argument, if any, of an option.
302+ *
303+ * @param option name of the option.
304+ * @param defaultValue is a supplier for the default value to be returned if the option is not specified.
305+ * @return Value of the argument if option is set, and has an argument, otherwise {@code defaultValue}.
306+ * @since 1.7.0
307+ */
308+ public String getOptionValue (final Option option , final Supplier <String > defaultValue ) {
309+ final String answer = getOptionValue (option );
310+ return answer != null ? answer : defaultValue .get ();
311+ }
312+
288313 /**
289314 * Gets the first argument, if any, of this option.
290315 *
@@ -303,9 +328,22 @@ public String getOptionValue(final String opt) {
303328 * @return Value of the argument if option is set, and has an argument, otherwise {@code defaultValue}.
304329 */
305330 public String getOptionValue (final String opt , final String defaultValue ) {
331+ return getOptionValue (resolveOption (opt ), () -> defaultValue );
332+ }
333+
334+ /**
335+ * Gets the first argument, if any, of an option.
336+ *
337+ * @param opt name of the option.
338+ * @param defaultValue is a supplier for the default value to be returned if the option is not specified.
339+ * @return Value of the argument if option is set, and has an argument, otherwise {@code defaultValue}.
340+ * @since 1.7.0
341+ */
342+ public String getOptionValue (final String opt , final Supplier <String > defaultValue ) {
306343 return getOptionValue (resolveOption (opt ), defaultValue );
307344 }
308345
346+
309347 /**
310348 * Gets the array of values, if any, of an option.
311349 *
@@ -349,46 +387,97 @@ public String[] getOptionValues(final String opt) {
349387 * Gets a version of this {@code Option} converted to a particular type.
350388 *
351389 * @param opt the name of the option.
390+ * @param <T> The return type for the method.
352391 * @return the value parsed into a particular object.
353392 * @throws ParseException if there are problems turning the option value into the desired type
354393 * @see PatternOptionBuilder
355394 * @since 1.5.0
356395 */
357- public Object getParsedOptionValue (final char opt ) throws ParseException {
396+ public < T > T getParsedOptionValue (final char opt ) throws ParseException {
358397 return getParsedOptionValue (String .valueOf (opt ));
359398 }
360399
361400 /**
362401 * Gets a version of this {@code Option} converted to a particular type.
363402 *
364403 * @param option the name of the option.
404+ * @param <T> The return type for the method.
365405 * @return the value parsed into a particular object.
366406 * @throws ParseException if there are problems turning the option value into the desired type
367407 * @see PatternOptionBuilder
368408 * @since 1.5.0
369409 */
370- public Object getParsedOptionValue (final Option option ) throws ParseException {
410+ public <T > T getParsedOptionValue (final Option option ) throws ParseException {
411+ return getParsedOptionValue (option , null );
412+ }
413+
414+ /**
415+ * Gets a version of this {@code Option} converted to a particular type.
416+ *
417+ * @param opt the name of the option.
418+ * @param <T> The return type for the method.
419+ * @return the value parsed into a particular object.
420+ * @throws ParseException if there are problems turning the option value into the desired type
421+ * @see PatternOptionBuilder
422+ * @since 1.2
423+ */
424+ public <T > T getParsedOptionValue (final String opt ) throws ParseException {
425+ return getParsedOptionValue (resolveOption (opt ));
426+ }
427+
428+ /**
429+ * Gets a version of this {@code Option} converted to a particular type.
430+ *
431+ * @param opt the name of the option.
432+ * @param defaultValue the default value to return if opt is not set.
433+ * @param <T> The return type for the method.
434+ * @return the value parsed into a particular object.
435+ * @throws ParseException if there are problems turning the option value into the desired type
436+ * @see PatternOptionBuilder
437+ * @since 1.7.0
438+ */
439+ public <T > T getParsedOptionValue (final char opt , final T defaultValue ) throws ParseException {
440+ return getParsedOptionValue (String .valueOf (opt ), defaultValue );
441+ }
442+
443+ /**
444+ * Gets a version of this {@code Option} converted to a particular type.
445+ *
446+ * @param option the name of the option.
447+ * @param defaultValue the default value to return if opt is not set.
448+ * @param <T> The return type for the method.
449+ * @return the value parsed into a particular object.
450+ * @throws ParseException if there are problems turning the option value into the desired type
451+ * @see PatternOptionBuilder
452+ * @since 1.7.0
453+ */
454+ @ SuppressWarnings ("unchecked" )
455+ public <T > T getParsedOptionValue (final Option option , final T defaultValue ) throws ParseException {
371456 if (option == null ) {
372457 return null ;
373458 }
374459 final String res = getOptionValue (option );
375- if (res == null ) {
376- return null ;
460+
461+ try {
462+ return res == null ? defaultValue : (T ) option .getConverter ().apply (res );
463+ } catch (Exception e ) {
464+ throw ParseException .wrap (e );
377465 }
378- return TypeHandler .createValue (res , option .getType ());
379466 }
380467
381468 /**
382469 * Gets a version of this {@code Option} converted to a particular type.
383470 *
384471 * @param opt the name of the option.
472+ * @param defaultValue the default value to return if opt is not set.
473+ * @param <T> The return type for the method.
385474 * @return the value parsed into a particular object.
386475 * @throws ParseException if there are problems turning the option value into the desired type
387476 * @see PatternOptionBuilder
388- * @since 1.2
477+ * @since 1.7.0
389478 */
390- public Object getParsedOptionValue (final String opt ) throws ParseException {
391- return getParsedOptionValue (resolveOption (opt ));
479+ public < T > T getParsedOptionValue (final String opt , final T defaultValue ) throws ParseException {
480+ return getParsedOptionValue (resolveOption (opt ), defaultValue );
392481 }
393482
394483 /**
0 commit comments