@@ -27,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2727import java .io .ByteArrayOutputStream ;
2828import java .io .PrintStream ;
2929import java .util .ArrayList ;
30+ import java .util .Arrays ;
3031import java .util .List ;
3132import java .util .Properties ;
3233import java .util .function .Supplier ;
@@ -905,4 +906,133 @@ public void testNullOption() throws Exception {
905906 assertNull (cmd .getOptionValue ((OptionGroup ) null ));
906907 assertNull (cmd .getParsedOptionValue ((OptionGroup ) null ));
907908 }
909+
910+ @ ParameterizedTest (name = "{0}, {1}" )
911+ @ MethodSource ("createParsedOptionValuesParameters" )
912+ public void testGetParsedOptionValues (final String [] args , final Option opt , final OptionGroup optionGroup , final boolean optDep ,
913+ final Integer [] optValue , final boolean grpDep , final Integer [] grpValue , final Option grpOpt ) throws ParseException {
914+ final Options options = new Options ().addOptionGroup (optionGroup );
915+ final List <Option > handler = new ArrayList <>();
916+ final CommandLine commandLine = DefaultParser .builder ().setDeprecatedHandler (handler ::add ).get ().parse (options , args );
917+ final Supplier <Integer []> thinger = () -> new Integer []{2 , 3 };
918+ final OptionGroup otherGroup = new OptionGroup ().addOption (Option .builder ("o" ).longOpt ("other" ).hasArg ().build ())
919+ .addOption (Option .builder ().option ("p" ).longOpt ("part" ).hasArg ().build ());
920+ final OptionGroup nullGroup = null ;
921+ final Integer [] thing = {2 , 3 };
922+
923+ // test char option arg
924+ assertArrayEquals (optValue , commandLine .getParsedOptionValues (asChar (opt )));
925+ checkHandler (optDep , handler , opt );
926+
927+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (asChar (opt ), thing ));
928+ checkHandler (optDep , handler , opt );
929+
930+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (asChar (opt ), thinger ));
931+ checkHandler (optDep , handler , opt );
932+
933+ // test short option arg
934+ assertArrayEquals (optValue , commandLine .getParsedOptionValues (opt .getOpt ()));
935+ checkHandler (optDep , handler , opt );
936+
937+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getOpt (), thing ));
938+ checkHandler (optDep , handler , opt );
939+
940+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getOpt (), thinger ));
941+ checkHandler (optDep , handler , opt );
942+
943+ // test long option arg
944+ assertArrayEquals (optValue , commandLine .getParsedOptionValues (opt .getLongOpt ()));
945+ checkHandler (optDep , handler , opt );
946+
947+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getLongOpt (), thing ));
948+ checkHandler (optDep , handler , opt );
949+
950+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getLongOpt (), thinger ));
951+ checkHandler (optDep , handler , opt );
952+
953+
954+ // test Option arg
955+ assertArrayEquals (optValue , commandLine .getParsedOptionValues (opt ));
956+ checkHandler (optDep , handler , opt );
957+
958+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt , thing ));
959+ checkHandler (optDep , handler , opt );
960+
961+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt , thinger ));
962+ checkHandler (optDep , handler , opt );
963+
964+ // test OptionGroup arg
965+ assertArrayEquals (grpValue , commandLine .getParsedOptionValues (optionGroup ));
966+ checkHandler (grpDep , handler , grpOpt );
967+
968+ assertArrayEquals (grpValue == null ? thing : grpValue , commandLine .getParsedOptionValues (optionGroup , thing ));
969+ checkHandler (grpDep , handler , grpOpt );
970+
971+ assertArrayEquals (grpValue == null ? thing : grpValue , commandLine .getParsedOptionValues (optionGroup , thinger ));
972+ checkHandler (grpDep , handler , grpOpt );
973+
974+ // test other Group arg
975+ assertNull (commandLine .getParsedOptionValues (otherGroup ));
976+ checkHandler (false , handler , grpOpt );
977+
978+ assertArrayEquals (thing , commandLine .getParsedOptionValues (otherGroup , thing ));
979+ checkHandler (false , handler , grpOpt );
980+
981+ assertArrayEquals (thing , commandLine .getParsedOptionValues (otherGroup , thinger ));
982+ checkHandler (false , handler , grpOpt );
983+
984+ // test null Group arg
985+ assertNull (commandLine .getParsedOptionValues (nullGroup ));
986+ checkHandler (false , handler , grpOpt );
987+
988+ assertArrayEquals (thing , commandLine .getParsedOptionValues (nullGroup , thing ));
989+ checkHandler (false , handler , grpOpt );
990+
991+ assertArrayEquals (thing , commandLine .getParsedOptionValues (nullGroup , thinger ));
992+ checkHandler (false , handler , grpOpt );
993+
994+
995+ // test not an option
996+ assertNull (commandLine .getParsedOptionValues ("Nope" ));
997+ checkHandler (false , handler , opt );
998+
999+ assertArrayEquals (thing , commandLine .getParsedOptionValues ("Nope" , thing ));
1000+ checkHandler (false , handler , opt );
1001+
1002+ assertArrayEquals (thing , commandLine .getParsedOptionValues ("Nope" , thinger ));
1003+ checkHandler (false , handler , opt );
1004+ }
1005+
1006+ private static Stream <Arguments > createParsedOptionValuesParameters () throws ParseException {
1007+ final List <Arguments > lst = new ArrayList <>();
1008+ final Option optT = Option .builder ().option ("T" ).longOpt ("tee" ).deprecated ().type (Integer .class ).optionalArg (true ).hasArgs ().build ();
1009+ final Option optU = Option .builder ("U" ).longOpt ("you" ).type (Integer .class ).optionalArg (true ).hasArgs ().build ();
1010+ final OptionGroup optionGroup = new OptionGroup ().addOption (optT ).addOption (optU );
1011+ final Integer [] expected = new Integer []{1 , 2 };
1012+
1013+ // T set
1014+ lst .add (Arguments .of (new String [] {"-T" }, optT , optionGroup , true , null , true , null , optT ));
1015+ lst .add (Arguments .of (new String [] {"-T" , "1" , "2" }, optT , optionGroup , true , expected , true , expected , optT ));
1016+ lst .add (Arguments .of (new String [] {"--tee" }, optT , optionGroup , true , null , true , null , optT ));
1017+ lst .add (Arguments .of (new String [] {"--tee" , "1" , "2" }, optT , optionGroup , true , expected , true , expected , optT ));
1018+
1019+ lst .add (Arguments .of (new String [] {"-U" }, optT , optionGroup , false , null , false , null , optU ));
1020+ lst .add (Arguments .of (new String [] {"-U" , "1" , "2" }, optT , optionGroup , false , null , false , expected , optU ));
1021+ lst .add (Arguments .of (new String [] {"--you" }, optT , optionGroup , false , null , false , null , optU ));
1022+ lst .add (Arguments .of (new String [] {"--you" , "1" , "2" }, optT , optionGroup , false , null , false , expected , optU ));
1023+
1024+
1025+ // U set
1026+ lst .add (Arguments .of (new String [] {"-T" }, optU , optionGroup , false , null , true , null , optT ));
1027+ lst .add (Arguments .of (new String [] {"-T" , "1" , "2" }, optU , optionGroup , false , null , true , expected , optT ));
1028+ lst .add (Arguments .of (new String [] {"--tee" }, optU , optionGroup , false , null , true , null , optT ));
1029+ lst .add (Arguments .of (new String [] {"--tee" , "1" , "2" }, optU , optionGroup , false , null , true , expected , optT ));
1030+
1031+ lst .add (Arguments .of (new String [] {"-U" }, optU , optionGroup , false , null , false , null , optU ));
1032+ lst .add (Arguments .of (new String [] {"-U" , "1" , "2" }, optU , optionGroup , false , expected , false , expected , optU ));
1033+ lst .add (Arguments .of (new String [] {"--you" }, optU , optionGroup , false , null , false , null , optU ));
1034+ lst .add (Arguments .of (new String [] {"--you" , "1" , "2" }, optU , optionGroup , false , expected , false , expected , optU ));
1035+
1036+ return lst .stream ();
1037+ }
9081038}
0 commit comments