-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptionBuilder.java
398 lines (341 loc) · 10.4 KB
/
OptionBuilder.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* Copyright 1999-2001,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.cli;
/**
* <p>OptionBuilder allows the user to create Options using descriptive
* methods.</p>
* <p>Details on the Builder pattern can be found at
* <a href="http://c2.com/cgi-bin/wiki?BuilderPattern">
* http://c2.com/cgi-bin/wiki?BuilderPattern</a>.</p>
*
* @author John Keyes (john at integralsource.com)
* @since 1.0
*/
public class OptionBuilder {
/** long option */
private static String longopt;
/** option description */
private static String description;
/** argument name */
private static String argName;
/** is required? */
private static boolean required;
/** the number of arguments */
private static int numberOfArgs = Option.UNINITIALIZED;
/** option type */
private static Object type;
/** option can have an optional argument value */
private static boolean optionalArg;
/** value separator for argument value */
private static char valuesep;
/** argument pattern and number of times that the pattern occures */
private static String argPattern;
private static int limit;
/** option builder instance */
private static OptionBuilder instance = new OptionBuilder();
/**
* private constructor to prevent instances being created
*/
private OptionBuilder()
{
// hide the constructor
}
/**
* Resets the member variables to their default values.
*/
private static void reset()
{
description = null;
argName = "arg";
longopt = null;
type = null;
required = false;
numberOfArgs = Option.UNINITIALIZED;
// PMM 9/6/02 - these were missing
optionalArg = false;
valuesep = (char) 0;
// reset argument pattern
argPattern = null;
limit = 0;
}
/**
* The next Option created will have the following long option value.
*
* @param newLongopt the long option value
* @return the OptionBuilder instance
*/
public static OptionBuilder withLongOpt(String newLongopt)
{
OptionBuilder.longopt = newLongopt;
return instance;
}
/**
* The next Option created will require an argument value.
*
* @return the OptionBuilder instance
*/
public static OptionBuilder hasArg()
{
OptionBuilder.numberOfArgs = 1;
return instance;
}
/**
* The next Option created will require an argument value if
* <code>hasArg</code> is true.
*
* @param hasArg if true then the Option has an argument value
* @return the OptionBuilder instance
*/
public static OptionBuilder hasArg(boolean hasArg)
{
OptionBuilder.numberOfArgs = (hasArg == true) ? 1 : Option.UNINITIALIZED;
return instance;
}
/**
* The next Option created will have the specified argument value
* name.
*
* @param name the name for the argument value
* @return the OptionBuilder instance
*/
public static OptionBuilder withArgName(String name)
{
OptionBuilder.argName = name;
return instance;
}
/**
* The next Option created will be required.
*
* @return the OptionBuilder instance
*/
public static OptionBuilder isRequired()
{
OptionBuilder.required = true;
return instance;
}
/**
* The next Option created uses <code>sep</code> as a means to
* separate argument values.
*
* <b>Example:</b>
* <pre>
* Option opt = OptionBuilder.withValueSeparator(':')
* .create('D');
*
* CommandLine line = parser.parse(args);
* String propertyName = opt.getValue(0);
* String propertyValue = opt.getValue(1);
* </pre>
*
* @param sep The value separator to be used for the argument values.
*
* @return the OptionBuilder instance
*/
public static OptionBuilder withValueSeparator(char sep)
{
OptionBuilder.valuesep = sep;
return instance;
}
/**
* The next Option created uses '<code>=</code>' as a means to
* separate argument values.
*
* <b>Example:</b>
* <pre>
* Option opt = OptionBuilder.withValueSeparator()
* .create('D');
*
* CommandLine line = parser.parse(args);
* String propertyName = opt.getValue(0);
* String propertyValue = opt.getValue(1);
* </pre>
*
* @return the OptionBuilder instance
*/
public static OptionBuilder withValueSeparator()
{
OptionBuilder.valuesep = '=';
return instance;
}
/**
* The next Option created will be required if <code>required</code>
* is true.
*
* @param newRequired if true then the Option is required
* @return the OptionBuilder instance
*/
public static OptionBuilder isRequired(boolean newRequired)
{
OptionBuilder.required = newRequired;
return instance;
}
/**
* The next Option created can have unlimited argument values.
*
* @return the OptionBuilder instance
*/
public static OptionBuilder hasArgs()
{
OptionBuilder.numberOfArgs = Option.UNLIMITED_VALUES;
return instance;
}
/**
* The next Option created can have <code>num</code>
* argument values.
*
* @param num the number of args that the option can have
* @return the OptionBuilder instance
*/
public static OptionBuilder hasArgs(int num)
{
OptionBuilder.numberOfArgs = num;
return instance;
}
/**
* The next Option can have an optional argument.
*
* @return the OptionBuilder instance
*/
public static OptionBuilder hasOptionalArg()
{
OptionBuilder.numberOfArgs = 1;
OptionBuilder.optionalArg = true;
return instance;
}
/**
* The next Option can have an unlimited number of
* optional arguments.
*
* @return the OptionBuilder instance
*/
public static OptionBuilder hasOptionalArgs()
{
OptionBuilder.numberOfArgs = Option.UNLIMITED_VALUES;
OptionBuilder.optionalArg = true;
return instance;
}
/**
* The next Option can have the specified number of
* optional arguments.
*
* @param numArgs - the maximum number of optional arguments
* the next Option created can have.
* @return the OptionBuilder instance
*/
public static OptionBuilder hasOptionalArgs(int numArgs)
{
OptionBuilder.numberOfArgs = numArgs;
OptionBuilder.optionalArg = true;
return instance;
}
/**
* The next Option created will have a value that will be an instance
* of <code>type</code>.
*
* @param newType the type of the Options argument value
* @return the OptionBuilder instance
*/
public static OptionBuilder withType(Object newType)
{
OptionBuilder.type = newType;
return instance;
}
/**
* The next Option created will have the specified description
*
* @param newDescription a description of the Option's purpose
* @return the OptionBuilder instance
*/
public static OptionBuilder withDescription(String newDescription)
{
OptionBuilder.description = newDescription;
return instance;
}
/**
* The next Option created will have an argument patterns and
* the number of pattern occurances
*
* @param argPattern string representing a pattern regex
* @param limit the number of pattern occurance in the argument
* return the OptionBuilder instance
*/
public static OptionBuilder withArgPattern( String argPattern,
int limit )
{
OptionBuilder.argPattern = argPattern;
OptionBuilder.limit = limit;
return instance;
}
/**
* Create an Option using the current settings and with
* the specified Option <code>char</code>.
*
* @param opt the character representation of the Option
* @return the Option instance
* @throws IllegalArgumentException if <code>opt</code> is not
* a valid character. See Option.
*/
public static Option create(char opt)
throws IllegalArgumentException
{
return create(String.valueOf(opt));
}
/**
* Create an Option using the current settings
*
* @return the Option instance
* @throws IllegalArgumentException if <code>longOpt</code> has
* not been set.
*/
public static Option create()
throws IllegalArgumentException
{
if (longopt == null)
{
throw new IllegalArgumentException("must specify longopt");
}
return create(null);
}
/**
* Create an Option using the current settings and with
* the specified Option <code>char</code>.
*
* @param opt the <code>java.lang.String</code> representation
* of the Option
* @return the Option instance
* @throws IllegalArgumentException if <code>opt</code> is not
* a valid character. See Option.
*/
public static Option create(String opt)
throws IllegalArgumentException
{
// create the option
Option option = new Option(opt, description);
// set the option properties
option.setLongOpt(longopt);
option.setRequired(required);
option.setOptionalArg(optionalArg);
option.setArgs(numberOfArgs);
option.setType(type);
option.setValueSeparator(valuesep);
option.setArgName(argName);
option.setArgPattern(argPattern, limit);
// reset the OptionBuilder properties
OptionBuilder.reset();
// return the Option instance
return option;
}
}