1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2.builder;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.cli2.Argument;
23 import org.apache.commons.cli2.option.ArgumentImpl;
24 import org.apache.commons.cli2.resource.ResourceConstants;
25 import org.apache.commons.cli2.resource.ResourceHelper;
26 import org.apache.commons.cli2.validation.Validator;
27
28
29
30
31 public class ArgumentBuilder {
32
33
34 private final static ResourceHelper resources = ResourceHelper.getResourceHelper();
35
36
37 private String name;
38
39
40 private String description;
41
42
43 private int minimum;
44
45
46 private int maximum;
47
48
49 private char initialSeparator;
50
51
52 private char subsequentSeparator;
53
54
55 private Validator validator;
56
57
58 private String consumeRemaining;
59
60
61 private List defaultValues;
62
63
64 private int id;
65
66
67
68
69 public ArgumentBuilder() {
70 reset();
71 }
72
73
74
75
76
77
78
79
80 public final Argument create() {
81 final Argument argument =
82 new ArgumentImpl(
83 name,
84 description,
85 minimum,
86 maximum,
87 initialSeparator,
88 subsequentSeparator,
89 validator,
90 consumeRemaining,
91 defaultValues,
92 id);
93
94 reset();
95
96 return argument;
97 }
98
99
100
101
102
103
104 public final ArgumentBuilder reset() {
105 name = "arg";
106 description = null;
107 minimum = 0;
108 maximum = Integer.MAX_VALUE;
109 initialSeparator = ArgumentImpl.DEFAULT_INITIAL_SEPARATOR;
110 subsequentSeparator = ArgumentImpl.DEFAULT_SUBSEQUENT_SEPARATOR;
111 validator = null;
112 consumeRemaining = "--";
113 defaultValues = null;
114 id = 0;
115 return this;
116 }
117
118
119
120
121
122
123
124
125
126
127 public final ArgumentBuilder withName(final String newName) {
128 if (newName == null) {
129 throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_NAME));
130 }
131 if ("".equals(newName)) {
132 throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_NAME));
133 }
134 this.name = newName;
135 return this;
136 }
137
138
139
140
141
142
143
144
145
146 public final ArgumentBuilder withDescription(final String newDescription) {
147 this.description = newDescription;
148 return this;
149 }
150
151
152
153
154
155
156
157 public final ArgumentBuilder withMinimum(final int newMinimum) {
158 if (newMinimum < 0) {
159 throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MINIMUM));
160 }
161 this.minimum = newMinimum;
162 return this;
163 }
164
165
166
167
168
169
170
171 public final ArgumentBuilder withMaximum(final int newMaximum) {
172 if (newMaximum < 0) {
173 throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MAXIMUM));
174 }
175 this.maximum = newMaximum;
176 return this;
177 }
178
179
180
181
182
183
184
185
186
187
188 public final ArgumentBuilder withInitialSeparator(
189 final char newInitialSeparator) {
190
191 this.initialSeparator = newInitialSeparator;
192 return this;
193 }
194
195
196
197
198
199
200
201
202
203
204 public final ArgumentBuilder withSubsequentSeparator(
205 final char newSubsequentSeparator) {
206
207 this.subsequentSeparator = newSubsequentSeparator;
208 return this;
209 }
210
211
212
213
214
215
216
217
218 public final ArgumentBuilder withValidator(final Validator newValidator) {
219 if (newValidator == null) {
220 throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_VALIDATOR));
221 }
222 this.validator = newValidator;
223 return this;
224 }
225
226
227
228
229
230
231
232
233
234 public final ArgumentBuilder withConsumeRemaining(final String newConsumeRemaining) {
235 if (newConsumeRemaining == null) {
236 throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_CONSUME_REMAINING));
237 }
238 if ( "".equals(newConsumeRemaining)) {
239 throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING));
240 }
241 this.consumeRemaining = newConsumeRemaining;
242 return this;
243 }
244
245
246
247
248
249
250
251 public final ArgumentBuilder withDefault(final Object defaultValue) {
252 if (defaultValue == null) {
253 throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULT));
254 }
255
256 if (this.defaultValues == null) {
257 this.defaultValues = new ArrayList(1);
258 }
259 this.defaultValues.add(defaultValue);
260 return this;
261 }
262
263
264
265
266
267
268
269 public final ArgumentBuilder withDefaults(final List newDefaultValues) {
270 if (newDefaultValues == null) {
271 throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULTS));
272 }
273 this.defaultValues = newDefaultValues;
274 return this;
275 }
276
277
278
279
280
281
282
283 public final ArgumentBuilder withId(final int newId) {
284 this.id = newId;
285 return this;
286 }
287 }