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.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")
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 }