@@ -19,6 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1919
2020import static org .junit .jupiter .api .Assertions .assertEquals ;
2121import static org .junit .jupiter .api .Assertions .assertFalse ;
22+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2223import static org .junit .jupiter .api .Assertions .assertTrue ;
2324
2425import java .util .HashSet ;
@@ -157,6 +158,83 @@ public void setUp() {
157158 parser = new DefaultParser ();
158159 }
159160
161+ @ Test
162+ void chainingParsersHappyPath () throws ParseException {
163+ Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
164+ Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
165+ Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
166+ Option d = Option .builder ().option ("d" ).longOpt ("fourth-letter" ).build ();
167+
168+ Options baseOptions = new Options ();
169+ baseOptions .addOption (a );
170+ baseOptions .addOption (b );
171+ Options specificOptions = new Options ();
172+ specificOptions .addOption (a );
173+ specificOptions .addOption (b );
174+ specificOptions .addOption (c );
175+ specificOptions .addOption (d );
176+
177+ String [] args = {"-a" , "-b" , "-c" , "-d" , "arg1" , "arg2" };
178+
179+ DefaultParser parser = new DefaultParser ();
180+
181+ CommandLine baseCommandLine = parser .parse (baseOptions , args , null , false , false );
182+ assertEquals (2 , baseCommandLine .getOptions ().length );
183+ assertEquals (4 , baseCommandLine .getArgs ().length );
184+ assertTrue (baseCommandLine .hasOption ("a" ));
185+ assertTrue (baseCommandLine .hasOption ("b" ));
186+ assertFalse (baseCommandLine .hasOption ("c" ));
187+ assertFalse (baseCommandLine .hasOption ("d" ));
188+ assertFalse (baseCommandLine .getArgList ().contains ("-a" ));
189+ assertFalse (baseCommandLine .getArgList ().contains ("-b" ));
190+ assertTrue (baseCommandLine .getArgList ().contains ("-c" ));
191+ assertTrue (baseCommandLine .getArgList ().contains ("-d" ));
192+ assertTrue (baseCommandLine .getArgList ().contains ("arg1" ));
193+ assertTrue (baseCommandLine .getArgList ().contains ("arg2" ));
194+
195+ CommandLine specificCommandLine = parser .parse (specificOptions , args , null , false , true );
196+ assertEquals (4 , specificCommandLine .getOptions ().length );
197+ assertEquals (2 , specificCommandLine .getArgs ().length );
198+ assertTrue (specificCommandLine .hasOption ("a" ));
199+ assertTrue (specificCommandLine .hasOption ("b" ));
200+ assertTrue (specificCommandLine .hasOption ("c" ));
201+ assertTrue (specificCommandLine .hasOption ("d" ));
202+ assertFalse (specificCommandLine .getArgList ().contains ("-a" ));
203+ assertFalse (specificCommandLine .getArgList ().contains ("-b" ));
204+ assertFalse (specificCommandLine .getArgList ().contains ("-c" ));
205+ assertFalse (specificCommandLine .getArgList ().contains ("-d" ));
206+ assertTrue (specificCommandLine .getArgList ().contains ("arg1" ));
207+ assertTrue (specificCommandLine .getArgList ().contains ("arg2" ));
208+ }
209+
210+ @ Test
211+ void chainingParsersNonHappyPath () throws ParseException {
212+ Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
213+ Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
214+ Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
215+ Option d = Option .builder ().option ("d" ).longOpt ("fourth-letter" ).build ();
216+
217+ Options baseOptions = new Options ();
218+ baseOptions .addOption (a );
219+ baseOptions .addOption (b );
220+ Options specificOptions = new Options ();
221+ specificOptions .addOption (a );
222+ specificOptions .addOption (b );
223+ specificOptions .addOption (c );
224+ specificOptions .addOption (d );
225+
226+ String [] args = {"-a" , "-b" , "-c" , "-d" , "arg1" , "arg2" , "--rogue-option" };
227+
228+ DefaultParser parser = new DefaultParser ();
229+
230+ CommandLine baseCommandLine = parser .parse (baseOptions , args , null , false , false );
231+ assertEquals (2 , baseCommandLine .getOptions ().length );
232+ assertEquals (5 , baseCommandLine .getArgs ().length );
233+
234+ UnrecognizedOptionException e = assertThrows (UnrecognizedOptionException .class , () -> parser .parse (specificOptions , args , null , false , true ));
235+ assertTrue (e .getMessage ().contains ("--rogue-option" ));
236+ }
237+
160238 @ Test
161239 void testBuilder () {
162240 // @formatter:off
0 commit comments