|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| package edu.rice.cs.plt.text; |
|
36 |
| |
|
37 |
| import java.util.Map; |
|
38 |
| import java.util.HashMap; |
|
39 |
| import java.util.List; |
|
40 |
| import java.util.LinkedList; |
|
41 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
42 |
| import edu.rice.cs.plt.tuple.Pair; |
|
43 |
| import edu.rice.cs.plt.tuple.Triple; |
|
44 |
| import edu.rice.cs.plt.tuple.Quad; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| public class ArgumentParser { |
|
57 |
| |
|
58 |
| private final Map<String, Integer> _supportedOptions; |
|
59 |
| private final HashMap<String, Iterable<String>> _defaults; |
|
60 |
| private final Map<String, String> _aliases; |
|
61 |
| private boolean _strictOrder; |
|
62 |
| private int _requiredParams; |
|
63 |
| |
|
64 |
| |
|
65 |
4
| public ArgumentParser() {
|
|
66 |
4
| _supportedOptions = new HashMap<String, Integer>();
|
|
67 |
4
| _defaults = new HashMap<String, Iterable<String>>();
|
|
68 |
4
| _aliases = new HashMap<String, String>();
|
|
69 |
4
| _strictOrder = false;
|
|
70 |
4
| _requiredParams = 0;
|
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
1
| public void requireStrictOrder() { _strictOrder = true; }
|
|
75 |
| |
|
76 |
| |
|
77 |
2
| public void requireParams(int count) { _requiredParams = count; }
|
|
78 |
| |
|
79 |
| |
|
80 |
8
| public boolean supportsOption(String name) {
|
|
81 |
8
| return _supportedOptions.containsKey(name) || _aliases.containsKey(name);
|
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
7
| public void supportOption(String name, int arity) {
|
|
91 |
0
| if (supportsOption(name)) { throw new IllegalArgumentException(name + " is already supported"); }
|
|
92 |
7
| _supportedOptions.put(name, arity);
|
|
93 |
| } |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
3
| public void supportOption(String name, String... defaultArguments) {
|
|
104 |
3
| supportOption(name, defaultArguments.length);
|
|
105 |
3
| if (defaultArguments.length > 0) {
|
|
106 |
2
| _defaults.put(name, IterUtil.asIterable(defaultArguments));
|
|
107 |
| } |
|
108 |
| } |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
1
| public void supportVarargOption(String name) {
|
|
117 |
0
| if (supportsOption(name)) { throw new IllegalArgumentException(name + " is already supported"); }
|
|
118 |
1
| _supportedOptions.put(name, -1);
|
|
119 |
| } |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
0
| public void supportVarargOption(String name, String... defaultArguments) {
|
|
132 |
0
| supportVarargOption(name);
|
|
133 |
0
| _defaults.put(name, IterUtil.asIterable(defaultArguments));
|
|
134 |
| } |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
0
| public void supportAlias(String aliasName, String optionName) {
|
|
143 |
0
| if (supportsOption(aliasName)) { throw new IllegalArgumentException(aliasName + " is already supported"); }
|
|
144 |
0
| if (!supportsOption(optionName)) { throw new IllegalArgumentException(optionName + " is not supported"); }
|
|
145 |
0
| if (_aliases.containsKey(optionName)) { optionName = _aliases.get(optionName); }
|
|
146 |
0
| _aliases.put(aliasName, optionName);
|
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
34
| public Result parse(String... args) throws IllegalArgumentException {
|
|
152 |
34
| @SuppressWarnings("unchecked")
|
|
153 |
| Map<String, Iterable<String>> options = (Map<String, Iterable<String>>) _defaults.clone(); |
|
154 |
| |
|
155 |
34
| List<String> params = new LinkedList<String>();
|
|
156 |
| |
|
157 |
34
| String currentOption = null;
|
|
158 |
34
| List<String> currentOptionArgs = null;
|
|
159 |
34
| int optionArgsCount = -1;
|
|
160 |
34
| boolean allowMoreOptions = true;
|
|
161 |
| |
|
162 |
34
| for (String s : args) {
|
|
163 |
115
| if (currentOption != null && optionArgsCount == 0) {
|
|
164 |
| |
|
165 |
24
| options.put(currentOption, IterUtil.asSizedIterable(currentOptionArgs));
|
|
166 |
24
| currentOption = null;
|
|
167 |
| } |
|
168 |
| |
|
169 |
115
| if (s.startsWith("-")) {
|
|
170 |
46
| String opt = s.substring(1);
|
|
171 |
0
| if (_aliases.containsKey(opt)) { opt = _aliases.get(opt); }
|
|
172 |
46
| if (optionArgsCount > 0) {
|
|
173 |
1
| throw new IllegalArgumentException("Expected " + optionArgsCount + " more argument(s) for option " +
|
|
174 |
| currentOption); |
|
175 |
| } |
|
176 |
45
| else if (!allowMoreOptions) {
|
|
177 |
1
| throw new IllegalArgumentException("Unexpected option: " + opt);
|
|
178 |
| } |
|
179 |
44
| else if (!_supportedOptions.containsKey(opt)) {
|
|
180 |
3
| throw new IllegalArgumentException("Unrecognized option: " + opt);
|
|
181 |
| } |
|
182 |
| else { |
|
183 |
41
| if (currentOption != null) {
|
|
184 |
| |
|
185 |
1
| options.put(currentOption, IterUtil.asSizedIterable(currentOptionArgs));
|
|
186 |
| } |
|
187 |
41
| currentOption = opt;
|
|
188 |
41
| currentOptionArgs = new LinkedList<String>();
|
|
189 |
41
| optionArgsCount = _supportedOptions.get(opt);
|
|
190 |
| } |
|
191 |
| } |
|
192 |
| |
|
193 |
| else { |
|
194 |
69
| if (currentOption == null) {
|
|
195 |
2
| if (_strictOrder) { allowMoreOptions = false; }
|
|
196 |
31
| params.add(s);
|
|
197 |
| } |
|
198 |
| else { |
|
199 |
38
| currentOptionArgs.add(s);
|
|
200 |
33
| if (optionArgsCount > 0) { optionArgsCount--; }
|
|
201 |
| } |
|
202 |
| } |
|
203 |
| } |
|
204 |
29
| if (optionArgsCount > 0) {
|
|
205 |
3
| throw new IllegalArgumentException("Expected " + optionArgsCount + " more argument(s) for option " +
|
|
206 |
| currentOption); |
|
207 |
| } |
|
208 |
26
| if (currentOption != null) {
|
|
209 |
| |
|
210 |
12
| options.put(currentOption, IterUtil.asSizedIterable(currentOptionArgs));
|
|
211 |
| } |
|
212 |
| |
|
213 |
26
| if (params.size() < _requiredParams) {
|
|
214 |
4
| throw new IllegalArgumentException("Expected at least " + _requiredParams + " parameter(s)");
|
|
215 |
| } |
|
216 |
22
| return new Result(options, params);
|
|
217 |
| } |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| public static class Result { |
|
222 |
| private final Map<String, Iterable<String>> _options; |
|
223 |
| private final Iterable<String> _params; |
|
224 |
| |
|
225 |
22
| public Result(Map<String, Iterable<String>> options, Iterable<String> params) {
|
|
226 |
22
| _options = options;
|
|
227 |
22
| _params = params;
|
|
228 |
| } |
|
229 |
| |
|
230 |
| |
|
231 |
22
| public Iterable<String> params() { return _params; }
|
|
232 |
| |
|
233 |
| |
|
234 |
13
| public boolean hasOption(String opt) { return _options.containsKey(opt); }
|
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
13
| public Iterable<String> getOption(String opt) { return _options.get(opt); }
|
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
12
| public boolean hasNullaryOption(String opt) {
|
|
247 |
12
| return _options.containsKey(opt) && IterUtil.isEmpty(_options.get(opt));
|
|
248 |
| } |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
11
| public String getUnaryOption(String opt) {
|
|
256 |
11
| Iterable<String> result = _options.get(opt);
|
|
257 |
0
| if (result == null || IterUtil.sizeOf(result) != 1) { return null; }
|
|
258 |
11
| else { return IterUtil.first(result); }
|
|
259 |
| } |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
12
| public Pair<String, String> getBinaryOption(String opt) {
|
|
267 |
12
| Iterable<String> result = _options.get(opt);
|
|
268 |
1
| if (result == null || IterUtil.sizeOf(result) != 2) { return null; }
|
|
269 |
11
| else { return IterUtil.makePair(result); }
|
|
270 |
| } |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
1
| public Triple<String, String, String> getTernaryOption(String opt) {
|
|
278 |
1
| Iterable<String> result = _options.get(opt);
|
|
279 |
1
| if (result == null || IterUtil.sizeOf(result) != 3) { return null; }
|
|
280 |
0
| else { return IterUtil.makeTriple(result); }
|
|
281 |
| } |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
1
| public Quad<String, String, String, String> getQuaternaryOption(String opt) {
|
|
289 |
1
| Iterable<String> result = _options.get(opt);
|
|
290 |
1
| if (result == null || IterUtil.sizeOf(result) != 4) { return null; }
|
|
291 |
0
| else { return IterUtil.makeQuad(result); }
|
|
292 |
| } |
|
293 |
| |
|
294 |
| } |
|
295 |
| |
|
296 |
| } |