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.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27
28 @SuppressWarnings("deprecation")
29 public class ValuesTest {
30 private CommandLine cmd;
31
32 @BeforeEach
33 public void setUp() throws Exception {
34 final Options options = new Options();
35
36 options.addOption("a", false, "toggle -a");
37 options.addOption("b", true, "set -b");
38 options.addOption("c", "c", false, "toggle -c");
39 options.addOption("d", "d", true, "set -d");
40
41 options.addOption(OptionBuilder.withLongOpt("e").hasArgs().withDescription("set -e ").create('e'));
42 options.addOption("f", "f", false, "jk");
43 options.addOption(OptionBuilder.withLongOpt("g").hasArgs(2).withDescription("set -g").create('g'));
44 options.addOption(OptionBuilder.withLongOpt("h").hasArg().withDescription("set -h").create('h'));
45 options.addOption(OptionBuilder.withLongOpt("i").withDescription("set -i").create('i'));
46 options.addOption(OptionBuilder.withLongOpt("j").hasArgs().withDescription("set -j").withValueSeparator('=').create('j'));
47 options.addOption(OptionBuilder.withLongOpt("k").hasArgs().withDescription("set -k").withValueSeparator('=').create('k'));
48 options.addOption(OptionBuilder.withLongOpt("m").hasArgs().withDescription("set -m").withValueSeparator().create('m'));
49
50
51 final String[] args = {
52 "-a",
53 "-b", "foo",
54 "--c",
55 "--d", "bar",
56 "-e", "one", "two",
57 "-f",
58 "arg1", "arg2",
59 "-g", "val1", "val2", "arg3",
60 "-h", "val1", "-i",
61 "-h", "val2",
62 "-jkey=value",
63 "-j", "key=value",
64 "-kkey1=value1",
65 "-kkey2=value2",
66 "-mkey=value"
67 };
68
69
70 final CommandLineParser parser = new PosixParser();
71
72 cmd = parser.parse(options, args);
73 }
74
75 @Test
76 public void testCharSeparator() {
77
78 assertTrue(cmd.hasOption("j"), "Option j is not set");
79 assertTrue(cmd.hasOption('j'), "Option j is not set");
80 assertArrayEquals(new String[] {"key", "value", "key", "value"}, cmd.getOptionValues("j"));
81 assertArrayEquals(new String[] {"key", "value", "key", "value"}, cmd.getOptionValues('j'));
82
83 assertTrue(cmd.hasOption("k"), "Option k is not set");
84 assertTrue(cmd.hasOption('k'), "Option k is not set");
85 assertArrayEquals(new String[] {"key1", "value1", "key2", "value2"}, cmd.getOptionValues("k"));
86 assertArrayEquals(new String[] {"key1", "value1", "key2", "value2"}, cmd.getOptionValues('k'));
87
88 assertTrue(cmd.hasOption("m"), "Option m is not set");
89 assertTrue(cmd.hasOption('m'), "Option m is not set");
90 assertArrayEquals(new String[] {"key", "value"}, cmd.getOptionValues("m"));
91 assertArrayEquals(new String[] {"key", "value"}, cmd.getOptionValues('m'));
92 }
93
94 @Test
95 public void testComplexValues() {
96 assertTrue(cmd.hasOption("i"), "Option i is not set");
97 assertTrue(cmd.hasOption("h"), "Option h is not set");
98 assertArrayEquals(new String[] {"val1", "val2"}, cmd.getOptionValues("h"));
99 }
100
101 @Test
102 public void testExtraArgs() {
103 assertArrayEquals(new String[] {"arg1", "arg2", "arg3"}, cmd.getArgs(), "Extra args");
104 }
105
106 @Test
107 public void testMultipleArgValues() {
108 assertTrue(cmd.hasOption("e"), "Option e is not set");
109 assertArrayEquals(new String[] {"one", "two"}, cmd.getOptionValues("e"));
110 }
111
112 @Test
113 public void testShortArgs() {
114 assertTrue(cmd.hasOption("a"), "Option a is not set");
115 assertTrue(cmd.hasOption("c"), "Option c is not set");
116
117 assertNull(cmd.getOptionValues("a"));
118 assertNull(cmd.getOptionValues("c"));
119 }
120
121 @Test
122 public void testShortArgsWithValue() {
123 assertTrue(cmd.hasOption("b"), "Option b is not set");
124 assertEquals("foo", cmd.getOptionValue("b"));
125 assertEquals(1, cmd.getOptionValues("b").length);
126
127 assertTrue(cmd.hasOption("b"), "Option b is not set");
128 assertEquals("bar", cmd.getOptionValue("d"));
129 assertEquals(1, cmd.getOptionValues("d").length);
130 }
131
132 @Test
133 public void testTwoArgValues() {
134 assertTrue(cmd.hasOption("g"), "Option g is not set");
135 assertArrayEquals(new String[] {"val1", "val2"}, cmd.getOptionValues("g"));
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 }