@@ -159,7 +159,7 @@ public void setUp() {
159159 }
160160
161161 @ Test
162- void chainingParsersHappyPath () throws ParseException {
162+ void chainingParsersSkipHappyPath () throws ParseException {
163163 Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
164164 Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
165165 Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
@@ -178,7 +178,7 @@ void chainingParsersHappyPath() throws ParseException {
178178
179179 DefaultParser parser = new DefaultParser ();
180180
181- CommandLine baseCommandLine = parser .parse (baseOptions , args , null , false , false );
181+ CommandLine baseCommandLine = parser .parse (baseOptions , args , null , DefaultParser . NonOptionAction . SKIP );
182182 assertEquals (2 , baseCommandLine .getOptions ().length );
183183 assertEquals (4 , baseCommandLine .getArgs ().length );
184184 assertTrue (baseCommandLine .hasOption ("a" ));
@@ -192,7 +192,7 @@ void chainingParsersHappyPath() throws ParseException {
192192 assertTrue (baseCommandLine .getArgList ().contains ("arg1" ));
193193 assertTrue (baseCommandLine .getArgList ().contains ("arg2" ));
194194
195- CommandLine specificCommandLine = parser .parse (specificOptions , args , null , false , true );
195+ CommandLine specificCommandLine = parser .parse (specificOptions , args , null , DefaultParser . NonOptionAction . THROW );
196196 assertEquals (4 , specificCommandLine .getOptions ().length );
197197 assertEquals (2 , specificCommandLine .getArgs ().length );
198198 assertTrue (specificCommandLine .hasOption ("a" ));
@@ -208,7 +208,7 @@ void chainingParsersHappyPath() throws ParseException {
208208 }
209209
210210 @ Test
211- void chainingParsersNonHappyPath () throws ParseException {
211+ void chainingParsersSkipNonHappyPath () throws ParseException {
212212 Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
213213 Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
214214 Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
@@ -225,11 +225,88 @@ void chainingParsersNonHappyPath() throws ParseException {
225225
226226 DefaultParser parser = new DefaultParser ();
227227
228- CommandLine baseCommandLine = parser .parse (baseOptions , args , null , false , false );
228+ CommandLine baseCommandLine = parser .parse (baseOptions , args , null , DefaultParser . NonOptionAction . SKIP );
229229 assertEquals (2 , baseCommandLine .getOptions ().length );
230230 assertEquals (4 , baseCommandLine .getArgs ().length );
231231
232- UnrecognizedOptionException e = assertThrows (UnrecognizedOptionException .class , () -> parser .parse (specificOptions , args , null , false , true ));
232+ UnrecognizedOptionException e = assertThrows (UnrecognizedOptionException .class ,
233+ () -> parser .parse (specificOptions , args , null , DefaultParser .NonOptionAction .THROW ));
234+ assertTrue (e .getMessage ().contains ("-d" ));
235+ }
236+
237+ @ Test
238+ void chainingParsersIgnoreHappyPath () throws ParseException {
239+ Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
240+ Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
241+ Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
242+ Option d = Option .builder ().option ("d" ).longOpt ("fourth-letter" ).build ();
243+
244+ Options baseOptions = new Options ();
245+ baseOptions .addOption (a );
246+ baseOptions .addOption (b );
247+ Options specificOptions = new Options ();
248+ specificOptions .addOption (a );
249+ specificOptions .addOption (b );
250+ specificOptions .addOption (c );
251+ specificOptions .addOption (d );
252+
253+ String [] args = {"-a" , "-b" , "-c" , "-d" , "arg1" , "arg2" };
254+
255+ DefaultParser parser = new DefaultParser ();
256+
257+ CommandLine baseCommandLine = parser .parse (baseOptions , args , null , DefaultParser .NonOptionAction .IGNORE );
258+ assertEquals (2 , baseCommandLine .getOptions ().length );
259+ assertEquals (2 , baseCommandLine .getArgs ().length );
260+ assertTrue (baseCommandLine .hasOption ("a" ));
261+ assertTrue (baseCommandLine .hasOption ("b" ));
262+ assertFalse (baseCommandLine .hasOption ("c" ));
263+ assertFalse (baseCommandLine .hasOption ("d" ));
264+ assertFalse (baseCommandLine .getArgList ().contains ("-a" ));
265+ assertFalse (baseCommandLine .getArgList ().contains ("-b" ));
266+ assertFalse (baseCommandLine .getArgList ().contains ("-c" ));
267+ assertFalse (baseCommandLine .getArgList ().contains ("-d" ));
268+ assertTrue (baseCommandLine .getArgList ().contains ("arg1" ));
269+ assertTrue (baseCommandLine .getArgList ().contains ("arg2" ));
270+
271+ CommandLine specificCommandLine = parser .parse (specificOptions , args , null , DefaultParser .NonOptionAction .THROW );
272+ assertEquals (4 , specificCommandLine .getOptions ().length );
273+ assertEquals (2 , specificCommandLine .getArgs ().length );
274+ assertTrue (specificCommandLine .hasOption ("a" ));
275+ assertTrue (specificCommandLine .hasOption ("b" ));
276+ assertTrue (specificCommandLine .hasOption ("c" ));
277+ assertTrue (specificCommandLine .hasOption ("d" ));
278+ assertFalse (specificCommandLine .getArgList ().contains ("-a" ));
279+ assertFalse (specificCommandLine .getArgList ().contains ("-b" ));
280+ assertFalse (specificCommandLine .getArgList ().contains ("-c" ));
281+ assertFalse (specificCommandLine .getArgList ().contains ("-d" ));
282+ assertTrue (specificCommandLine .getArgList ().contains ("arg1" ));
283+ assertTrue (specificCommandLine .getArgList ().contains ("arg2" ));
284+ }
285+
286+ @ Test
287+ void chainingParsersIgnoreNonHappyPath () throws ParseException {
288+ Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
289+ Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
290+ Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
291+
292+ Options baseOptions = new Options ();
293+ baseOptions .addOption (a );
294+ baseOptions .addOption (b );
295+ Options specificOptions = new Options ();
296+ specificOptions .addOption (a );
297+ specificOptions .addOption (b );
298+ specificOptions .addOption (c );
299+
300+ String [] args = {"-a" , "-b" , "-c" , "-d" , "arg1" , "arg2" }; // -d is rogue option
301+
302+ DefaultParser parser = new DefaultParser ();
303+
304+ CommandLine baseCommandLine = parser .parse (baseOptions , args , null , DefaultParser .NonOptionAction .IGNORE );
305+ assertEquals (2 , baseCommandLine .getOptions ().length );
306+ assertEquals (2 , baseCommandLine .getArgs ().length );
307+
308+ UnrecognizedOptionException e = assertThrows (UnrecognizedOptionException .class ,
309+ () -> parser .parse (specificOptions , args , null , DefaultParser .NonOptionAction .THROW ));
233310 assertTrue (e .getMessage ().contains ("-d" ));
234311 }
235312
0 commit comments