1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.cli;
19
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Enumeration;
23 import java.util.List;
24 import java.util.ListIterator;
25 import java.util.Properties;
26
27
28
29
30
31
32 @Deprecated
33 public abstract class Parser implements CommandLineParser {
34
35 protected CommandLine cmd;
36
37
38 private Options options;
39
40
41 private List requiredOptions;
42
43
44
45
46
47
48 protected void checkRequiredOptions() throws MissingOptionException {
49
50 if (!getRequiredOptions().isEmpty()) {
51 throw new MissingOptionException(getRequiredOptions());
52 }
53 }
54
55
56
57
58
59
60
61
62
63
64 protected abstract String[] flatten(Options opts, String[] arguments, boolean stopAtNonOption) throws ParseException;
65
66
67
68
69
70
71 protected Options getOptions() {
72 return options;
73 }
74
75
76
77
78
79
80 protected List getRequiredOptions() {
81 return requiredOptions;
82 }
83
84
85
86
87
88
89
90
91
92 @Override
93 public CommandLine parse(final Options options, final String[] arguments) throws ParseException {
94 return parse(options, arguments, null, false);
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108 @Override
109 public CommandLine parse(final Options options, final String[] arguments, final boolean stopAtNonOption) throws ParseException {
110 return parse(options, arguments, null, stopAtNonOption);
111 }
112
113
114
115
116
117
118
119
120
121
122
123 public CommandLine parse(final Options options, final String[] arguments, final Properties properties) throws ParseException {
124 return parse(options, arguments, properties, false);
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public CommandLine parse(final Options options, final String[] arguments, final Properties properties, final boolean stopAtNonOption)
141 throws ParseException {
142
143 for (final Option opt : options.helpOptions()) {
144 opt.clearValues();
145 }
146
147 for (final OptionGroup group : options.getOptionGroups()) {
148 group.setSelected(null);
149 }
150
151 setOptions(options);
152 cmd = CommandLine.builder().build();
153 boolean eatTheRest = false;
154 final List<String> tokenList = Arrays.asList(flatten(getOptions(), arguments == null ? new String[0] : arguments, stopAtNonOption));
155 final ListIterator<String> iterator = tokenList.listIterator();
156
157 while (iterator.hasNext()) {
158 final String token = iterator.next();
159 if (token != null) {
160
161 if ("--".equals(token)) {
162 eatTheRest = true;
163 } else if ("-".equals(token)) {
164
165 if (stopAtNonOption) {
166 eatTheRest = true;
167 } else {
168 cmd.addArg(token);
169 }
170 } else if (token.startsWith("-")) {
171
172 if (stopAtNonOption && !getOptions().hasOption(token)) {
173 eatTheRest = true;
174 cmd.addArg(token);
175 } else {
176 processOption(token, iterator);
177 }
178 } else {
179
180 cmd.addArg(token);
181 if (stopAtNonOption) {
182 eatTheRest = true;
183 }
184 }
185
186 if (eatTheRest) {
187 while (iterator.hasNext()) {
188 final String str = iterator.next();
189
190 if (!"--".equals(str)) {
191 cmd.addArg(str);
192 }
193 }
194 }
195 }
196 }
197 processProperties(properties);
198 checkRequiredOptions();
199 return cmd;
200 }
201
202
203
204
205
206
207
208
209
210 public void processArgs(final Option opt, final ListIterator<String> iter) throws ParseException {
211
212 while (iter.hasNext()) {
213 final String str = iter.next();
214
215 if (getOptions().hasOption(str) && str.startsWith("-")) {
216 iter.previous();
217 break;
218 }
219
220 try {
221 opt.processValue(Util.stripLeadingAndTrailingQuotes(str));
222 } catch (final RuntimeException exp) {
223 iter.previous();
224 break;
225 }
226 }
227 if (opt.getValues() == null && !opt.hasOptionalArg()) {
228 throw new MissingArgumentException(opt);
229 }
230 }
231
232
233
234
235
236
237
238
239
240 protected void processOption(final String arg, final ListIterator<String> iter) throws ParseException {
241 final boolean hasOption = getOptions().hasOption(arg);
242
243 if (!hasOption) {
244 throw new UnrecognizedOptionException("Unrecognized option: " + arg, arg);
245 }
246
247 final Option opt = (Option) getOptions().getOption(arg).clone();
248
249 updateRequiredOptions(opt);
250
251 if (opt.hasArg()) {
252 processArgs(opt, iter);
253 }
254
255 cmd.addOption(opt);
256 }
257
258
259
260
261
262
263
264 protected void processProperties(final Properties properties) throws ParseException {
265 if (properties == null) {
266 return;
267 }
268 for (final Enumeration<?> e = properties.propertyNames(); e.hasMoreElements();) {
269 final String option = e.nextElement().toString();
270 final Option opt = options.getOption(option);
271 if (opt == null) {
272 throw new UnrecognizedOptionException("Default option wasn't defined", option);
273 }
274
275 final OptionGroup group = options.getOptionGroup(opt);
276 final boolean selected = group != null && group.isSelected();
277 if (!cmd.hasOption(option) && !selected) {
278
279 final String value = properties.getProperty(option);
280 if (opt.hasArg()) {
281 if (Util.isEmpty(opt.getValues())) {
282 try {
283 opt.processValue(value);
284 } catch (final RuntimeException exp) {
285
286 }
287 }
288 } else if (!("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {
289
290
291 continue;
292 }
293 cmd.addOption(opt);
294 updateRequiredOptions(opt);
295 }
296 }
297 }
298
299
300
301
302
303
304 protected void setOptions(final Options options) {
305 this.options = options;
306 this.requiredOptions = new ArrayList<>(options.getRequiredOptions());
307 }
308
309
310
311
312
313
314 private void updateRequiredOptions(final Option opt) throws ParseException {
315
316
317 if (opt.isRequired()) {
318 getRequiredOptions().remove(opt.getKey());
319 }
320
321
322 if (getOptions().getOptionGroup(opt) != null) {
323 final OptionGroup group = getOptions().getOptionGroup(opt);
324 if (group.isRequired()) {
325 getRequiredOptions().remove(group);
326 }
327 group.setSelected(opt);
328 }
329 }
330
331 }