@@ -157,6 +157,24 @@ public void testDoubleDash1() throws Exception
157157 assertTrue ("Confirm 2 extra args: " + cl .getArgList ().size (), cl .getArgList ().size () == 2 );
158158 }
159159
160+ public void testDoubleDash2 () throws Exception
161+ {
162+ Options options = new Options ();
163+ options .addOption (OptionBuilder .hasArg ().create ('n' ));
164+ options .addOption (OptionBuilder .create ('m' ));
165+
166+ try
167+ {
168+ parser .parse (options , new String []{"-n" , "--" , "-m" });
169+ fail ("MissingArgumentException not thrown for option -n" );
170+ }
171+ catch (MissingArgumentException e )
172+ {
173+ assertNotNull ("option null" , e .getOption ());
174+ assertEquals ("n" , e .getOption ().getOpt ());
175+ }
176+ }
177+
160178 public void testSingleDash () throws Exception
161179 {
162180 String [] args = new String [] { "--copt" ,
@@ -228,6 +246,16 @@ public void testNegativeArgument() throws Exception
228246 assertEquals ("-1" , cl .getOptionValue ("b" ));
229247 }
230248
249+ public void testNegativeOption () throws Exception
250+ {
251+ String [] args = new String [] { "-b" , "-1" } ;
252+
253+ options .addOption ("1" , false , null );
254+
255+ CommandLine cl = parser .parse (options , args );
256+ assertEquals ("-1" , cl .getOptionValue ("b" ));
257+ }
258+
231259 public void testArgumentStartingWithHyphen () throws Exception
232260 {
233261 String [] args = new String []{"-b" , "-foo" };
@@ -284,6 +312,105 @@ public void testLongWithEqualSingleDash() throws Exception
284312 assertEquals ("bar" , cl .getOptionValue ("foo" ));
285313 }
286314
315+ public void testLongWithoutEqualSingleDash () throws Exception
316+ {
317+ String [] args = new String [] { "-foobar" };
318+
319+ Options options = new Options ();
320+ options .addOption (OptionBuilder .withLongOpt ("foo" ).hasArg ().create ('f' ));
321+
322+ CommandLine cl = parser .parse (options , args );
323+
324+ assertEquals ("bar" , cl .getOptionValue ("foo" ));
325+ }
326+
327+ public void testAmbiguousLongWithoutEqualSingleDash () throws Exception
328+ {
329+ String [] args = new String [] { "-b" , "-foobar" };
330+
331+ Options options = new Options ();
332+ options .addOption (OptionBuilder .withLongOpt ("foo" ).hasOptionalArg ().create ('f' ));
333+ options .addOption (OptionBuilder .withLongOpt ("bar" ).hasOptionalArg ().create ('b' ));
334+
335+ CommandLine cl = parser .parse (options , args );
336+
337+ assertTrue (cl .hasOption ("b" ));
338+ assertTrue (cl .hasOption ("f" ));
339+ assertEquals ("bar" , cl .getOptionValue ("foo" ));
340+ }
341+
342+ public void testLongWithoutEqualDoubleDash () throws Exception
343+ {
344+ String [] args = new String [] { "--foobar" };
345+
346+ Options options = new Options ();
347+ options .addOption (OptionBuilder .withLongOpt ("foo" ).hasArg ().create ('f' ));
348+
349+ CommandLine cl = parser .parse (options , args , true );
350+
351+ assertFalse (cl .hasOption ("foo" )); // foo isn't expected to be recognized with a double dash
352+ }
353+
354+ public void testLongWithUnexpectedArgument1 () throws Exception
355+ {
356+ String [] args = new String [] { "--foo=bar" };
357+
358+ Options options = new Options ();
359+ options .addOption (OptionBuilder .withLongOpt ("foo" ).create ('f' ));
360+
361+ try
362+ {
363+ parser .parse (options , args );
364+ }
365+ catch (UnrecognizedOptionException e )
366+ {
367+ assertEquals ("--foo=bar" , e .getOption ());
368+ return ;
369+ }
370+
371+ fail ("UnrecognizedOptionException not thrown" );
372+ }
373+
374+ public void testLongWithUnexpectedArgument2 () throws Exception
375+ {
376+ String [] args = new String [] { "-foobar" };
377+
378+ Options options = new Options ();
379+ options .addOption (OptionBuilder .withLongOpt ("foo" ).create ('f' ));
380+
381+ try
382+ {
383+ parser .parse (options , args );
384+ }
385+ catch (UnrecognizedOptionException e )
386+ {
387+ assertEquals ("-foobar" , e .getOption ());
388+ return ;
389+ }
390+
391+ fail ("UnrecognizedOptionException not thrown" );
392+ }
393+
394+ public void testShortWithUnexpectedArgument () throws Exception
395+ {
396+ String [] args = new String [] { "-f=bar" };
397+
398+ Options options = new Options ();
399+ options .addOption (OptionBuilder .withLongOpt ("foo" ).create ('f' ));
400+
401+ try
402+ {
403+ parser .parse (options , args );
404+ }
405+ catch (UnrecognizedOptionException e )
406+ {
407+ assertEquals ("-f=bar" , e .getOption ());
408+ return ;
409+ }
410+
411+ fail ("UnrecognizedOptionException not thrown" );
412+ }
413+
287414 public void testPropertiesOption1 () throws Exception
288415 {
289416 String [] args = new String [] { "-Jsource=1.5" , "-J" , "target" , "1.5" , "foo" };
@@ -726,4 +853,20 @@ public void testStopBursting2() throws Exception
726853 assertTrue ("Confirm 1 extra arg: " + cl .getArgList ().size (), cl .getArgList ().size () == 1 );
727854 assertTrue ("Confirm value of extra arg: " + cl .getArgList ().get (0 ), cl .getArgList ().get (0 ).equals ("foobar" ));
728855 }
856+
857+ public void testUnlimitedArgs () throws Exception
858+ {
859+ String [] args = new String []{"-e" , "one" , "two" , "-f" , "alpha" };
860+
861+ Options options = new Options ();
862+ options .addOption (OptionBuilder .hasArgs ().create ("e" ));
863+ options .addOption (OptionBuilder .hasArgs ().create ("f" ));
864+
865+ CommandLine cl = parser .parse (options , args );
866+
867+ assertTrue ("Confirm -e is set" , cl .hasOption ("e" ));
868+ assertEquals ("number of arg for -e" , 2 , cl .getOptionValues ("e" ).length );
869+ assertTrue ("Confirm -f is set" , cl .hasOption ("f" ));
870+ assertEquals ("number of arg for -f" , 1 , cl .getOptionValues ("f" ).length );
871+ }
729872}
0 commit comments