1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.cli;
19
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.PrintStream;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
32 import java.util.function.Supplier;
33 import java.util.stream.Stream;
34
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.params.ParameterizedTest;
37 import org.junit.jupiter.params.provider.Arguments;
38 import org.junit.jupiter.params.provider.MethodSource;
39
40 class CommandLineTest {
41
42 private enum Count { ONE, TWO, THREE }
43
44 private static Stream<Arguments> createHasOptionParameters() throws ParseException {
45 final List<Arguments> lst = new ArrayList<>();
46 final Option optT = Option.builder().option("T").longOpt("tee").deprecated().optionalArg(true).get();
47 final Option optU = Option.builder("U").longOpt("you").optionalArg(true).get();
48 final OptionGroup optionGroup = new OptionGroup().addOption(optT).addOption(optU);
49
50 final String[] foobar = { "foo", "bar" };
51
52 lst.add(Arguments.of(new String[] {"-T"}, optT, optionGroup, true, true, true, true, optT));
53 lst.add(Arguments.of(new String[] {"-T", "foo"}, optT, optionGroup, true, true, true, true, optT));
54 lst.add(Arguments.of(new String[] {"--tee"}, optT, optionGroup, true, true, true, true, optT));
55 lst.add(Arguments.of(new String[] {"--tee", "foo"}, optT, optionGroup, true, true, true, true, optT));
56
57 lst.add(Arguments.of(new String[] {"-U"}, optT, optionGroup, false, false, false, true, optU));
58 lst.add(Arguments.of(new String[] {"-U", "foo", "bar"}, optT, optionGroup, false, false, false, true, optU));
59 lst.add(Arguments.of(new String[] {"--you"}, optT, optionGroup, false, false, false, true, optU));
60 lst.add(Arguments.of(new String[] {"--you", "foo", "bar"}, optT, optionGroup, false, false, false, true, optU));
61
62
63 lst.add(Arguments.of(new String[] {"-T"}, optU, optionGroup, false, false, true, true, optT));
64 lst.add(Arguments.of(new String[] {"-T", "foo", "bar"}, optU, optionGroup, false, false, true, true, optT));
65 lst.add(Arguments.of(new String[] {"--tee"}, optU, optionGroup, false, false, true, true, optT));
66 lst.add(Arguments.of(new String[] {"--tee", "foo", "bar"}, optU, optionGroup, false, false, true, true, optT));
67
68 lst.add(Arguments.of(new String[] {"-U"}, optU, optionGroup, false, true, false, true, optU));
69 lst.add(Arguments.of(new String[] {"-U", "foo", "bar"}, optU, optionGroup, false, true, false, true, optU));
70 lst.add(Arguments.of(new String[] {"--you"}, optU, optionGroup, false, true, false, true, optU));
71 lst.add(Arguments.of(new String[] {"--you", "foo", "bar"}, optU, optionGroup, false, true, false, true, optU));
72
73 return lst.stream();
74 }
75
76 private static Stream<Arguments> createOptionValueParameters() throws ParseException {
77 final List<Arguments> lst = new ArrayList<>();
78 final Option optT = Option.builder().option("T").longOpt("tee").deprecated().optionalArg(true).get();
79 final Option optU = Option.builder("U").longOpt("you").optionalArg(true).get();
80 final OptionGroup optionGroup = new OptionGroup().addOption(optT).addOption(optU);
81
82
83 lst.add(Arguments.of(new String[] {"-T"}, optT, optionGroup, true, null, true, null, optT));
84 lst.add(Arguments.of(new String[] {"-T", "foo"}, optT, optionGroup, true, "foo", true, "foo", optT));
85 lst.add(Arguments.of(new String[] {"--tee"}, optT, optionGroup, true, null, true, null, optT));
86 lst.add(Arguments.of(new String[] {"--tee", "foo"}, optT, optionGroup, true, "foo", true, "foo", optT));
87
88 lst.add(Arguments.of(new String[] {"-U"}, optT, optionGroup, false, null, false, null, optU));
89 lst.add(Arguments.of(new String[] {"-U", "foo"}, optT, optionGroup, false, null, false, "foo", optU));
90 lst.add(Arguments.of(new String[] {"--you"}, optT, optionGroup, false, null, false, null, optU));
91 lst.add(Arguments.of(new String[] {"--you", "foo"}, optT, optionGroup, false, null, false, "foo", optU));
92
93
94 lst.add(Arguments.of(new String[] {"-T"}, optU, optionGroup, false, null, true, null, optT));
95 lst.add(Arguments.of(new String[] {"-T", "foo"}, optU, optionGroup, false, null, true, "foo", optT));
96 lst.add(Arguments.of(new String[] {"--tee"}, optU, optionGroup, false, null, true, null, optT));
97 lst.add(Arguments.of(new String[] {"--tee", "foo"}, optU, optionGroup, false, null, true, "foo", optT));
98
99 lst.add(Arguments.of(new String[] {"-U"}, optU, optionGroup, false, null, false, null, optU));
100 lst.add(Arguments.of(new String[] {"-U", "foo"}, optU, optionGroup, false, "foo", false, "foo", optU));
101 lst.add(Arguments.of(new String[] {"--you"}, optU, optionGroup, false, null, false, null, optU));
102 lst.add(Arguments.of(new String[] {"--you", "foo"}, optU, optionGroup, false, "foo", false, "foo", optU));
103
104 return lst.stream();
105 }
106
107 private static Stream<Arguments> createOptionValuesParameters() throws ParseException {
108 final List<Arguments> lst = new ArrayList<>();
109 final Option optT = Option.builder().option("T").longOpt("tee").numberOfArgs(2).deprecated().optionalArg(true).get();
110 final Option optU = Option.builder("U").longOpt("you").numberOfArgs(2).optionalArg(true).get();
111 final OptionGroup optionGroup = new OptionGroup().addOption(optT).addOption(optU);
112
113 final String[] foobar = { "foo", "bar" };
114
115 lst.add(Arguments.of(new String[] {"-T"}, optT, optionGroup, true, null, true, null, optT));
116 lst.add(Arguments.of(new String[] {"-T", "foo", "bar"}, optT, optionGroup, true, foobar, true, foobar, optT));
117 lst.add(Arguments.of(new String[] {"--tee"}, optT, optionGroup, true, null, true, null, optT));
118 lst.add(Arguments.of(new String[] {"--tee", "foo", "bar"}, optT, optionGroup, true, foobar, true, foobar, optT));
119
120 lst.add(Arguments.of(new String[] {"-U"}, optT, optionGroup, false, null, false, null, optU));
121 lst.add(Arguments.of(new String[] {"-U", "foo", "bar"}, optT, optionGroup, false, null, false, foobar, optU));
122 lst.add(Arguments.of(new String[] {"--you"}, optT, optionGroup, false, null, false, null, optU));
123 lst.add(Arguments.of(new String[] {"--you", "foo", "bar"}, optT, optionGroup, false, null, false, foobar, optU));
124
125
126 lst.add(Arguments.of(new String[] {"-T"}, optU, optionGroup, false, null, true, null, optT));
127 lst.add(Arguments.of(new String[] {"-T", "foo", "bar"}, optU, optionGroup, false, null, true, foobar, optT));
128 lst.add(Arguments.of(new String[] {"--tee"}, optU, optionGroup, false, null, true, null, optT));
129 lst.add(Arguments.of(new String[] {"--tee", "foo", "bar"}, optU, optionGroup, false, null, true, foobar, optT));
130
131 lst.add(Arguments.of(new String[] {"-U"}, optU, optionGroup, false, null, false, null, optU));
132 lst.add(Arguments.of(new String[] {"-U", "foo", "bar"}, optU, optionGroup, false, foobar, false, foobar, optU));
133 lst.add(Arguments.of(new String[] {"--you"}, optU, optionGroup, false, null, false, null, optU));
134 lst.add(Arguments.of(new String[] {"--you", "foo", "bar"}, optU, optionGroup, false, foobar, false, foobar, optU));
135
136 return lst.stream();
137 }
138
139 private static Stream<Arguments> createParsedOptionValueParameters() throws ParseException {
140 final List<Arguments> lst = new ArrayList<>();
141 final Option optT = Option.builder().option("T").longOpt("tee").deprecated().type(Integer.class).optionalArg(true).get();
142 final Option optU = Option.builder("U").longOpt("you").type(Integer.class).optionalArg(true).get();
143 final OptionGroup optionGroup = new OptionGroup().addOption(optT).addOption(optU);
144 final Integer expected = Integer.valueOf(1);
145
146
147 lst.add(Arguments.of(new String[] {"-T"}, optT, optionGroup, true, null, true, null, optT));
148 lst.add(Arguments.of(new String[] {"-T", "1"}, optT, optionGroup, true, expected, true, expected, optT));
149 lst.add(Arguments.of(new String[] {"--tee"}, optT, optionGroup, true, null, true, null, optT));
150 lst.add(Arguments.of(new String[] {"--tee", "1"}, optT, optionGroup, true, expected, true, expected, optT));
151
152 lst.add(Arguments.of(new String[] {"-U"}, optT, optionGroup, false, null, false, null, optU));
153 lst.add(Arguments.of(new String[] {"-U", "1"}, optT, optionGroup, false, null, false, expected, optU));
154 lst.add(Arguments.of(new String[] {"--you"}, optT, optionGroup, false, null, false, null, optU));
155 lst.add(Arguments.of(new String[] {"--you", "1"}, optT, optionGroup, false, null, false, expected, optU));
156
157
158 lst.add(Arguments.of(new String[] {"-T"}, optU, optionGroup, false, null, true, null, optT));
159 lst.add(Arguments.of(new String[] {"-T", "1"}, optU, optionGroup, false, null, true, expected, optT));
160 lst.add(Arguments.of(new String[] {"--tee"}, optU, optionGroup, false, null, true, null, optT));
161 lst.add(Arguments.of(new String[] {"--tee", "1"}, optU, optionGroup, false, null, true, expected, optT));
162
163 lst.add(Arguments.of(new String[] {"-U"}, optU, optionGroup, false, null, false, null, optU));
164 lst.add(Arguments.of(new String[] {"-U", "1"}, optU, optionGroup, false, expected, false, expected, optU));
165 lst.add(Arguments.of(new String[] {"--you"}, optU, optionGroup, false, null, false, null, optU));
166 lst.add(Arguments.of(new String[] {"--you", "1"}, optU, optionGroup, false, expected, false, expected, optU));
167
168 return lst.stream();
169 }
170
171 private static Stream<Arguments> createParsedOptionValuesParameters() throws ParseException {
172 final List<Arguments> lst = new ArrayList<>();
173 final Option optT = Option.builder().option("T").longOpt("tee").deprecated().type(Integer.class).optionalArg(true).hasArgs().get();
174 final Option optU = Option.builder("U").longOpt("you").type(Integer.class).optionalArg(true).hasArgs().get();
175 final OptionGroup optionGroup = new OptionGroup().addOption(optT).addOption(optU);
176 final Integer[] expected = {1, 2};
177
178
179 lst.add(Arguments.of(new String[] {"-T"}, optT, optionGroup, true, null, true, null, optT));
180 lst.add(Arguments.of(new String[] {"-T", "1", "2"}, optT, optionGroup, true, expected, true, expected, optT));
181 lst.add(Arguments.of(new String[] {"--tee"}, optT, optionGroup, true, null, true, null, optT));
182 lst.add(Arguments.of(new String[] {"--tee", "1", "2"}, optT, optionGroup, true, expected, true, expected, optT));
183
184 lst.add(Arguments.of(new String[] {"-U"}, optT, optionGroup, false, null, false, null, optU));
185 lst.add(Arguments.of(new String[] {"-U", "1", "2"}, optT, optionGroup, false, null, false, expected, optU));
186 lst.add(Arguments.of(new String[] {"--you"}, optT, optionGroup, false, null, false, null, optU));
187 lst.add(Arguments.of(new String[] {"--you", "1", "2"}, optT, optionGroup, false, null, false, expected, optU));
188
189
190 lst.add(Arguments.of(new String[] {"-T"}, optU, optionGroup, false, null, true, null, optT));
191 lst.add(Arguments.of(new String[] {"-T", "1", "2"}, optU, optionGroup, false, null, true, expected, optT));
192 lst.add(Arguments.of(new String[] {"--tee"}, optU, optionGroup, false, null, true, null, optT));
193 lst.add(Arguments.of(new String[] {"--tee", "1", "2"}, optU, optionGroup, false, null, true, expected, optT));
194
195 lst.add(Arguments.of(new String[] {"-U"}, optU, optionGroup, false, null, false, null, optU));
196 lst.add(Arguments.of(new String[] {"-U", "1", "2"}, optU, optionGroup, false, expected, false, expected, optU));
197 lst.add(Arguments.of(new String[] {"--you"}, optU, optionGroup, false, null, false, null, optU));
198 lst.add(Arguments.of(new String[] {"--you", "1", "2"}, optU, optionGroup, false, expected, false, expected, optU));
199
200 return lst.stream();
201 }
202
203 char asChar(final Option opt) {
204 return opt.getOpt().charAt(0);
205 }
206
207 private void assertWritten(final boolean optDep, final ByteArrayOutputStream baos) {
208 System.out.flush();
209 if (optDep) {
210 assertEquals("Option 'T''tee': Deprecated", baos.toString().trim());
211 } else {
212 assertEquals("", baos.toString());
213 }
214 baos.reset();
215 }
216
217
218
219
220
221
222
223 void checkHandler(final boolean optDep, final List<Option> handler, final Option opt) {
224 if (optDep) {
225 assertEquals(1, handler.size());
226 assertEquals(opt, handler.get(0));
227 } else {
228 assertEquals(0, handler.size());
229 }
230 handler.clear();
231 }
232
233 @Test
234 void testBadGetParsedOptionValue() throws Exception {
235
236 final Options options = new Options();
237 options.addOption(Option.builder("i").hasArg().type(Number.class).get());
238 options.addOption(Option.builder("c").hasArg().converter(s -> Count.valueOf(s.toUpperCase())).get());
239
240 final CommandLineParser parser = new DefaultParser();
241 final CommandLine cmd = parser.parse(options, new String[] {"-i", "foo", "-c", "bar"});
242
243 assertEquals(NumberFormatException.class, assertThrows(ParseException.class, () -> cmd.getParsedOptionValue("i")).getCause().getClass());
244 assertEquals(IllegalArgumentException.class, assertThrows(ParseException.class, () -> cmd.getParsedOptionValue("c")).getCause().getClass());
245 }
246
247 @Test
248 void testBuilderBuild() {
249
250 final CommandLine cmd = CommandLine.builder()
251 .addArg("foo")
252 .addArg("bar")
253 .addOption(Option.builder("T").get())
254 .build();
255
256 assertEquals("foo", cmd.getArgs()[0]);
257 assertEquals("bar", cmd.getArgList().get(1));
258 assertEquals("T", cmd.getOptions()[0].getOpt());
259 }
260
261 @Test
262 void testBuilderGet() {
263
264 final CommandLine cmd = CommandLine.builder()
265 .addArg("foo")
266 .addArg("bar")
267 .addOption(Option.builder("T").get())
268 .get();
269
270 assertEquals("foo", cmd.getArgs()[0]);
271 assertEquals("bar", cmd.getArgList().get(1));
272 assertEquals("T", cmd.getOptions()[0].getOpt());
273 }
274
275 @Test
276 void testBuilderNullArgs() {
277 final CommandLine.Builder builder = CommandLine.builder();
278 builder.addArg(null).addArg(null);
279 builder.addOption(Option.builder("T").get());
280 final CommandLine cmd = builder.build();
281
282 assertEquals(0, cmd.getArgs().length);
283 assertEquals("T", cmd.getOptions()[0].getOpt());
284 }
285
286 @Test
287 void testBuilderNullOption() {
288 final CommandLine.Builder builder = CommandLine.builder();
289 builder.addArg("foo").addArg("bar");
290 builder.addOption(null);
291 builder.addOption(null);
292 builder.addOption(null);
293 final CommandLine cmd = builder.build();
294
295 assertEquals("foo", cmd.getArgs()[0]);
296 assertEquals("bar", cmd.getArgList().get(1));
297 assertEquals(0, cmd.getOptions().length);
298 }
299
300 @Test
301 void testGetOptionProperties() throws Exception {
302 final String[] args = {"-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar"};
303
304 final Options options = new Options();
305 options.addOption(Option.builder("D").valueSeparator().optionalArg(true).numberOfArgs(2).get());
306 options.addOption(Option.builder().valueSeparator().numberOfArgs(2).longOpt("property").get());
307
308 final Parser parser = new GnuParser();
309 final CommandLine cl = parser.parse(options, args);
310
311 final Properties props = cl.getOptionProperties("D");
312 assertNotNull(props, "null properties");
313 assertEquals(4, props.size(), "number of properties in " + props);
314 assertEquals("value1", props.getProperty("param1"), "property 1");
315 assertEquals("value2", props.getProperty("param2"), "property 2");
316 assertEquals("true", props.getProperty("param3"), "property 3");
317 assertEquals("value4", props.getProperty("param4"), "property 4");
318
319 assertEquals("bar", cl.getOptionProperties("property").getProperty("foo"), "property with long format");
320 }
321
322 @Test
323 void testGetOptionPropertiesWithOption() throws Exception {
324 final String[] args = {"-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar"};
325
326 final Options options = new Options();
327 final Option optionD = Option.builder("D").valueSeparator().numberOfArgs(2).optionalArg(true).get();
328 final Option optionProperty = Option.builder().valueSeparator().numberOfArgs(2).longOpt("property").get();
329 options.addOption(optionD);
330 options.addOption(optionProperty);
331
332 final Parser parser = new GnuParser();
333 final CommandLine cl = parser.parse(options, args);
334
335 final Properties props = cl.getOptionProperties(optionD);
336 assertNotNull(props, "null properties");
337 assertEquals(4, props.size(), "number of properties in " + props);
338 assertEquals("value1", props.getProperty("param1"), "property 1");
339 assertEquals("value2", props.getProperty("param2"), "property 2");
340 assertEquals("true", props.getProperty("param3"), "property 3");
341 assertEquals("value4", props.getProperty("param4"), "property 4");
342
343 assertEquals("bar", cl.getOptionProperties(optionProperty).getProperty("foo"), "property with long format");
344 }
345
346 @Test
347 void testGetOptionsBuilder() {
348 final CommandLine cmd = CommandLine.builder().build();
349 assertNotNull(cmd.getOptions());
350 assertEquals(0, cmd.getOptions().length);
351
352 cmd.addOption(null);
353 cmd.addOption(new Option("a", null));
354 cmd.addOption(new Option("b", null));
355 cmd.addOption(new Option("c", null));
356
357 assertEquals(3, cmd.getOptions().length);
358 }
359
360 @Test
361 void testGetOptionsCtor() {
362 final CommandLine cmd = new CommandLine();
363 assertNotNull(cmd.getOptions());
364 assertEquals(0, cmd.getOptions().length);
365
366 cmd.addOption(new Option("a", null));
367 cmd.addOption(new Option("b", null));
368 cmd.addOption(new Option("c", null));
369 cmd.addOption(null);
370
371 assertEquals(3, cmd.getOptions().length);
372 }
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387 @ParameterizedTest(name = "{0}, {1}")
388 @MethodSource("createOptionValueParameters")
389 void testGetOptionValue(final String[] args, final Option opt, final OptionGroup optionGroup, final boolean optDep,
390 final String optValue, final boolean grpDep, final String grpValue, final Option grpOpt) throws ParseException {
391 final Options options = new Options().addOptionGroup(optionGroup);
392 final List<Option> handler = new ArrayList<>();
393 final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options, args);
394 final Supplier<String> thinger = () -> "thing";
395 final OptionGroup otherGroup = new OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().get())
396 .addOption(Option.builder().option("p").longOpt("part").hasArg().get());
397 final OptionGroup nullGroup = null;
398
399
400 assertEquals(optValue, commandLine.getOptionValue(asChar(opt)));
401 checkHandler(optDep, handler, opt);
402
403 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(asChar(opt), "thing"));
404 checkHandler(optDep, handler, opt);
405
406 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(asChar(opt), thinger));
407 checkHandler(optDep, handler, opt);
408
409
410 assertEquals(optValue, commandLine.getOptionValue(opt.getOpt()));
411 checkHandler(optDep, handler, opt);
412
413 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt.getOpt(), "thing"));
414 checkHandler(optDep, handler, opt);
415
416 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt.getOpt(), thinger));
417 checkHandler(optDep, handler, opt);
418
419
420 assertEquals(optValue, commandLine.getOptionValue(opt.getLongOpt()));
421 checkHandler(optDep, handler, opt);
422
423 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt.getLongOpt(), "thing"));
424 checkHandler(optDep, handler, opt);
425
426 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt.getLongOpt(), thinger));
427 checkHandler(optDep, handler, opt);
428
429
430 assertEquals(optValue, commandLine.getOptionValue(opt));
431 checkHandler(optDep, handler, opt);
432
433 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt, "thing"));
434 checkHandler(optDep, handler, opt);
435
436 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt, thinger));
437 checkHandler(optDep, handler, opt);
438
439
440 assertEquals(grpValue, commandLine.getOptionValue(optionGroup));
441 checkHandler(grpDep, handler, grpOpt);
442
443 assertEquals(grpValue == null ? "thing" : grpValue, commandLine.getOptionValue(optionGroup, "thing"));
444 checkHandler(grpDep, handler, grpOpt);
445
446 assertEquals(grpValue == null ? "thing" : grpValue, commandLine.getOptionValue(optionGroup, thinger));
447 checkHandler(grpDep, handler, grpOpt);
448
449
450 assertNull(commandLine.getOptionValue(otherGroup));
451 checkHandler(false, handler, grpOpt);
452
453 assertEquals("thing", commandLine.getOptionValue(otherGroup, "thing"));
454 checkHandler(false, handler, grpOpt);
455
456 assertEquals("thing", commandLine.getOptionValue(otherGroup, thinger));
457 checkHandler(false, handler, grpOpt);
458
459
460 assertNull(commandLine.getOptionValue(nullGroup));
461 checkHandler(false, handler, grpOpt);
462
463 assertEquals("thing", commandLine.getOptionValue(nullGroup, "thing"));
464 checkHandler(false, handler, grpOpt);
465
466 assertEquals("thing", commandLine.getOptionValue(nullGroup, thinger));
467 checkHandler(false, handler, grpOpt);
468
469
470 assertNull(commandLine.getOptionValue("Nope"));
471 checkHandler(false, handler, opt);
472
473 assertEquals("thing", commandLine.getOptionValue("Nope", "thing"));
474 checkHandler(false, handler, opt);
475
476 assertEquals("thing", commandLine.getOptionValue("Nope", thinger));
477 checkHandler(false, handler, opt);
478 }
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493 @ParameterizedTest(name = "{0}, {1}")
494 @MethodSource("createOptionValuesParameters")
495 void testGetOptionValues(final String[] args, final Option opt, final OptionGroup optionGroup, final boolean optDep,
496 final String[] optValue, final boolean grpDep, final String[] grpValue, final Option grpOpt) throws ParseException {
497 final Options options = new Options().addOptionGroup(optionGroup);
498 final List<Option> handler = new ArrayList<>();
499 final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options, args);
500 final OptionGroup otherGroup = new OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().get())
501 .addOption(Option.builder().option("p").longOpt("part").hasArg().get());
502 final OptionGroup nullGroup = null;
503
504
505 assertArrayEquals(optValue, commandLine.getOptionValues(asChar(opt)));
506 checkHandler(optDep, handler, opt);
507
508
509 assertArrayEquals(optValue, commandLine.getOptionValues(opt.getOpt()));
510 checkHandler(optDep, handler, opt);
511
512
513 assertArrayEquals(optValue, commandLine.getOptionValues(opt.getLongOpt()));
514 checkHandler(optDep, handler, opt);
515
516
517 assertArrayEquals(optValue, commandLine.getOptionValues(opt));
518 checkHandler(optDep, handler, opt);
519
520
521 assertArrayEquals(grpValue, commandLine.getOptionValues(optionGroup));
522 checkHandler(grpDep, handler, grpOpt);
523
524
525 assertNull(commandLine.getOptionValues("Nope"));
526 checkHandler(false, handler, opt);
527
528
529 assertNull(commandLine.getOptionValues(otherGroup));
530 checkHandler(false, handler, grpOpt);
531
532
533 assertNull(commandLine.getOptionValues(nullGroup));
534 checkHandler(false, handler, grpOpt);
535 }
536
537 @ParameterizedTest(name = "{0}, {1}")
538 @MethodSource("createParsedOptionValueParameters")
539 void testGetParsedOptionValue(final String[] args, final Option opt, final OptionGroup optionGroup, final boolean optDep,
540 final Integer optValue, final boolean grpDep, final Integer grpValue, final Option grpOpt) throws ParseException {
541 final Options options = new Options().addOptionGroup(optionGroup);
542 final List<Option> handler = new ArrayList<>();
543 final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options, args);
544 final Supplier<Integer> thinger = () -> 2;
545 final OptionGroup otherGroup = new OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().get())
546 .addOption(Option.builder().option("p").longOpt("part").hasArg().get());
547 final OptionGroup nullGroup = null;
548 final Integer thing = 2;
549
550
551 assertEquals(optValue, commandLine.getParsedOptionValue(asChar(opt)));
552 checkHandler(optDep, handler, opt);
553
554 assertEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValue(asChar(opt), thing));
555 checkHandler(optDep, handler, opt);
556
557 assertEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValue(asChar(opt), thinger));
558 checkHandler(optDep, handler, opt);
559
560
561 assertEquals(optValue, commandLine.getParsedOptionValue(opt.getOpt()));
562 checkHandler(optDep, handler, opt);
563
564 assertEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValue(opt.getOpt(), thing));
565 checkHandler(optDep, handler, opt);
566
567 assertEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValue(opt.getOpt(), thinger));
568 checkHandler(optDep, handler, opt);
569
570
571 assertEquals(optValue, commandLine.getParsedOptionValue(opt.getLongOpt()));
572 checkHandler(optDep, handler, opt);
573
574 assertEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValue(opt.getLongOpt(), thing));
575 checkHandler(optDep, handler, opt);
576
577 assertEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValue(opt.getLongOpt(), thinger));
578 checkHandler(optDep, handler, opt);
579
580
581 assertEquals(optValue, commandLine.getParsedOptionValue(opt));
582 checkHandler(optDep, handler, opt);
583
584 assertEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValue(opt, thing));
585 checkHandler(optDep, handler, opt);
586
587 assertEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValue(opt, thinger));
588 checkHandler(optDep, handler, opt);
589
590
591 assertEquals(grpValue, commandLine.getParsedOptionValue(optionGroup));
592 checkHandler(grpDep, handler, grpOpt);
593
594 assertEquals(grpValue == null ? thing : grpValue, commandLine.getParsedOptionValue(optionGroup, thing));
595 checkHandler(grpDep, handler, grpOpt);
596
597 assertEquals(grpValue == null ? thing : grpValue, commandLine.getParsedOptionValue(optionGroup, thinger));
598 checkHandler(grpDep, handler, grpOpt);
599
600
601 assertNull(commandLine.getParsedOptionValue(otherGroup));
602 checkHandler(false, handler, grpOpt);
603
604 assertEquals(thing, commandLine.getParsedOptionValue(otherGroup, thing));
605 checkHandler(false, handler, grpOpt);
606
607 assertEquals(thing, commandLine.getParsedOptionValue(otherGroup, thinger));
608 checkHandler(false, handler, grpOpt);
609
610
611 assertNull(commandLine.getParsedOptionValue(nullGroup));
612 checkHandler(false, handler, grpOpt);
613
614 assertEquals(thing, commandLine.getParsedOptionValue(nullGroup, thing));
615 checkHandler(false, handler, grpOpt);
616
617 assertEquals(thing, commandLine.getParsedOptionValue(nullGroup, thinger));
618 checkHandler(false, handler, grpOpt);
619
620
621 assertNull(commandLine.getParsedOptionValue("Nope"));
622 checkHandler(false, handler, opt);
623
624 assertEquals(thing, commandLine.getParsedOptionValue("Nope", thing));
625 checkHandler(false, handler, opt);
626
627 assertEquals(thing, commandLine.getParsedOptionValue("Nope", thinger));
628 checkHandler(false, handler, opt);
629 }
630
631 @ParameterizedTest(name = "{0}, {1}")
632 @MethodSource("createParsedOptionValuesParameters")
633 void testGetParsedOptionValues(final String[] args, final Option opt, final OptionGroup optionGroup, final boolean optDep,
634 final Integer[] optValue, final boolean grpDep, final Integer[] grpValue, final Option grpOpt) throws ParseException {
635 final Options options = new Options().addOptionGroup(optionGroup);
636 final List<Option> handler = new ArrayList<>();
637 final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options, args);
638 final Supplier<Integer[]> thinger = () -> new Integer[]{2, 3};
639 final OptionGroup otherGroup = new OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().get())
640 .addOption(Option.builder().option("p").longOpt("part").hasArg().get());
641 final OptionGroup nullGroup = null;
642 final Integer[] thing = {2, 3};
643
644
645 assertArrayEquals(optValue, commandLine.getParsedOptionValues(asChar(opt)));
646 checkHandler(optDep, handler, opt);
647
648 assertArrayEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValues(asChar(opt), thing));
649 checkHandler(optDep, handler, opt);
650
651 assertArrayEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValues(asChar(opt), thinger));
652 checkHandler(optDep, handler, opt);
653
654
655 assertArrayEquals(optValue, commandLine.getParsedOptionValues(opt.getOpt()));
656 checkHandler(optDep, handler, opt);
657
658 assertArrayEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValues(opt.getOpt(), thing));
659 checkHandler(optDep, handler, opt);
660
661 assertArrayEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValues(opt.getOpt(), thinger));
662 checkHandler(optDep, handler, opt);
663
664
665 assertArrayEquals(optValue, commandLine.getParsedOptionValues(opt.getLongOpt()));
666 checkHandler(optDep, handler, opt);
667
668 assertArrayEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValues(opt.getLongOpt(), thing));
669 checkHandler(optDep, handler, opt);
670
671 assertArrayEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValues(opt.getLongOpt(), thinger));
672 checkHandler(optDep, handler, opt);
673
674
675 assertArrayEquals(optValue, commandLine.getParsedOptionValues(opt));
676 checkHandler(optDep, handler, opt);
677
678 assertArrayEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValues(opt, thing));
679 checkHandler(optDep, handler, opt);
680
681 assertArrayEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValues(opt, thinger));
682 checkHandler(optDep, handler, opt);
683
684
685 assertArrayEquals(grpValue, commandLine.getParsedOptionValues(optionGroup));
686 checkHandler(grpDep, handler, grpOpt);
687
688 assertArrayEquals(grpValue == null ? thing : grpValue, commandLine.getParsedOptionValues(optionGroup, thing));
689 checkHandler(grpDep, handler, grpOpt);
690
691 assertArrayEquals(grpValue == null ? thing : grpValue, commandLine.getParsedOptionValues(optionGroup, thinger));
692 checkHandler(grpDep, handler, grpOpt);
693
694
695 assertNull(commandLine.getParsedOptionValues(otherGroup));
696 checkHandler(false, handler, grpOpt);
697
698 assertArrayEquals(thing, commandLine.getParsedOptionValues(otherGroup, thing));
699 checkHandler(false, handler, grpOpt);
700
701 assertArrayEquals(thing, commandLine.getParsedOptionValues(otherGroup, thinger));
702 checkHandler(false, handler, grpOpt);
703
704
705 assertNull(commandLine.getParsedOptionValues(nullGroup));
706 checkHandler(false, handler, grpOpt);
707
708 assertArrayEquals(thing, commandLine.getParsedOptionValues(nullGroup, thing));
709 checkHandler(false, handler, grpOpt);
710
711 assertArrayEquals(thing, commandLine.getParsedOptionValues(nullGroup, thinger));
712 checkHandler(false, handler, grpOpt);
713
714
715 assertNull(commandLine.getParsedOptionValues("Nope"));
716 checkHandler(false, handler, opt);
717
718 assertArrayEquals(thing, commandLine.getParsedOptionValues("Nope", thing));
719 checkHandler(false, handler, opt);
720
721 assertArrayEquals(thing, commandLine.getParsedOptionValues("Nope", thinger));
722 checkHandler(false, handler, opt);
723 }
724
725
726
727
728
729
730
731
732
733
734
735
736
737 @ParameterizedTest(name = "{0}, {1}")
738 @MethodSource("createHasOptionParameters")
739 void testHasOption(final String[] args, final Option opt, final OptionGroup optionGroup, final boolean optDep,
740 final boolean has, final boolean grpDep, final boolean hasGrp, final Option grpOpt) throws ParseException {
741 final Options options = new Options().addOptionGroup(optionGroup);
742 final List<Option> handler = new ArrayList<>();
743 final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options, args);
744 final OptionGroup otherGroup = new OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().get())
745 .addOption(Option.builder().option("p").longOpt("part").hasArg().get());
746 final OptionGroup nullGroup = null;
747
748
749 assertEquals(has, commandLine.hasOption(asChar(opt)));
750 checkHandler(optDep, handler, opt);
751
752
753 assertEquals(has, commandLine.hasOption(opt.getOpt()));
754 checkHandler(optDep, handler, opt);
755
756
757 assertEquals(has, commandLine.hasOption(opt.getLongOpt()));
758 checkHandler(optDep, handler, opt);
759
760
761 assertEquals(has, commandLine.hasOption(opt));
762 checkHandler(optDep, handler, opt);
763
764
765 assertEquals(hasGrp, commandLine.hasOption(optionGroup));
766 checkHandler(grpDep, handler, grpOpt);
767
768
769 assertFalse(commandLine.hasOption(otherGroup));
770 checkHandler(false, handler, grpOpt);
771
772
773 assertFalse(commandLine.hasOption(nullGroup));
774 checkHandler(false, handler, grpOpt);
775
776
777 assertFalse(commandLine.hasOption("Nope"));
778 checkHandler(false, handler, opt);
779 }
780
781
782
783
784
785
786
787
788
789
790
791
792
793 @ParameterizedTest(name = "{0}, {1}")
794 @MethodSource("createHasOptionParameters")
795 void testHasOptionNoDeprecationHandler(final String[] args, final Option opt, final OptionGroup optionGroup, final boolean optDep,
796 final boolean has, final boolean grpDep, final boolean hasGrp, final Option grpOpt) throws ParseException {
797 final Options options = new Options().addOptionGroup(optionGroup);
798 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
799 final CommandLine commandLine = DefaultParser.builder().get().parse(options, args);
800 final PrintStream ps = System.out;
801 try {
802 System.setOut(new PrintStream(baos));
803
804
805 assertEquals(has, commandLine.hasOption(asChar(opt)));
806 assertWritten(optDep, baos);
807
808
809 assertEquals(has, commandLine.hasOption(opt.getOpt()));
810 assertWritten(optDep, baos);
811
812
813 assertEquals(has, commandLine.hasOption(opt.getLongOpt()));
814 assertWritten(optDep, baos);
815
816
817 assertEquals(has, commandLine.hasOption(opt));
818 assertWritten(optDep, baos);
819
820
821 assertEquals(hasGrp, commandLine.hasOption(optionGroup));
822 assertWritten(grpDep, baos);
823
824
825 assertFalse(commandLine.hasOption("Nope"));
826 assertWritten(false, baos);
827 } finally {
828 System.setOut(ps);
829 }
830 }
831
832
833
834
835
836
837
838
839
840
841
842
843
844 @ParameterizedTest(name = "{0}, {1}")
845 @MethodSource("createHasOptionParameters")
846 void testHasOptionNullDeprecationHandler(final String[] args, final Option opt, final OptionGroup optionGroup, final boolean optDep,
847 final boolean has, final boolean grpDep, final boolean hasGrp, final Option grpOpt) throws ParseException {
848 final Options options = new Options().addOptionGroup(optionGroup);
849 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
850 final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(null).get().parse(options, args);
851 final PrintStream ps = System.out;
852 try {
853 System.setOut(new PrintStream(baos));
854
855
856 assertEquals(has, commandLine.hasOption(asChar(opt)));
857 assertWritten(false, baos);
858
859
860 assertEquals(has, commandLine.hasOption(opt.getOpt()));
861 assertWritten(false, baos);
862
863
864 assertEquals(has, commandLine.hasOption(opt.getLongOpt()));
865 assertWritten(false, baos);
866
867
868 assertEquals(has, commandLine.hasOption(opt));
869 assertWritten(false, baos);
870
871
872 assertEquals(hasGrp, commandLine.hasOption(optionGroup));
873 assertWritten(false, baos);
874
875
876 assertFalse(commandLine.hasOption("Nope"));
877 assertWritten(false, baos);
878 } finally {
879 System.setOut(ps);
880 }
881 }
882
883 @ParameterizedTest(name = "{0}, {1}")
884 @MethodSource("createOptionValueParameters")
885 void testNoDeprecationHandler(final String[] args, final Option opt, final OptionGroup optionGroup, final boolean optDep,
886 final String optValue, final boolean grpDep, final String grpValue, final Option grpOpt) throws ParseException {
887 final Options options = new Options().addOptionGroup(optionGroup);
888 final CommandLine commandLine = DefaultParser.builder().get().parse(options, args);
889 final Supplier<String> thinger = () -> "thing";
890 final Supplier<String> nullSupplier = null;
891 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
892 final PrintStream ps = System.out;
893 try {
894 System.setOut(new PrintStream(baos));
895
896 final OptionGroup otherGroup = new OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().get())
897 .addOption(Option.builder().option("p").longOpt("part").hasArg().get());
898 final OptionGroup nullGroup = null;
899
900
901 assertEquals(optValue, commandLine.getOptionValue(asChar(opt)));
902 assertWritten(optDep, baos);
903
904 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(asChar(opt), "thing"));
905 assertWritten(optDep, baos);
906
907 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(asChar(opt), thinger));
908 assertWritten(optDep, baos);
909
910 assertEquals(optValue, commandLine.getOptionValue(asChar(opt), nullSupplier));
911 assertWritten(optDep, baos);
912
913
914 assertEquals(optValue, commandLine.getOptionValue(opt.getOpt()));
915 assertWritten(optDep, baos);
916
917 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt.getOpt(), "thing"));
918 assertWritten(optDep, baos);
919
920 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt.getOpt(), thinger));
921 assertWritten(optDep, baos);
922
923 assertEquals(optValue, commandLine.getOptionValue(opt.getOpt(), nullSupplier));
924 assertWritten(optDep, baos);
925
926
927 assertEquals(optValue, commandLine.getOptionValue(opt.getLongOpt()));
928 assertWritten(optDep, baos);
929
930 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt.getLongOpt(), "thing"));
931 assertWritten(optDep, baos);
932
933 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt.getLongOpt(), thinger));
934 assertWritten(optDep, baos);
935
936 assertEquals(optValue, commandLine.getOptionValue(opt.getLongOpt(), nullSupplier));
937 assertWritten(optDep, baos);
938
939
940 assertEquals(optValue, commandLine.getOptionValue(opt));
941 assertWritten(optDep, baos);
942
943 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt, "thing"));
944 assertWritten(optDep, baos);
945
946 assertEquals(optValue == null ? "thing" : optValue, commandLine.getOptionValue(opt, thinger));
947 assertWritten(optDep, baos);
948
949 assertEquals(optValue, commandLine.getOptionValue(opt, nullSupplier));
950 assertWritten(optDep, baos);
951
952
953 assertEquals(grpValue, commandLine.getOptionValue(optionGroup));
954 assertWritten(grpDep, baos);
955
956 assertEquals(grpValue == null ? "thing" : grpValue, commandLine.getOptionValue(optionGroup, "thing"));
957 assertWritten(grpDep, baos);
958
959 assertEquals(grpValue == null ? "thing" : grpValue, commandLine.getOptionValue(optionGroup, thinger));
960 assertWritten(grpDep, baos);
961
962 assertEquals(grpValue, commandLine.getOptionValue(optionGroup, nullSupplier));
963 assertWritten(grpDep, baos);
964
965
966 assertNull(commandLine.getOptionValue(otherGroup));
967 assertWritten(false, baos);
968
969 assertEquals("thing", commandLine.getOptionValue(otherGroup, "thing"));
970 assertWritten(false, baos);
971
972 assertEquals("thing", commandLine.getOptionValue(otherGroup, thinger));
973 assertWritten(false, baos);
974
975 assertNull(commandLine.getOptionValue(otherGroup, nullSupplier));
976 assertWritten(false, baos);
977
978
979 assertNull(commandLine.getOptionValue(nullGroup));
980 assertWritten(false, baos);
981
982 assertEquals("thing", commandLine.getOptionValue(nullGroup, "thing"));
983 assertWritten(false, baos);
984
985 assertEquals("thing", commandLine.getOptionValue(nullGroup, thinger));
986 assertWritten(false, baos);
987
988 assertNull(commandLine.getOptionValue(nullGroup, nullSupplier));
989 assertWritten(false, baos);
990
991
992 assertNull(commandLine.getOptionValue("Nope"));
993 assertWritten(false, baos);
994
995 assertEquals("thing", commandLine.getOptionValue("Nope", "thing"));
996 assertWritten(false, baos);
997
998 assertEquals("thing", commandLine.getOptionValue("Nope", thinger));
999 assertWritten(false, baos);
1000
1001 assertNull(commandLine.getOptionValue("Nope", nullSupplier));
1002 assertWritten(false, baos);
1003 } finally {
1004 System.setOut(ps);
1005 }
1006 }
1007
1008 @Test
1009 void testNullOption() throws Exception {
1010 final Options options = new Options();
1011 final Option optI = Option.builder("i").hasArg().type(Number.class).get();
1012 final Option optF = Option.builder("f").hasArg().get();
1013 options.addOption(optI);
1014 options.addOption(optF);
1015 final CommandLineParser parser = new DefaultParser();
1016 final CommandLine cmd = parser.parse(options, new String[] {"-i", "123", "-f", "foo"});
1017 assertNull(cmd.getOptionValue((Option) null));
1018 assertNull(cmd.getParsedOptionValue((Option) null));
1019 assertNull(cmd.getOptionValue((OptionGroup) null));
1020 assertNull(cmd.getParsedOptionValue((OptionGroup) null));
1021 }
1022 }