1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2.application;
18
19 import java.util.ArrayList;
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.OptionException;
27 import org.apache.commons.cli2.builder.ArgumentBuilder;
28 import org.apache.commons.cli2.builder.DefaultOptionBuilder;
29 import org.apache.commons.cli2.builder.GroupBuilder;
30 import org.apache.commons.cli2.commandline.Parser;
31 import org.apache.commons.cli2.option.PropertyOption;
32
33
34 public class AntTest extends TestCase {
35 public void testAnt() throws OptionException {
36 final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
37 final ArgumentBuilder abuilder = new ArgumentBuilder();
38 final GroupBuilder gbuilder = new GroupBuilder();
39
40 final Group options =
41 gbuilder
42 .withName("ant")
43 .withOption(
44 obuilder
45 .withShortName("help")
46 .withDescription("print this message")
47 .create())
48 .withOption(
49 obuilder
50 .withShortName("projecthelp")
51 .withDescription("print project help information")
52 .create())
53 .withOption(
54 obuilder
55 .withShortName("version")
56 .withDescription("print the version information and exit")
57 .create())
58 .withOption(
59 obuilder
60 .withShortName("diagnostics")
61 .withDescription("print information that might be helpful to diagnose or report problems.")
62 .create())
63 .withOption(
64 obuilder
65 .withShortName("quiet")
66 .withShortName("q")
67 .withDescription("be extra quiet")
68 .create())
69 .withOption(
70 obuilder
71 .withShortName("verbose")
72 .withShortName("v")
73 .withDescription("be extra verbose")
74 .create())
75 .withOption(
76 obuilder
77 .withShortName("debug")
78 .withDescription("print debugging information")
79 .create())
80 .withOption(
81 obuilder
82 .withShortName("emacs")
83 .withDescription("produce logging information without adornments")
84 .create())
85 .withOption(
86 obuilder
87 .withShortName("logfile")
88 .withShortName("l")
89 .withDescription("use given file for log")
90 .withArgument(
91 abuilder
92 .withName("file")
93 .withMinimum(1)
94 .withMaximum(1)
95 .create())
96 .create())
97 .withOption(
98 obuilder
99 .withShortName("logger")
100 .withDescription("the class which is to perform logging")
101 .withArgument(
102 abuilder
103 .withName("classname")
104 .withMinimum(1)
105 .withMaximum(1)
106 .create())
107 .create())
108 .withOption(
109 obuilder
110 .withShortName("listener")
111 .withDescription("add an instance of class as a project listener")
112 .withArgument(
113 abuilder
114 .withName("classname")
115 .withMinimum(1)
116 .withMaximum(1)
117 .create())
118 .create())
119 .withOption(
120 obuilder
121 .withShortName("buildfile")
122 .withShortName("file")
123 .withShortName("f")
124 .withDescription("use given buildfile")
125 .withArgument(
126 abuilder
127 .withName("file")
128 .withMinimum(1)
129 .withMaximum(1)
130 .create())
131 .create())
132 .withOption(PropertyOption.INSTANCE)
133 .withOption(
134 obuilder
135 .withShortName("propertyfile")
136 .withDescription("load all properties from file with -D properties taking precedence")
137 .withArgument(
138 abuilder
139 .withName("name")
140 .withMinimum(1)
141 .withMaximum(1)
142 .create())
143 .create())
144 .withOption(
145 obuilder
146 .withShortName("inputhandler")
147 .withDescription("the class which will handle input requests")
148 .withArgument(
149 abuilder
150 .withName("class")
151 .withMinimum(1)
152 .withMaximum(1)
153 .create())
154 .create())
155 .withOption(
156 obuilder
157 .withShortName("find")
158 .withDescription("search for buildfile towards the root of the filesystem and use it")
159 .withArgument(
160 abuilder
161 .withName("file")
162 .withMinimum(1)
163 .withMaximum(1)
164 .create())
165 .create())
166 .withOption(abuilder.withName("target").create())
167 .create();
168
169 Parser parser = new Parser();
170 parser.setGroup(options);
171 CommandLine line =
172 parser.parse(
173 new String[] {
174 "-buildfile",
175 "mybuild.xml",
176 "-Dproperty=value",
177 "-Dproperty1=value1",
178 "-projecthelp",
179 "compile",
180 "docs" });
181
182
183 assertEquals(2, line.getProperties().size());
184 assertEquals("value", line.getProperty("property"));
185 assertEquals("value1", line.getProperty("property1"));
186
187
188 assertEquals("mybuild.xml", line.getValue("-buildfile"));
189 assertTrue(line.hasOption("-projecthelp"));
190 assertFalse(line.hasOption("-help"));
191
192 assertTrue(line.hasOption("target"));
193 final List targets = new ArrayList();
194 targets.add("compile");
195 targets.add("docs");
196 assertEquals(targets, line.getValues("target"));
197 }
198 }