@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1818package org .apache .commons .cli ;
1919
2020import java .io .Serializable ;
21+ import java .lang .reflect .Array ;
2122import java .util .ArrayList ;
2223import java .util .Iterator ;
2324import java .util .LinkedList ;
@@ -503,7 +504,7 @@ public String[] getOptionValues(final String opt) {
503504 *
504505 * @param opt the name of the option.
505506 * @param <T> The return type for the method.
506- * @return the value parsed into a particular object.
507+ * @return the value parsed into a particular object or null if the option is not set .
507508 * @throws ParseException if there are problems turning the option value into the desired type
508509 * @see PatternOptionBuilder
509510 * @since 1.5.0
@@ -518,7 +519,7 @@ public <T> T getParsedOptionValue(final char opt) throws ParseException {
518519 * @param opt the name of the option.
519520 * @param defaultValue the default value to return if opt is not set.
520521 * @param <T> The return type for the method.
521- * @return the value parsed into a particular object.
522+ * @return the value parsed into a particular object or the defaultValue if the option is not set .
522523 * @throws ParseException if there are problems turning the option value into the desired type
523524 * @see PatternOptionBuilder
524525 * @since 1.7.0
@@ -533,7 +534,7 @@ public <T> T getParsedOptionValue(final char opt, final Supplier<T> defaultValue
533534 * @param opt the name of the option.
534535 * @param defaultValue the default value to return if opt is not set.
535536 * @param <T> The return type for the method.
536- * @return the value parsed into a particular object.
537+ * @return the value parsed into a particular object or the defaultValue if the option is not set .
537538 * @throws ParseException if there are problems turning the option value into the desired type
538539 * @see PatternOptionBuilder
539540 * @since 1.7.0
@@ -547,7 +548,7 @@ public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws P
547548 *
548549 * @param option the option.
549550 * @param <T> The return type for the method.
550- * @return the value parsed into a particular object.
551+ * @return the value parsed into a particular object or null if the option is not set .
551552 * @throws ParseException if there are problems turning the option value into the desired type
552553 * @see PatternOptionBuilder
553554 * @since 1.5.0
@@ -562,7 +563,7 @@ public <T> T getParsedOptionValue(final Option option) throws ParseException {
562563 * @param option the option.
563564 * @param defaultValue the default value to return if opt is not set.
564565 * @param <T> The return type for the method.
565- * @return the value parsed into a particular object.
566+ * @return the value parsed into a particular object or the defaultValue if the option is not set .
566567 * @throws ParseException if there are problems turning the option value into the desired type
567568 * @see PatternOptionBuilder
568569 * @since 1.7.0
@@ -589,7 +590,7 @@ public <T> T getParsedOptionValue(final Option option, final Supplier<T> default
589590 * @param option the option.
590591 * @param defaultValue the default value to return if opt is not set.
591592 * @param <T> The return type for the method.
592- * @return the value parsed into a particular object.
593+ * @return the value parsed into a particular object or the defaultValue if the option is not set .
593594 * @throws ParseException if there are problems turning the option value into the desired type
594595 * @see PatternOptionBuilder
595596 * @since 1.7.0
@@ -603,7 +604,7 @@ public <T> T getParsedOptionValue(final Option option, final T defaultValue) thr
603604 *
604605 * @param optionGroup the option group.
605606 * @param <T> The return type for the method.
606- * @return the value parsed into a particular object.
607+ * @return the value parsed into a particular object or null if no option in the OptionGroup is set .
607608 * @throws ParseException if there are problems turning the selected option value into the desired type
608609 * @see PatternOptionBuilder
609610 * @since 1.9.0
@@ -618,7 +619,7 @@ public <T> T getParsedOptionValue(final OptionGroup optionGroup) throws ParseExc
618619 * @param optionGroup the option group.
619620 * @param defaultValue the default value to return if opt is not set.
620621 * @param <T> The return type for the method.
621- * @return the value parsed into a particular object.
622+ * @return the value parsed into a particular object or the defaultValue if no option in the OptionGroup is set .
622623 * @throws ParseException if there are problems turning the selected option value into the desired type
623624 * @see PatternOptionBuilder
624625 * @since 1.9.0
@@ -636,7 +637,7 @@ public <T> T getParsedOptionValue(final OptionGroup optionGroup, final Supplier<
636637 * @param optionGroup the option group.
637638 * @param defaultValue the default value to return if an option is not selected.
638639 * @param <T> The return type for the method.
639- * @return the value parsed into a particular object.
640+ * @return the value parsed into a particular object or the defaultValue if no option in the OptionGroup is set .
640641 * @throws ParseException if there are problems turning the option value into the desired type
641642 * @see PatternOptionBuilder
642643 * @since 1.9.0
@@ -650,7 +651,7 @@ public <T> T getParsedOptionValue(final OptionGroup optionGroup, final T default
650651 *
651652 * @param opt the name of the option.
652653 * @param <T> The return type for the method.
653- * @return the value parsed into a particular object.
654+ * @return the value parsed into a particular object or null if the option is not set .
654655 * @throws ParseException if there are problems turning the option value into the desired type
655656 * @see PatternOptionBuilder
656657 * @since 1.2
@@ -665,7 +666,7 @@ public <T> T getParsedOptionValue(final String opt) throws ParseException {
665666 * @param opt the name of the option.
666667 * @param defaultValue the default value to return if opt is not set.
667668 * @param <T> The return type for the method.
668- * @return the value parsed into a particular object.
669+ * @return the value parsed into a particular object or the defaultValue if the option is not set .
669670 * @throws ParseException if there are problems turning the option value into the desired type
670671 * @see PatternOptionBuilder
671672 * @since 1.7.0
@@ -680,7 +681,7 @@ public <T> T getParsedOptionValue(final String opt, final Supplier<T> defaultVal
680681 * @param opt the name of the option.
681682 * @param defaultValue the default value to return if opt is not set.
682683 * @param <T> The return type for the method.
683- * @return the value parsed into a particular object.
684+ * @return the value parsed into a particular object or the defaultValue if the option is not set .
684685 * @throws ParseException if there are problems turning the option value into the desired type
685686 * @see PatternOptionBuilder
686687 * @since 1.7.0
@@ -689,6 +690,202 @@ public <T> T getParsedOptionValue(final String opt, final T defaultValue) throws
689690 return getParsedOptionValue (resolveOption (opt ), defaultValue );
690691 }
691692
693+ /**
694+ * Gets a version of this {@code Option} converted to an array of a particular type.
695+ *
696+ * @param opt the name of the option.
697+ * @param <T> The array type for the return value.
698+ * @return the values parsed into an array of objects or null if the option is not set.
699+ * @throws ParseException if there are problems turning the option value into the desired type
700+ * @see PatternOptionBuilder
701+ * @since 1.10.0
702+ */
703+ public <T > T [] getParsedOptionValues (final char opt ) throws ParseException {
704+ return getParsedOptionValues (String .valueOf (opt ));
705+ }
706+
707+ /**
708+ * Gets a version of this {@code Option} converted to an array of a particular type.
709+ *
710+ * @param opt the name of the option.
711+ * @param defaultValue the default value to return if opt is not set.
712+ * @param <T> The array type for the return value.
713+ * @return the values parsed into an array of objects or the defaultValue if the option is not set.
714+ * @throws ParseException if there are problems turning the option value into the desired type
715+ * @see PatternOptionBuilder
716+ * @since 1.10.0
717+ */
718+ public <T > T [] getParsedOptionValues (final char opt , final Supplier <T []> defaultValue ) throws ParseException {
719+ return getParsedOptionValues (String .valueOf (opt ), defaultValue );
720+ }
721+
722+ /**
723+ * Gets a version of this {@code Option} converted to an array of a particular type.
724+ *
725+ * @param opt the name of the option.
726+ * @param defaultValue the default value to return if opt is not set.
727+ * @param <T> The array type for the return value.
728+ * @return the values parsed into an array of objects or the defaultValue if the option is not set.
729+ * @throws ParseException if there are problems turning the option value into the desired type
730+ * @see PatternOptionBuilder
731+ * @since 1.10.0
732+ */
733+ public <T > T [] getParsedOptionValues (final char opt , final T [] defaultValue ) throws ParseException {
734+ return getParsedOptionValues (String .valueOf (opt ), defaultValue );
735+ }
736+
737+ /**
738+ * Gets a version of this {@code Option} converted to an array of a particular type.
739+ *
740+ * @param option the option.
741+ * @param <T> The array type for the return value.
742+ * @return the values parsed into an array of objects or null if the option is not set.
743+ * @throws ParseException if there are problems turning the option value into the desired type
744+ * @see PatternOptionBuilder
745+ * @since 1.10.0
746+ */
747+ public <T > T [] getParsedOptionValues (final Option option ) throws ParseException {
748+ return getParsedOptionValues (option , () -> null );
749+ }
750+
751+ /**
752+ * Gets a version of this {@code Option} converted to an array of a particular type.
753+ *
754+ * @param option the option.
755+ * @param defaultValue the default value to return if opt is not set.
756+ * @param <T> The array type for the return value.
757+ * @return the values parsed into an array of objects or the defaultValue if the option is not set.
758+ * @throws ParseException if there are problems turning the option value into the desired type
759+ * @see PatternOptionBuilder
760+ * @since 1.10.0
761+ */
762+ @ SuppressWarnings ("unchecked" )
763+ public <T > T [] getParsedOptionValues (final Option option , final Supplier <T []> defaultValue ) throws ParseException {
764+ if (option == null ) {
765+ return get (defaultValue );
766+ }
767+ Class <? extends T > clazz = (Class <? extends T >) option .getType ();
768+ String [] values = getOptionValues (option );
769+ if (values == null ) {
770+ return get (defaultValue );
771+ }
772+ T [] result = (T []) Array .newInstance (clazz , values .length );
773+ try {
774+ for (int i = 0 ; i < values .length ; i ++) {
775+ result [i ] = clazz .cast (option .getConverter ().apply (values [i ]));
776+ }
777+ return result ;
778+ } catch (final Exception t ) {
779+ throw ParseException .wrap (t );
780+ }
781+ }
782+
783+ /**
784+ * Gets a version of this {@code Option} converted to an array of a particular type.
785+ *
786+ * @param option the option.
787+ * @param defaultValue the default value to return if opt is not set.
788+ * @param <T> The array type for the return value.
789+ * @return the values parsed into an array of objects or the defaultValue if the option is not set.
790+ * @throws ParseException if there are problems turning the option value into the desired type
791+ * @see PatternOptionBuilder
792+ * @since 1.10.0
793+ */
794+ public <T > T [] getParsedOptionValues (final Option option , final T [] defaultValue ) throws ParseException {
795+ return getParsedOptionValues (option , () -> defaultValue );
796+ }
797+
798+ /**
799+ * Gets a version of this {@code OptionGroup} converted to an array of a particular type.
800+ *
801+ * @param optionGroup the option group.
802+ * @param <T> The array type for the return value.
803+ * @return the values parsed into an array of objects or null if no option in the OptionGroup is set.
804+ * @throws ParseException if there are problems turning the selected option value into the desired type
805+ * @see PatternOptionBuilder
806+ * @since 1.10.0
807+ */
808+ public <T > T [] getParsedOptionValues (final OptionGroup optionGroup ) throws ParseException {
809+ return getParsedOptionValues (optionGroup , () -> null );
810+ }
811+
812+ /**
813+ * Gets a version of this {@code OptionGroup} converted to an array of a particular type.
814+ *
815+ * @param optionGroup the option group.
816+ * @param defaultValue the default value to return if opt is not set.
817+ * @param <T> The array type for the return value.
818+ * @return the values parsed into an array of objects or null if no option in the OptionGroup is set.
819+ * @throws ParseException if there are problems turning the selected option value into the desired type
820+ * @see PatternOptionBuilder
821+ * @since 1.10.0
822+ */
823+ public <T > T [] getParsedOptionValues (final OptionGroup optionGroup , final Supplier <T []> defaultValue ) throws ParseException {
824+ if (optionGroup == null || !optionGroup .isSelected ()) {
825+ return get (defaultValue );
826+ }
827+ return getParsedOptionValues (optionGroup .getSelected (), defaultValue );
828+ }
829+
830+ /**
831+ * Gets a version of this {@code OptionGroup} converted to an array of a particular type.
832+ *
833+ * @param optionGroup the option group.
834+ * @param defaultValue the default value to return if an option is not selected.
835+ * @param <T> The array type for the return value.
836+ * @return the values parsed into an array of objects or null if no option in the OptionGroup is set.
837+ * @throws ParseException if there are problems turning the option value into the desired type
838+ * @see PatternOptionBuilder
839+ * @since 1.10.0
840+ */
841+ public <T > T [] getParsedOptionValues (final OptionGroup optionGroup , final T [] defaultValue ) throws ParseException {
842+ return getParsedOptionValues (optionGroup , () -> defaultValue );
843+ }
844+
845+ /**
846+ * Gets a version of this {@code Option} converted to an array of a particular type.
847+ *
848+ * @param opt the name of the option.
849+ * @param <T> The array type for the return value.
850+ * @return the values parsed into an array of objects or null if the option is not set.
851+ * @throws ParseException if there are problems turning the option value into the desired type
852+ * @see PatternOptionBuilder
853+ * @since 1.10.0
854+ */
855+ public <T > T [] getParsedOptionValues (final String opt ) throws ParseException {
856+ return getParsedOptionValues (resolveOption (opt ));
857+ }
858+
859+ /**
860+ * Gets a version of this {@code Option} converted to an array of a particular type.
861+ *
862+ * @param opt the name of the option.
863+ * @param defaultValue the default value to return if opt is not set.
864+ * @param <T> The array type for the return value.
865+ * @return the values parsed into an array of objects or defaultValues if the option is not set.
866+ * @throws ParseException if there are problems turning the option value into the desired type
867+ * @see PatternOptionBuilder
868+ * @since 1.10.0
869+ */
870+ public <T > T [] getParsedOptionValues (final String opt , final Supplier <T []> defaultValue ) throws ParseException {
871+ return getParsedOptionValues (resolveOption (opt ), defaultValue );
872+ }
873+
874+ /**
875+ * Gets a version of this {@code Option} converted to an array of a particular type.
876+ *
877+ * @param opt the name of the option.
878+ * @param defaultValue the default value to return if opt is not set.
879+ * @param <T> The array type for the return value.
880+ * @return the values parsed into an array of objects or defaultValues if the option is not set.
881+ * @throws ParseException if there are problems turning the option value into the desired type
882+ * @see PatternOptionBuilder
883+ * @since 1.10.0
884+ */
885+ public <T > T [] getParsedOptionValues (final String opt , final T [] defaultValue ) throws ParseException {
886+ return getParsedOptionValues (resolveOption (opt ), defaultValue );
887+ }
888+
692889 /**
693890 * Handles deprecated options.
694891 *
0 commit comments