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.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.Test;
26  
27  @SuppressWarnings("deprecation") // tests some deprecated classes
28  public class ArgumentIsOptionTest {
29  
30      private Options options;
31      private CommandLineParser parser;
32  
33      @BeforeEach
34      public void setUp() {
35          options = new Options().addOption("p", false, "Option p").addOption("attr", true, "Option accepts argument");
36          parser = new PosixParser();
37      }
38  
39      @Test
40      public void testOption() throws Exception {
41          final String[] args = {"-p"};
42  
43          final CommandLine cl = parser.parse(options, args);
44          assertTrue(cl.hasOption("p"), "Confirm -p is set");
45          assertFalse(cl.hasOption("attr"), "Confirm -attr is not set");
46          assertEquals(0, cl.getArgs().length, "Confirm all arguments recognized");
47      }
48  
49      @Test
50      public void testOptionAndOptionWithArgument() throws Exception {
51          final String[] args = {"-p", "-attr", "p"};
52  
53          final CommandLine cl = parser.parse(options, args);
54          assertTrue(cl.hasOption("p"), "Confirm -p is set");
55          assertTrue(cl.hasOption("attr"), "Confirm -attr is set");
56          assertEquals("p", cl.getOptionValue("attr"), "Confirm arg of -attr");
57          assertEquals(0, cl.getArgs().length, "Confirm all arguments recognized");
58      }
59  
60      @Test
61      public void testOptionWithArgument() throws Exception {
62          final String[] args = {"-attr", "p"};
63  
64          final CommandLine cl = parser.parse(options, args);
65          assertFalse(cl.hasOption("p"), "Confirm -p is set");
66          assertTrue(cl.hasOption("attr"), "Confirm -attr is set");
67          assertEquals("p", cl.getOptionValue("attr"), "Confirm arg of -attr");
68          assertEquals(0, cl.getArgs().length, "Confirm all arguments recognized");
69      }
70  }