Skip to content

Commit 764c148

Browse files
Fix for CLI-340 (#334)
* Fix for CLI-340 * removed unused import * fixed javadocs and catch(Throwable) issues * rebased and fixed catch issue * Remove extra whitespace --------- Co-authored-by: Gary Gregory <[email protected]>
1 parent d9f575d commit 764c148

File tree

4 files changed

+340
-25
lines changed

4 files changed

+340
-25
lines changed

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

Lines changed: 209 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1818
package org.apache.commons.cli;
1919

2020
import java.io.Serializable;
21+
import java.lang.reflect.Array;
2122
import java.util.ArrayList;
2223
import java.util.Iterator;
2324
import 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
*

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,9 @@ public String getValue(final String defaultValue) {
707707
}
708708

709709
/**
710-
* Gets the values of this Option as a String array or null if there are no values.
710+
* Gets the values of this Option as a String array or an empty array if there are no values.
711711
*
712-
* @return the values of this Option as a String array or null if there are no values.
712+
* @return the values of this Option as a String array or an empty array if there are no values.
713713
*/
714714
public String[] getValues() {
715715
return hasNoValues() ? null : values.toArray(EMPTY_STRING_ARRAY);
@@ -725,9 +725,9 @@ public char getValueSeparator() {
725725
}
726726

727727
/**
728-
* Gets the values of this Option as a List or null if there are no values.
728+
* Gets the values of this Option as a List. Will return an empty list if there are no values.
729729
*
730-
* @return the values of this Option as a List or null if there are no values.
730+
* @return the values of this Option as a List or an empty List if there are no values.
731731
*/
732732
public List<String> getValuesList() {
733733
return values;

src/main/java/org/apache/commons/cli/help/HelpFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected HelpFormatter(final Builder builder) {
147147
*/
148148
@Override
149149
public TableDefinition getTableDefinition(final Iterable<Option> options) {
150-
// set up the base TextStyle for the columns configured for the Option opt and arg values..
150+
// set up the base TextStyle for the columns configured for the Option opt and arg values.
151151
final TextStyle.Builder builder = TextStyle.builder().setAlignment(TextStyle.Alignment.LEFT).setIndent(DEFAULT_LEFT_PAD).setScalable(false);
152152
final List<TextStyle> styles = new ArrayList<>();
153153
styles.add(builder.get());

0 commit comments

Comments
 (0)