1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2.bug;
18
19 import java.util.Arrays;
20 import java.util.List;
21
22 import junit.framework.TestCase;
23
24 import org.apache.commons.cli2.CommandLine;
25 import org.apache.commons.cli2.Group;
26 import org.apache.commons.cli2.builder.ArgumentBuilder;
27 import org.apache.commons.cli2.builder.DefaultOptionBuilder;
28 import org.apache.commons.cli2.builder.GroupBuilder;
29 import org.apache.commons.cli2.commandline.Parser;
30 import org.apache.commons.cli2.option.DefaultOption;
31
32
33
34
35 public class BugCLI158Test extends TestCase {
36
37 private Parser createDefaultValueParser(String[] defaults) {
38 final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
39 final ArgumentBuilder abuilder = new ArgumentBuilder();
40 final GroupBuilder gbuilder = new GroupBuilder();
41
42 DefaultOption bOption = obuilder.withShortName("b")
43 .withLongName("b")
44 .withArgument(abuilder.withName("b")
45 .withMinimum(0)
46 .withMaximum(defaults.length)
47 .withDefaults(Arrays.asList(defaults))
48 .create())
49 .create();
50
51 Group options = gbuilder
52 .withName("options")
53 .withOption(bOption)
54 .create();
55
56 Parser parser = new Parser();
57 parser.setHelpTrigger("--help");
58 parser.setGroup(options);
59 return parser;
60 }
61
62 public void testSingleOptionSingleArgument() throws Exception {
63 Parser parser = createDefaultValueParser(new String[]{"100", "1000"});
64 String enteredValue1 = "1";
65 String[] args = new String[]{"-b", enteredValue1};
66 CommandLine cl = parser.parse(args);
67 CommandLine cmd = cl;
68 assertNotNull(cmd);
69 List b = cmd.getValues("-b");
70 assertEquals("[" + enteredValue1 + ", 1000]", b + "");
71 }
72
73 public void testSingleOptionNoArgument() throws Exception {
74 Parser parser = createDefaultValueParser(new String[]{"100", "1000"});
75 String[] args = new String[]{"-b"};
76 CommandLine cl = parser.parse(args);
77 CommandLine cmd = cl;
78 assertNotNull(cmd);
79 List b = cmd.getValues("-b");
80 assertEquals("[100, 1000]", b + "");
81 }
82
83 public void testSingleOptionMaximumNumberOfArgument() throws Exception {
84 String[] args = new String[]{"-b", "1", "2"};
85 final ArgumentBuilder abuilder = new ArgumentBuilder();
86 final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
87 final GroupBuilder gbuilder = new GroupBuilder();
88
89 DefaultOption bOption = obuilder.withShortName("b")
90 .withLongName("b")
91 .withArgument(abuilder.withName("b")
92 .withMinimum(2)
93 .withMaximum(4)
94 .withDefault("100")
95 .withDefault("1000")
96 .withDefault("10000")
97 .create())
98 .create();
99
100 Group options = gbuilder
101 .withName("options")
102 .withOption(bOption)
103 .create();
104
105 Parser parser = new Parser();
106 parser.setHelpTrigger("--help");
107 parser.setGroup(options);
108 CommandLine cl = parser.parse(args);
109 CommandLine cmd = cl;
110 assertNotNull(cmd);
111 List b = cmd.getValues("-b");
112 assertEquals("[1, 2, 10000]", b + "");
113 }
114 }