View Javadoc
1   /*
2     Licensed to the Apache Software Foundation (ASF) under one or more
3     contributor license agreements.  See the NOTICE file distributed with
4     this work for additional information regarding copyright ownership.
5     The ASF licenses this file to You under the Apache License, Version 2.0
6     (the "License"); you may not use this file except in compliance with
7     the License.  You may obtain a copy of the License at
8   
9         http://www.apache.org/licenses/LICENSE-2.0
10  
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16   */
17  
18  package org.apache.commons.cli;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.stream.Stream;
26  
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.params.ParameterizedTest;
30  import org.junit.jupiter.params.provider.MethodSource;
31  
32  @SuppressWarnings("deprecation") // tests some deprecated classes
33  public class ValueTest {
34  
35      private static final Option NULL_OPTION = null;
36      private static final String NULL_STRING = null;
37  
38      protected static Stream<CommandLineParser> parsers() {
39          return Stream.of(new DefaultParser(), new PosixParser());
40      }
41  
42      private final Options opts = new Options();
43  
44      private CommandLine cl;
45  
46      @BeforeEach
47      public void setUp() throws Exception {
48          opts.addOption("a", false, "toggle -a");
49          opts.addOption("b", true, "set -b");
50          opts.addOption("c", "c", false, "toggle -c");
51          opts.addOption("d", "d", true, "set -d");
52  
53          opts.addOption(OptionBuilder.hasOptionalArg().create('e'));
54          opts.addOption(OptionBuilder.hasOptionalArg().withLongOpt("fish").create());
55          opts.addOption(OptionBuilder.hasOptionalArgs().withLongOpt("gravy").create());
56          opts.addOption(OptionBuilder.hasOptionalArgs(2).withLongOpt("hide").create());
57          opts.addOption(OptionBuilder.hasOptionalArgs(2).create('i'));
58          opts.addOption(OptionBuilder.hasOptionalArgs().create('j'));
59          opts.addOption(Option.builder().option("v").hasArg().valueSeparator().build());
60  
61          final String[] args = { "-a", "-b", "foo", "--c", "--d", "bar" };
62  
63          cl = new PosixParser().parse(opts, args);
64      }
65  
66      @Test
67      public void testLongNoArg() {
68          assertTrue(cl.hasOption("c"));
69          assertNull(cl.getOptionValue("c"));
70      }
71  
72      @Test
73      public void testLongNoArgWithOption() {
74          assertTrue(cl.hasOption(opts.getOption("c")));
75          assertNull(cl.getOptionValue(opts.getOption("c")));
76      }
77  
78      @ParameterizedTest
79      @MethodSource("parsers")
80      public void testLongOptionalArgValue(final CommandLineParser parser) throws Exception {
81          final String[] args = { "--fish", "face" };
82          final CommandLine cmd = parser.parse(opts, args);
83          assertTrue(cmd.hasOption("fish"));
84          assertEquals("face", cmd.getOptionValue("fish"));
85      }
86  
87      @ParameterizedTest
88      @MethodSource("parsers")
89      public void testLongOptionalArgValues(final CommandLineParser parser) throws Exception {
90          final String[] args = { "--gravy", "gold", "garden" };
91          final CommandLine cmd = parser.parse(opts, args);
92          assertNull(cmd.getOptionValues(NULL_OPTION));
93          assertNull(cmd.getOptionValues(NULL_STRING));
94          assertTrue(cmd.hasOption("gravy"));
95          assertEquals("gold", cmd.getOptionValue("gravy"));
96          assertEquals("gold", cmd.getOptionValues("gravy")[0]);
97          assertEquals("garden", cmd.getOptionValues("gravy")[1]);
98          assertEquals(cmd.getArgs().length, 0);
99      }
100 
101     @ParameterizedTest
102     @MethodSource("parsers")
103     public void testLongOptionalArgValuesWithOption(final CommandLineParser parser) throws Exception {
104         final String[] args = { "--gravy", "gold", "garden" };
105         final CommandLine cmd = parser.parse(opts, args);
106         assertNull(cmd.getOptionValues(NULL_OPTION));
107         assertNull(cmd.getOptionValues(NULL_STRING));
108         assertTrue(cmd.hasOption(opts.getOption("gravy")));
109         assertEquals("gold", cmd.getOptionValue(opts.getOption("gravy")));
110         assertEquals("gold", cmd.getOptionValues(opts.getOption("gravy"))[0]);
111         assertEquals("garden", cmd.getOptionValues(opts.getOption("gravy"))[1]);
112         assertEquals(cmd.getArgs().length, 0);
113     }
114 
115     @ParameterizedTest
116     @MethodSource("parsers")
117     public void testLongOptionalArgValueWithOption(final CommandLineParser parser) throws Exception {
118         final String[] args = { "--fish", "face" };
119         final CommandLine cmd = parser.parse(opts, args);
120         assertTrue(cmd.hasOption(opts.getOption("fish")));
121         assertEquals("face", cmd.getOptionValue(opts.getOption("fish")));
122     }
123 
124     @ParameterizedTest
125     @MethodSource("parsers")
126     public void testLongOptionalNArgValues(final CommandLineParser parser) throws Exception {
127         final String[] args = { "--hide", "house", "hair", "head" };
128         final CommandLine cmd = parser.parse(opts, args);
129         assertTrue(cmd.hasOption("hide"));
130         assertEquals("house", cmd.getOptionValue("hide"));
131         assertEquals("house", cmd.getOptionValues("hide")[0]);
132         assertEquals("hair", cmd.getOptionValues("hide")[1]);
133         assertEquals(cmd.getArgs().length, 1);
134         assertEquals("head", cmd.getArgs()[0]);
135     }
136 
137     @ParameterizedTest
138     @MethodSource("parsers")
139     public void testLongOptionalNArgValuesWithOption(final CommandLineParser parser) throws Exception {
140         final CommandLine cmd = parser.parse(opts, new String[] { "--hide", "house", "hair", "head" });
141         assertNull(cmd.getOptionValues(NULL_OPTION));
142         assertNull(cmd.getOptionValues(NULL_STRING));
143         assertTrue(cmd.hasOption(opts.getOption("hide")));
144         assertEquals("house", cmd.getOptionValue(opts.getOption("hide")));
145         assertEquals("house", cmd.getOptionValues(opts.getOption("hide"))[0]);
146         assertEquals("hair", cmd.getOptionValues(opts.getOption("hide"))[1]);
147         assertEquals(cmd.getArgs().length, 1);
148         assertEquals("head", cmd.getArgs()[0]);
149     }
150 
151     @ParameterizedTest
152     @MethodSource("parsers")
153     public void testLongOptionalNoValue(final CommandLineParser parser) throws Exception {
154         final String[] args = { "--fish" };
155         final CommandLine cmd = parser.parse(opts, args);
156         assertTrue(cmd.hasOption("fish"));
157         assertNull(cmd.getOptionValue("fish"));
158     }
159 
160     @ParameterizedTest
161     @MethodSource("parsers")
162     public void testLongOptionalNoValueWithOption(final CommandLineParser parser) throws Exception {
163         final String[] args = { "--fish" };
164         final CommandLine cmd = parser.parse(opts, args);
165         assertTrue(cmd.hasOption(opts.getOption("fish")));
166         assertNull(cmd.getOptionValue(opts.getOption("fish")));
167     }
168 
169     @Test
170     public void testLongWithArg() {
171         assertTrue(cl.hasOption("d"));
172         assertNotNull(cl.getOptionValue("d"));
173         assertEquals(cl.getOptionValue("d"), "bar");
174     }
175 
176     @Test
177     public void testLongWithArgWithOption() {
178         assertTrue(cl.hasOption(opts.getOption("d")));
179         assertNotNull(cl.getOptionValue(opts.getOption("d")));
180         assertEquals(cl.getOptionValue(opts.getOption("d")), "bar");
181     }
182 
183     @Test
184     public void testShortNoArg() {
185         assertTrue(cl.hasOption("a"));
186         assertNull(cl.getOptionValue("a"));
187     }
188 
189     @Test
190     public void testShortNoArgWithOption() {
191         assertTrue(cl.hasOption(opts.getOption("a")));
192         assertNull(cl.getOptionValue(opts.getOption("a")));
193     }
194 
195     @ParameterizedTest
196     @MethodSource("parsers")
197     public void testShortOptionalArgNoValue(final CommandLineParser parser) throws Exception {
198         final String[] args = { "-e" };
199         final CommandLine cmd = parser.parse(opts, args);
200         assertTrue(cmd.hasOption("e"));
201         assertNull(cmd.getOptionValue("e"));
202     }
203 
204     @ParameterizedTest
205     @MethodSource("parsers")
206     public void testShortOptionalArgNoValueWithOption(final CommandLineParser parser) throws Exception {
207         final String[] args = { "-e" };
208         final CommandLine cmd = parser.parse(opts, args);
209         assertTrue(cmd.hasOption(opts.getOption("e")));
210         assertNull(cmd.getOptionValue(opts.getOption("e")));
211     }
212 
213     @ParameterizedTest
214     @MethodSource("parsers")
215     public void testShortOptionalArgValue(final CommandLineParser parser) throws Exception {
216         final String[] args = { "-e", "everything" };
217         final CommandLine cmd = parser.parse(opts, args);
218         assertTrue(cmd.hasOption("e"));
219         assertEquals("everything", cmd.getOptionValue("e"));
220     }
221 
222     @ParameterizedTest
223     @MethodSource("parsers")
224     public void testShortOptionalArgValues(final CommandLineParser parser) throws Exception {
225         final String[] args = { "-j", "ink", "idea" };
226         final CommandLine cmd = parser.parse(opts, args);
227         assertTrue(cmd.hasOption("j"));
228         assertEquals("ink", cmd.getOptionValue("j"));
229         assertEquals("ink", cmd.getOptionValues("j")[0]);
230         assertEquals("idea", cmd.getOptionValues("j")[1]);
231         assertEquals(cmd.getArgs().length, 0);
232     }
233 
234     @ParameterizedTest
235     @MethodSource("parsers")
236     public void testShortOptionalArgValuesWithOption(final CommandLineParser parser) throws Exception {
237         final String[] args = { "-j", "ink", "idea" };
238         final CommandLine cmd = parser.parse(opts, args);
239         assertNull(cmd.getOptionValues(NULL_OPTION));
240         assertNull(cmd.getOptionValues(NULL_STRING));
241         assertTrue(cmd.hasOption(opts.getOption("j")));
242         assertEquals("ink", cmd.getOptionValue(opts.getOption("j")));
243         assertEquals("ink", cmd.getOptionValues(opts.getOption("j"))[0]);
244         assertEquals("idea", cmd.getOptionValues(opts.getOption("j"))[1]);
245         assertEquals(cmd.getArgs().length, 0);
246     }
247 
248     @ParameterizedTest
249     @MethodSource("parsers")
250     public void testShortOptionalArgValueWithOption(final CommandLineParser parser) throws Exception {
251         final String[] args = { "-e", "everything" };
252         final CommandLine cmd = parser.parse(opts, args);
253         assertNull(cmd.getOptionValues(NULL_OPTION));
254         assertNull(cmd.getOptionValues(NULL_STRING));
255         assertTrue(cmd.hasOption(opts.getOption("e")));
256         assertEquals("everything", cmd.getOptionValue(opts.getOption("e")));
257     }
258 
259     @ParameterizedTest
260     @MethodSource("parsers")
261     public void testShortOptionalNArgValues(final CommandLineParser parser) throws Exception {
262         final String[] args = { "-i", "ink", "idea", "isotope", "ice" };
263         final CommandLine cmd = parser.parse(opts, args);
264         assertNull(cmd.getOptionValues(NULL_OPTION));
265         assertNull(cmd.getOptionValues(NULL_STRING));
266         assertTrue(cmd.hasOption("i"));
267         assertEquals("ink", cmd.getOptionValue("i"));
268         assertEquals("ink", cmd.getOptionValues("i")[0]);
269         assertEquals("idea", cmd.getOptionValues("i")[1]);
270         assertEquals(cmd.getArgs().length, 2);
271         assertEquals("isotope", cmd.getArgs()[0]);
272         assertEquals("ice", cmd.getArgs()[1]);
273     }
274 
275     @Test
276     public void testShortOptionalNArgValuesSeparated() throws Exception {
277         final String[] args = { "-v=ink", "-v=idea", "-v=isotope", "-v=ice" };
278         final CommandLineParser parser = new DefaultParser();
279         final CommandLine cmd = parser.parse(opts, args);
280         assertNull(cmd.getOptionValues(NULL_OPTION));
281         assertNull(cmd.getOptionValues(NULL_STRING));
282         assertTrue(cmd.hasOption("v"));
283         assertEquals("ink", cmd.getOptionValue("v"));
284         assertEquals("ink", cmd.getOptionValues("v")[0]);
285         assertEquals("idea", cmd.getOptionValues("v")[1]);
286         assertEquals("isotope", cmd.getOptionValues("v")[2]);
287         assertEquals("ice", cmd.getOptionValues("v")[3]);
288     }
289 
290     @Test
291     public void testShortOptionalNArgValuesWithOption() throws Exception {
292         final String[] args = { "-i", "ink", "idea", "isotope", "ice" };
293         final CommandLineParser parser = new PosixParser();
294         final CommandLine cmd = parser.parse(opts, args);
295         assertNull(cmd.getOptionValues(NULL_OPTION));
296         assertNull(cmd.getOptionValues(NULL_STRING));
297         assertTrue(cmd.hasOption("i"));
298         assertEquals("ink", cmd.getOptionValue(opts.getOption("i")));
299         assertEquals("ink", cmd.getOptionValues(opts.getOption("i"))[0]);
300         assertEquals("idea", cmd.getOptionValues(opts.getOption("i"))[1]);
301         assertEquals(cmd.getArgs().length, 2);
302         assertEquals("isotope", cmd.getArgs()[0]);
303         assertEquals("ice", cmd.getArgs()[1]);
304     }
305 
306     @Test
307     public void testShortWithArg() {
308         assertTrue(cl.hasOption("b"));
309         assertNotNull(cl.getOptionValue("b"));
310         assertEquals(cl.getOptionValue("b"), "foo");
311     }
312 
313     @Test
314     public void testShortWithArgWithOption() {
315         assertTrue(cl.hasOption(opts.getOption("b")));
316         assertNotNull(cl.getOptionValue(opts.getOption("b")));
317         assertEquals(cl.getOptionValue(opts.getOption("b")), "foo");
318     }
319 }