1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli.bug;
18
19 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.fail;
23
24 import java.util.Properties;
25
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.CommandLineParser;
28 import org.apache.commons.cli.DefaultParser;
29 import org.apache.commons.cli.MissingArgumentException;
30 import org.apache.commons.cli.Option;
31 import org.apache.commons.cli.Options;
32 import org.apache.commons.cli.ParseException;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38 public class BugCLI312Test {
39 @Test
40 public void testNoOptionValues() {
41 final Option o1 = Option.builder("A").build();
42 final Option o2 = Option.builder().option("D").longOpt("define").numberOfArgs(2).valueSeparator('=').build();
43 final Options options = new Options().addOption(o1).addOption(o2);
44
45 final CommandLineParser parser = new DefaultParser();
46
47 assertThrows(MissingArgumentException.class, () -> parser.parse(options, "-D -A".split(" ")));
48 }
49
50 @Test
51 public void testPropertyStyleOption_withGetOptionProperties() throws ParseException {
52 final Option o1 = Option.builder().option("D").longOpt("define").numberOfArgs(2).valueSeparator('=').build();
53
54 final Options options = new Options();
55 options.addOption(o1);
56
57 final CommandLineParser parser = new DefaultParser();
58
59 final CommandLine cl = parser.parse(options, "-Dv -Dw=1 -D x=2 -D y -D z=3 other".split(" "));
60 assertArrayEquals(new String[] {"v", "w", "1", "x", "2", "y", "z", "3"}, cl.getOptionValues('D'));
61
62 final Properties properties = cl.getOptionProperties("D");
63 assertEquals("true", properties.getProperty("v"));
64 assertEquals("1", properties.getProperty("w"));
65 assertEquals("2", properties.getProperty("x"));
66 assertEquals("true", properties.getProperty("y"));
67 assertEquals("3", properties.getProperty("z"));
68 assertEquals(5, properties.size());
69 assertEquals("other", cl.getArgList().get(0));
70 }
71
72 @Test
73 public void testPropertyStyleOption_withGetOptions() throws ParseException {
74 final Option o1 = Option.builder().option("D").longOpt("define").numberOfArgs(2).valueSeparator('=').build();
75
76 final Options options = new Options();
77 options.addOption(o1);
78
79 final CommandLineParser parser = new DefaultParser();
80
81 final CommandLine cl = parser.parse(options, "-Dv -Dw=1 -D x=2 -D y -D z=3 other".split(" "));
82 assertArrayEquals(new String[] {"v", "w", "1", "x", "2", "y", "z", "3"}, cl.getOptionValues('D'));
83
84 int defineOptionsFound = 0;
85 for (final Option o : cl.getOptions()) {
86 if ("D".equals(o.getOpt())) {
87 defineOptionsFound++;
88
89 switch (defineOptionsFound) {
90 case 1:
91 assertArrayEquals(new String[] {"v"}, o.getValues());
92 break;
93 case 2:
94 assertArrayEquals(new String[] {"w", "1"}, o.getValues());
95 break;
96 case 3:
97 assertArrayEquals(new String[] {"x", "2"}, o.getValues());
98 break;
99 case 4:
100 assertArrayEquals(new String[] {"y"}, o.getValues());
101 break;
102 case 5:
103 assertArrayEquals(new String[] {"z", "3"}, o.getValues());
104 break;
105 default:
106 fail("Didn't expect " + defineOptionsFound + " occurrences of -D");
107 break;
108 }
109 }
110 }
111 assertEquals("other", cl.getArgList().get(0));
112 }
113 }