@@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2121import static org .junit .Assert .assertEquals ;
2222import static org .junit .Assert .assertFalse ;
2323import static org .junit .Assert .assertNotNull ;
24+ import static org .junit .Assert .assertNull ;
2425import static org .junit .Assert .assertTrue ;
2526import static org .junit .Assert .fail ;
2627import static org .junit .jupiter .api .Assertions .assertThrows ;
@@ -737,6 +738,54 @@ public void testPropertyOverrideValues() throws Exception {
737738 assertFalse (cmd .hasOption ("fake" ));
738739 }
739740
741+ @ Test
742+ public void testOptionalArgsOptionBuilder () throws Exception {
743+ final Options opts = new Options ();
744+ opts .addOption (OptionBuilder .hasOptionalArgs (2 ).create ('i' ));
745+ final Properties properties = new Properties ();
746+
747+ CommandLine cmd = parse (parser , opts , new String []{"-i" }, properties );
748+ assertTrue (cmd .hasOption ("i" ));
749+ assertNull (null , cmd .getOptionValues ("i" ));
750+
751+ cmd = parse (parser , opts , new String []{"-i" , "paper" }, properties );
752+ assertTrue (cmd .hasOption ("i" ));
753+ assertArrayEquals (new String []{"paper" }, cmd .getOptionValues ("i" ));
754+
755+ cmd = parse (parser , opts , new String []{"-i" , "paper" , "scissors" }, properties );
756+ assertTrue (cmd .hasOption ("i" ));
757+ assertArrayEquals (new String []{"paper" , "scissors" }, cmd .getOptionValues ("i" ));
758+
759+ cmd = parse (parser , opts , new String []{"-i" , "paper" , "scissors" , "rock" }, properties );
760+ assertTrue (cmd .hasOption ("i" ));
761+ assertArrayEquals (new String []{"paper" , "scissors" }, cmd .getOptionValues ("i" ));
762+ assertArrayEquals (new String []{"rock" }, cmd .getArgs ());
763+ }
764+
765+ @ Test
766+ public void testOptionalArgsOptionDotBuilder () throws Exception {
767+ final Options opts = new Options ();
768+ opts .addOption (Option .builder ("i" ).numberOfArgs (2 ).optionalArg (true ).build ());
769+ final Properties properties = new Properties ();
770+
771+ CommandLine cmd = parse (parser , opts , new String []{"-i" }, properties );
772+ assertTrue (cmd .hasOption ("i" ));
773+ assertNull (null , cmd .getOptionValues ("i" ));
774+
775+ cmd = parse (parser , opts , new String []{"-i" , "paper" }, properties );
776+ assertTrue (cmd .hasOption ("i" ));
777+ assertArrayEquals (new String []{"paper" }, cmd .getOptionValues ("i" ));
778+
779+ cmd = parse (parser , opts , new String []{"-i" , "paper" , "scissors" }, properties );
780+ assertTrue (cmd .hasOption ("i" ));
781+ assertArrayEquals (new String []{"paper" , "scissors" }, cmd .getOptionValues ("i" ));
782+
783+ cmd = parse (parser , opts , new String []{"-i" , "paper" , "scissors" , "rock" }, properties );
784+ assertTrue (cmd .hasOption ("i" ));
785+ assertArrayEquals (new String []{"paper" , "scissors" }, cmd .getOptionValues ("i" ));
786+ assertArrayEquals (new String []{"rock" }, cmd .getArgs ());
787+ }
788+
740789 @ Test
741790 public void testReuseOptionsTwice () throws Exception {
742791 final Options opts = new Options ();
0 commit comments