Skip to content

Commit f74b1b7

Browse files
committed
Add test for "legacy" behaviour
when there was one boolean doing this or that only
1 parent 4e65408 commit f74b1b7

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/test/java/org/apache/commons/cli/DefaultParserTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,33 @@ void chainingParsersNonHappyPath() throws ParseException {
233233
assertTrue(e.getMessage().contains("-d"));
234234
}
235235

236+
@Test
237+
void legacyStopAtNonOption() throws ParseException {
238+
Option a = Option.builder().option("a").longOpt("first-letter").build();
239+
Option b = Option.builder().option("b").longOpt("second-letter").build();
240+
Option c = Option.builder().option("c").longOpt("third-letter").build();
241+
242+
Options options = new Options();
243+
options.addOption(a);
244+
options.addOption(b);
245+
options.addOption(c);
246+
247+
String[] args = {"-a", "-b", "-c", "-d", "arg1", "arg2"}; // -d is rogue option
248+
249+
DefaultParser parser = new DefaultParser();
250+
251+
CommandLine commandLine = parser.parse(options, args, null, true);
252+
assertEquals(3, commandLine.getOptions().length);
253+
assertEquals(3, commandLine.getArgs().length);
254+
assertFalse(commandLine.getArgList().contains("-c"));
255+
assertTrue(commandLine.getArgList().contains("-d"));
256+
assertTrue(commandLine.getArgList().contains("arg1"));
257+
assertTrue(commandLine.getArgList().contains("arg2"));
258+
259+
UnrecognizedOptionException e = assertThrows(UnrecognizedOptionException.class, () -> parser.parse(options, args, null, false));
260+
assertTrue(e.getMessage().contains("-d"));
261+
}
262+
236263
@Test
237264
void testBuilder() {
238265
// @formatter:off

0 commit comments

Comments
 (0)