@@ -170,6 +170,38 @@ private static Stream<Arguments> createParsedOptionValueParameters() throws Pars
170170 return lst .stream ();
171171 }
172172
173+ private static Stream <Arguments > createParsedOptionValuesParameters () throws ParseException {
174+ final List <Arguments > lst = new ArrayList <>();
175+ final Option optT = Option .builder ().option ("T" ).longOpt ("tee" ).deprecated ().type (Integer .class ).optionalArg (true ).hasArgs ().build ();
176+ final Option optU = Option .builder ("U" ).longOpt ("you" ).type (Integer .class ).optionalArg (true ).hasArgs ().build ();
177+ final OptionGroup optionGroup = new OptionGroup ().addOption (optT ).addOption (optU );
178+ final Integer [] expected = new Integer []{1 , 2 };
179+
180+ // T set
181+ lst .add (Arguments .of (new String [] {"-T" }, optT , optionGroup , true , null , true , null , optT ));
182+ lst .add (Arguments .of (new String [] {"-T" , "1" , "2" }, optT , optionGroup , true , expected , true , expected , optT ));
183+ lst .add (Arguments .of (new String [] {"--tee" }, optT , optionGroup , true , null , true , null , optT ));
184+ lst .add (Arguments .of (new String [] {"--tee" , "1" , "2" }, optT , optionGroup , true , expected , true , expected , optT ));
185+
186+ lst .add (Arguments .of (new String [] {"-U" }, optT , optionGroup , false , null , false , null , optU ));
187+ lst .add (Arguments .of (new String [] {"-U" , "1" , "2" }, optT , optionGroup , false , null , false , expected , optU ));
188+ lst .add (Arguments .of (new String [] {"--you" }, optT , optionGroup , false , null , false , null , optU ));
189+ lst .add (Arguments .of (new String [] {"--you" , "1" , "2" }, optT , optionGroup , false , null , false , expected , optU ));
190+
191+ // U set
192+ lst .add (Arguments .of (new String [] {"-T" }, optU , optionGroup , false , null , true , null , optT ));
193+ lst .add (Arguments .of (new String [] {"-T" , "1" , "2" }, optU , optionGroup , false , null , true , expected , optT ));
194+ lst .add (Arguments .of (new String [] {"--tee" }, optU , optionGroup , false , null , true , null , optT ));
195+ lst .add (Arguments .of (new String [] {"--tee" , "1" , "2" }, optU , optionGroup , false , null , true , expected , optT ));
196+
197+ lst .add (Arguments .of (new String [] {"-U" }, optU , optionGroup , false , null , false , null , optU ));
198+ lst .add (Arguments .of (new String [] {"-U" , "1" , "2" }, optU , optionGroup , false , expected , false , expected , optU ));
199+ lst .add (Arguments .of (new String [] {"--you" }, optU , optionGroup , false , null , false , null , optU ));
200+ lst .add (Arguments .of (new String [] {"--you" , "1" , "2" }, optU , optionGroup , false , expected , false , expected , optU ));
201+
202+ return lst .stream ();
203+ }
204+
173205 char asChar (final Option opt ) {
174206 return opt .getOpt ().charAt (0 );
175207 }
@@ -328,6 +360,7 @@ public void testGetOptionsBuilder() {
328360 assertEquals (3 , cmd .getOptions ().length );
329361 }
330362
363+
331364 @ Test
332365 public void testGetOptionsCtor () {
333366 final CommandLine cmd = new CommandLine ();
@@ -342,7 +375,6 @@ public void testGetOptionsCtor() {
342375 assertEquals (3 , cmd .getOptions ().length );
343376 }
344377
345-
346378 /**
347379 * Test for get option value with and without default values. Verifies that deprecated options only report as
348380 * deprecated once.
@@ -600,6 +632,100 @@ public void testGetParsedOptionValue(final String[] args, final Option opt, fina
600632 checkHandler (false , handler , opt );
601633 }
602634
635+ @ ParameterizedTest (name = "{0}, {1}" )
636+ @ MethodSource ("createParsedOptionValuesParameters" )
637+ public void testGetParsedOptionValues (final String [] args , final Option opt , final OptionGroup optionGroup , final boolean optDep ,
638+ final Integer [] optValue , final boolean grpDep , final Integer [] grpValue , final Option grpOpt ) throws ParseException {
639+ final Options options = new Options ().addOptionGroup (optionGroup );
640+ final List <Option > handler = new ArrayList <>();
641+ final CommandLine commandLine = DefaultParser .builder ().setDeprecatedHandler (handler ::add ).get ().parse (options , args );
642+ final Supplier <Integer []> thinger = () -> new Integer []{2 , 3 };
643+ final OptionGroup otherGroup = new OptionGroup ().addOption (Option .builder ("o" ).longOpt ("other" ).hasArg ().build ())
644+ .addOption (Option .builder ().option ("p" ).longOpt ("part" ).hasArg ().build ());
645+ final OptionGroup nullGroup = null ;
646+ final Integer [] thing = {2 , 3 };
647+
648+ // test char option arg
649+ assertArrayEquals (optValue , commandLine .getParsedOptionValues (asChar (opt )));
650+ checkHandler (optDep , handler , opt );
651+
652+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (asChar (opt ), thing ));
653+ checkHandler (optDep , handler , opt );
654+
655+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (asChar (opt ), thinger ));
656+ checkHandler (optDep , handler , opt );
657+
658+ // test short option arg
659+ assertArrayEquals (optValue , commandLine .getParsedOptionValues (opt .getOpt ()));
660+ checkHandler (optDep , handler , opt );
661+
662+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getOpt (), thing ));
663+ checkHandler (optDep , handler , opt );
664+
665+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getOpt (), thinger ));
666+ checkHandler (optDep , handler , opt );
667+
668+ // test long option arg
669+ assertArrayEquals (optValue , commandLine .getParsedOptionValues (opt .getLongOpt ()));
670+ checkHandler (optDep , handler , opt );
671+
672+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getLongOpt (), thing ));
673+ checkHandler (optDep , handler , opt );
674+
675+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getLongOpt (), thinger ));
676+ checkHandler (optDep , handler , opt );
677+
678+ // test Option arg
679+ assertArrayEquals (optValue , commandLine .getParsedOptionValues (opt ));
680+ checkHandler (optDep , handler , opt );
681+
682+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt , thing ));
683+ checkHandler (optDep , handler , opt );
684+
685+ assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt , thinger ));
686+ checkHandler (optDep , handler , opt );
687+
688+ // test OptionGroup arg
689+ assertArrayEquals (grpValue , commandLine .getParsedOptionValues (optionGroup ));
690+ checkHandler (grpDep , handler , grpOpt );
691+
692+ assertArrayEquals (grpValue == null ? thing : grpValue , commandLine .getParsedOptionValues (optionGroup , thing ));
693+ checkHandler (grpDep , handler , grpOpt );
694+
695+ assertArrayEquals (grpValue == null ? thing : grpValue , commandLine .getParsedOptionValues (optionGroup , thinger ));
696+ checkHandler (grpDep , handler , grpOpt );
697+
698+ // test other Group arg
699+ assertNull (commandLine .getParsedOptionValues (otherGroup ));
700+ checkHandler (false , handler , grpOpt );
701+
702+ assertArrayEquals (thing , commandLine .getParsedOptionValues (otherGroup , thing ));
703+ checkHandler (false , handler , grpOpt );
704+
705+ assertArrayEquals (thing , commandLine .getParsedOptionValues (otherGroup , thinger ));
706+ checkHandler (false , handler , grpOpt );
707+
708+ // test null Group arg
709+ assertNull (commandLine .getParsedOptionValues (nullGroup ));
710+ checkHandler (false , handler , grpOpt );
711+
712+ assertArrayEquals (thing , commandLine .getParsedOptionValues (nullGroup , thing ));
713+ checkHandler (false , handler , grpOpt );
714+
715+ assertArrayEquals (thing , commandLine .getParsedOptionValues (nullGroup , thinger ));
716+ checkHandler (false , handler , grpOpt );
717+
718+ // test not an option
719+ assertNull (commandLine .getParsedOptionValues ("Nope" ));
720+ checkHandler (false , handler , opt );
721+
722+ assertArrayEquals (thing , commandLine .getParsedOptionValues ("Nope" , thing ));
723+ checkHandler (false , handler , opt );
724+
725+ assertArrayEquals (thing , commandLine .getParsedOptionValues ("Nope" , thinger ));
726+ checkHandler (false , handler , opt );
727+ }
728+
603729 /**
604730 * Tests the hasOption calls.
605731 * @param args the argument strings to parse.
@@ -897,130 +1023,4 @@ public void testNullOption() throws Exception {
8971023 assertNull (cmd .getOptionValue ((OptionGroup ) null ));
8981024 assertNull (cmd .getParsedOptionValue ((OptionGroup ) null ));
8991025 }
900-
901- @ ParameterizedTest (name = "{0}, {1}" )
902- @ MethodSource ("createParsedOptionValuesParameters" )
903- public void testGetParsedOptionValues (final String [] args , final Option opt , final OptionGroup optionGroup , final boolean optDep ,
904- final Integer [] optValue , final boolean grpDep , final Integer [] grpValue , final Option grpOpt ) throws ParseException {
905- final Options options = new Options ().addOptionGroup (optionGroup );
906- final List <Option > handler = new ArrayList <>();
907- final CommandLine commandLine = DefaultParser .builder ().setDeprecatedHandler (handler ::add ).get ().parse (options , args );
908- final Supplier <Integer []> thinger = () -> new Integer []{2 , 3 };
909- final OptionGroup otherGroup = new OptionGroup ().addOption (Option .builder ("o" ).longOpt ("other" ).hasArg ().build ())
910- .addOption (Option .builder ().option ("p" ).longOpt ("part" ).hasArg ().build ());
911- final OptionGroup nullGroup = null ;
912- final Integer [] thing = {2 , 3 };
913-
914- // test char option arg
915- assertArrayEquals (optValue , commandLine .getParsedOptionValues (asChar (opt )));
916- checkHandler (optDep , handler , opt );
917-
918- assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (asChar (opt ), thing ));
919- checkHandler (optDep , handler , opt );
920-
921- assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (asChar (opt ), thinger ));
922- checkHandler (optDep , handler , opt );
923-
924- // test short option arg
925- assertArrayEquals (optValue , commandLine .getParsedOptionValues (opt .getOpt ()));
926- checkHandler (optDep , handler , opt );
927-
928- assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getOpt (), thing ));
929- checkHandler (optDep , handler , opt );
930-
931- assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getOpt (), thinger ));
932- checkHandler (optDep , handler , opt );
933-
934- // test long option arg
935- assertArrayEquals (optValue , commandLine .getParsedOptionValues (opt .getLongOpt ()));
936- checkHandler (optDep , handler , opt );
937-
938- assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getLongOpt (), thing ));
939- checkHandler (optDep , handler , opt );
940-
941- assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt .getLongOpt (), thinger ));
942- checkHandler (optDep , handler , opt );
943-
944- // test Option arg
945- assertArrayEquals (optValue , commandLine .getParsedOptionValues (opt ));
946- checkHandler (optDep , handler , opt );
947-
948- assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt , thing ));
949- checkHandler (optDep , handler , opt );
950-
951- assertArrayEquals (optValue == null ? thing : optValue , commandLine .getParsedOptionValues (opt , thinger ));
952- checkHandler (optDep , handler , opt );
953-
954- // test OptionGroup arg
955- assertArrayEquals (grpValue , commandLine .getParsedOptionValues (optionGroup ));
956- checkHandler (grpDep , handler , grpOpt );
957-
958- assertArrayEquals (grpValue == null ? thing : grpValue , commandLine .getParsedOptionValues (optionGroup , thing ));
959- checkHandler (grpDep , handler , grpOpt );
960-
961- assertArrayEquals (grpValue == null ? thing : grpValue , commandLine .getParsedOptionValues (optionGroup , thinger ));
962- checkHandler (grpDep , handler , grpOpt );
963-
964- // test other Group arg
965- assertNull (commandLine .getParsedOptionValues (otherGroup ));
966- checkHandler (false , handler , grpOpt );
967-
968- assertArrayEquals (thing , commandLine .getParsedOptionValues (otherGroup , thing ));
969- checkHandler (false , handler , grpOpt );
970-
971- assertArrayEquals (thing , commandLine .getParsedOptionValues (otherGroup , thinger ));
972- checkHandler (false , handler , grpOpt );
973-
974- // test null Group arg
975- assertNull (commandLine .getParsedOptionValues (nullGroup ));
976- checkHandler (false , handler , grpOpt );
977-
978- assertArrayEquals (thing , commandLine .getParsedOptionValues (nullGroup , thing ));
979- checkHandler (false , handler , grpOpt );
980-
981- assertArrayEquals (thing , commandLine .getParsedOptionValues (nullGroup , thinger ));
982- checkHandler (false , handler , grpOpt );
983-
984- // test not an option
985- assertNull (commandLine .getParsedOptionValues ("Nope" ));
986- checkHandler (false , handler , opt );
987-
988- assertArrayEquals (thing , commandLine .getParsedOptionValues ("Nope" , thing ));
989- checkHandler (false , handler , opt );
990-
991- assertArrayEquals (thing , commandLine .getParsedOptionValues ("Nope" , thinger ));
992- checkHandler (false , handler , opt );
993- }
994-
995- private static Stream <Arguments > createParsedOptionValuesParameters () throws ParseException {
996- final List <Arguments > lst = new ArrayList <>();
997- final Option optT = Option .builder ().option ("T" ).longOpt ("tee" ).deprecated ().type (Integer .class ).optionalArg (true ).hasArgs ().build ();
998- final Option optU = Option .builder ("U" ).longOpt ("you" ).type (Integer .class ).optionalArg (true ).hasArgs ().build ();
999- final OptionGroup optionGroup = new OptionGroup ().addOption (optT ).addOption (optU );
1000- final Integer [] expected = new Integer []{1 , 2 };
1001-
1002- // T set
1003- lst .add (Arguments .of (new String [] {"-T" }, optT , optionGroup , true , null , true , null , optT ));
1004- lst .add (Arguments .of (new String [] {"-T" , "1" , "2" }, optT , optionGroup , true , expected , true , expected , optT ));
1005- lst .add (Arguments .of (new String [] {"--tee" }, optT , optionGroup , true , null , true , null , optT ));
1006- lst .add (Arguments .of (new String [] {"--tee" , "1" , "2" }, optT , optionGroup , true , expected , true , expected , optT ));
1007-
1008- lst .add (Arguments .of (new String [] {"-U" }, optT , optionGroup , false , null , false , null , optU ));
1009- lst .add (Arguments .of (new String [] {"-U" , "1" , "2" }, optT , optionGroup , false , null , false , expected , optU ));
1010- lst .add (Arguments .of (new String [] {"--you" }, optT , optionGroup , false , null , false , null , optU ));
1011- lst .add (Arguments .of (new String [] {"--you" , "1" , "2" }, optT , optionGroup , false , null , false , expected , optU ));
1012-
1013- // U set
1014- lst .add (Arguments .of (new String [] {"-T" }, optU , optionGroup , false , null , true , null , optT ));
1015- lst .add (Arguments .of (new String [] {"-T" , "1" , "2" }, optU , optionGroup , false , null , true , expected , optT ));
1016- lst .add (Arguments .of (new String [] {"--tee" }, optU , optionGroup , false , null , true , null , optT ));
1017- lst .add (Arguments .of (new String [] {"--tee" , "1" , "2" }, optU , optionGroup , false , null , true , expected , optT ));
1018-
1019- lst .add (Arguments .of (new String [] {"-U" }, optU , optionGroup , false , null , false , null , optU ));
1020- lst .add (Arguments .of (new String [] {"-U" , "1" , "2" }, optU , optionGroup , false , expected , false , expected , optU ));
1021- lst .add (Arguments .of (new String [] {"--you" }, optU , optionGroup , false , null , false , null , optU ));
1022- lst .add (Arguments .of (new String [] {"--you" , "1" , "2" }, optU , optionGroup , false , expected , false , expected , optU ));
1023-
1024- return lst .stream ();
1025- }
10261026}
0 commit comments