@@ -303,4 +303,94 @@ public void testPropertiesOption() throws Exception
303303 assertEquals ("Should be 1 arg left" ,1 ,argsleft .size ());
304304 assertEquals ("Expecting foo" ,"foo" ,argsleft .get (0 ));
305305 }
306+
307+ public void testUnambiguousPartialLongOption1 () throws Exception
308+ {
309+ String [] args = new String [] { "--ver" };
310+
311+ Options options = new Options ();
312+ options .addOption (OptionBuilder .withLongOpt ("version" ).create ());
313+ options .addOption (OptionBuilder .withLongOpt ("help" ).create ());
314+
315+ CommandLine cl = parser .parse (options , args );
316+
317+ assertTrue ("Confirm --version is set" , cl .hasOption ("version" ));
318+ }
319+
320+ public void testUnambiguousPartialLongOption2 () throws Exception
321+ {
322+ String [] args = new String [] { "-ver" };
323+
324+ Options options = new Options ();
325+ options .addOption (OptionBuilder .withLongOpt ("version" ).create ());
326+ options .addOption (OptionBuilder .withLongOpt ("help" ).create ());
327+
328+ CommandLine cl = parser .parse (options , args );
329+
330+ assertTrue ("Confirm --version is set" , cl .hasOption ("version" ));
331+ }
332+
333+ public void testAmbiguousPartialLongOption1 () throws Exception
334+ {
335+ String [] args = new String [] { "--ver" };
336+
337+ Options options = new Options ();
338+ options .addOption (OptionBuilder .withLongOpt ("version" ).create ());
339+ options .addOption (OptionBuilder .withLongOpt ("verbose" ).create ());
340+
341+ boolean caught = false ;
342+
343+ try
344+ {
345+ parser .parse (options , args );
346+ }
347+ catch (AmbiguousOptionException e )
348+ {
349+ caught = true ;
350+ assertEquals ("Partial option" , "--ver" , e .getOption ());
351+ assertNotNull ("Matching options null" , e .getMatchingOptions ());
352+ assertEquals ("Matching options size" , 2 , e .getMatchingOptions ().size ());
353+ }
354+
355+ assertTrue ( "Confirm MissingArgumentException caught" , caught );
356+ }
357+
358+ public void testAmbiguousPartialLongOption2 () throws Exception
359+ {
360+ String [] args = new String [] { "-ver" };
361+
362+ Options options = new Options ();
363+ options .addOption (OptionBuilder .withLongOpt ("version" ).create ());
364+ options .addOption (OptionBuilder .withLongOpt ("verbose" ).create ());
365+
366+ boolean caught = false ;
367+
368+ try
369+ {
370+ parser .parse (options , args );
371+ }
372+ catch (AmbiguousOptionException e )
373+ {
374+ caught = true ;
375+ assertEquals ("Partial option" , "-ver" , e .getOption ());
376+ assertNotNull ("Matching options null" , e .getMatchingOptions ());
377+ assertEquals ("Matching options size" , 2 , e .getMatchingOptions ().size ());
378+ }
379+
380+ assertTrue ( "Confirm MissingArgumentException caught" , caught );
381+ }
382+
383+ public void testPartialLongOptionWithShort () throws Exception
384+ {
385+ String [] args = new String [] { "-ver" };
386+
387+ Options options = new Options ();
388+ options .addOption (OptionBuilder .withLongOpt ("version" ).create ());
389+ options .addOption (OptionBuilder .hasArg ().create ('v' ));
390+
391+ CommandLine cl = parser .parse (options , args );
392+
393+ assertTrue ("Confirm --version is set" , cl .hasOption ("version" ));
394+ assertTrue ("Confirm -v is not set" , !cl .hasOption ("v" ));
395+ }
306396}
0 commit comments