1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.cli.bug;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotEquals;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.DefaultParser;
28 import org.apache.commons.cli.Option;
29 import org.apache.commons.cli.Options;
30 import org.junit.Before;
31 import org.junit.Test;
32
33
34
35
36
37
38 public class BugCLI265Test {
39
40 private DefaultParser parser;
41 private Options options;
42
43 @Before
44 public void setUp() {
45 parser = new DefaultParser();
46
47 final Option optionT1 = Option.builder("t1").hasArg().numberOfArgs(1).optionalArg(true).argName("t1_path").build();
48 final Option optionA = Option.builder("a").hasArg(false).build();
49 final Option optionB = Option.builder("b").hasArg(false).build();
50 final Option optionLast = Option.builder("last").hasArg(false).build();
51
52 options = new Options().addOption(optionT1).addOption(optionA).addOption(optionB).addOption(optionLast);
53 }
54
55 @Test
56 public void shouldParseConcatenatedShortOptions() throws Exception {
57 final String[] concatenatedShortOptions = {"-t1", "-ab"};
58
59 final CommandLine commandLine = parser.parse(options, concatenatedShortOptions);
60
61 assertTrue(commandLine.hasOption("t1"));
62 assertNull(commandLine.getOptionValue("t1"));
63 assertTrue(commandLine.hasOption("a"));
64 assertTrue(commandLine.hasOption("b"));
65 assertFalse(commandLine.hasOption("last"));
66 }
67
68 @Test
69 public void shouldParseShortOptionWithoutValue() throws Exception {
70 final String[] twoShortOptions = {"-t1", "-last"};
71
72 final CommandLine commandLine = parser.parse(options, twoShortOptions);
73
74 assertTrue(commandLine.hasOption("t1"));
75 assertNotEquals("Second option has been used as value for first option", "-last", commandLine.getOptionValue("t1"));
76 assertTrue("Second option has not been detected", commandLine.hasOption("last"));
77 }
78
79 @Test
80 public void shouldParseShortOptionWithValue() throws Exception {
81 final String[] shortOptionWithValue = {"-t1", "path/to/my/db"};
82
83 final CommandLine commandLine = parser.parse(options, shortOptionWithValue);
84
85 assertEquals("path/to/my/db", commandLine.getOptionValue("t1"));
86 assertFalse(commandLine.hasOption("last"));
87 }
88 }