Tuesday, May 18, 2010

Commons-CLI repeated options

I was curious about how repeated options are handled in commons-cli... Here is a test with the conclusion of that curiosity.

    /**
     * An option that may be specified more than once.
     *
     * @throws ParseException
     */

   
@Test
   
public void testMultipleInstance() throws ParseException {
       
Options o = new Options().addOption("a", "apple", true, "Apples");

       
String[] args = { "-a", "one", "-a", "two" };
       
CommandLine c = new GnuParser().parse(o, args);

        assertArrayEquals
(new String[] { "one", "two" }, c
               
.getOptionValues("apple"));

        assertEquals
("one", c.getOptionValue("apple"));
   
}

No comments:

Post a Comment