Mercurial > hg > truffle
comparison graal/com.oracle.max.base/src/com/sun/max/program/option/OptionSet.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children | bc8527f3071c |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.sun.max.program.option; | |
24 | |
25 import java.io.*; | |
26 import java.net.*; | |
27 import java.util.*; | |
28 import java.lang.reflect.Field; | |
29 import java.lang.reflect.Modifier; | |
30 | |
31 import com.sun.max.*; | |
32 import com.sun.max.lang.*; | |
33 import com.sun.max.program.*; | |
34 | |
35 /** | |
36 * The {@code OptionSet} class parses and collects options from the command line and | |
37 * configuration files. | |
38 */ | |
39 public class OptionSet { | |
40 /** | |
41 * The {@code Syntax} enum allows different options to be parsed differently, | |
42 * depending on their usage. | |
43 */ | |
44 public enum Syntax { | |
45 REQUIRES_EQUALS { | |
46 @Override | |
47 public String getUsage(Option option) { | |
48 return "-" + option.getName() + "=" + option.getType().getValueFormat(); | |
49 } | |
50 }, | |
51 EQUALS_OR_BLANK { | |
52 @Override | |
53 public String getUsage(Option option) { | |
54 return "-" + option.getName() + "[=" + option.getType().getValueFormat() + "]"; | |
55 } | |
56 }, | |
57 REQUIRES_BLANK { | |
58 @Override | |
59 public String getUsage(Option option) { | |
60 return "-" + option.getName(); | |
61 } | |
62 }, | |
63 CONSUMES_NEXT { | |
64 @Override | |
65 public String getUsage(Option option) { | |
66 return "-" + option.getName() + " " + option.getType().getValueFormat(); | |
67 } | |
68 }; | |
69 | |
70 public abstract String getUsage(Option option); | |
71 } | |
72 | |
73 protected final Map<String, Option> optionMap; | |
74 protected final Map<String, Syntax> optionSyntax; | |
75 protected final Map<String, String> optionValues; | |
76 protected final boolean allowUnrecognizedOptions; | |
77 | |
78 protected static final String[] NO_ARGUMENTS = {}; | |
79 | |
80 protected String[] arguments = NO_ARGUMENTS; | |
81 | |
82 /** | |
83 * Creates an option set that does not allow unrecognized options to be present when | |
84 * {@linkplain #parseArguments(String[]) parsing command line arguments} or | |
85 * {@linkplain #loadOptions(OptionSet) loading options from another option set}. | |
86 */ | |
87 public OptionSet() { | |
88 this(false); | |
89 } | |
90 | |
91 /** | |
92 * Creates an option set. | |
93 * | |
94 * @param allowUnrecognizedOptions | |
95 * specifies if this option set allows unrecognized options to be present when | |
96 * {@linkplain #parseArguments(String[]) parsing command line arguments} or | |
97 * {@linkplain #loadOptions(OptionSet) loading options from another option set}. | |
98 */ | |
99 public OptionSet(boolean allowUnrecognizedOptions) { | |
100 optionValues = new HashMap<String, String>(); | |
101 // Using a LinkedHashMap to preserve insertion order when iterating over values | |
102 optionMap = new LinkedHashMap<String, Option>(); | |
103 optionSyntax = new HashMap<String, Syntax>(); | |
104 this.allowUnrecognizedOptions = allowUnrecognizedOptions; | |
105 } | |
106 | |
107 /** | |
108 * Converts this option set into a list of command line arguments, to be used, for example, to pass to an external | |
109 * tool. For each option in this set that has been explicitly set this method will prepend an appropriate option | |
110 * string of appropriate syntactic form (e.g. "-name=value") to the array of arguments passed. | |
111 * | |
112 * @return a new array of program arguments that includes these options | |
113 */ | |
114 public String[] asArguments() { | |
115 String[] newArgs = Arrays.copyOf(arguments, arguments.length + optionValues.size()); | |
116 int i = 0; | |
117 for (String name : optionValues.keySet()) { | |
118 final String value = optionValues.get(name); | |
119 final Syntax syntax = optionSyntax.get(name); | |
120 if (syntax == Syntax.REQUIRES_BLANK) { | |
121 newArgs[i++] = "-" + name; | |
122 } else if (syntax == Syntax.CONSUMES_NEXT) { | |
123 newArgs = Arrays.copyOf(newArgs, newArgs.length + 1); | |
124 newArgs[i++] = "-" + name; | |
125 newArgs[i++] = value; | |
126 } else { | |
127 newArgs[i++] = "-" + name + "=" + value; | |
128 } | |
129 } | |
130 return newArgs; | |
131 } | |
132 | |
133 /** | |
134 * Gets an option set derived from this option set that contains all the unrecognized options that have been loaded | |
135 * or parsed into this option set. The returned option set also includes a copy of the | |
136 * {@linkplain #getArguments() non-option arguments} from this option set. | |
137 * @return a new option set encapsulating all the arguments and options | |
138 */ | |
139 public OptionSet getArgumentsAndUnrecognizedOptions() { | |
140 final OptionSet argumentsAndUnrecognizedOptions = new OptionSet(true); | |
141 for (Map.Entry<String, String> entry : optionValues.entrySet()) { | |
142 if (!optionMap.containsKey(entry.getKey())) { | |
143 argumentsAndUnrecognizedOptions.optionValues.put(entry.getKey(), entry.getValue()); | |
144 } | |
145 } | |
146 argumentsAndUnrecognizedOptions.arguments = arguments; | |
147 return argumentsAndUnrecognizedOptions; | |
148 } | |
149 | |
150 /** | |
151 * Handles an Option.Error raised while loading or parsing values into this option set. | |
152 * <p> | |
153 * This default implementation is to print a usage message and the call {@link System#exit(int)}. | |
154 * @param error the error that occurred | |
155 * @param optionName the name of the option being parsed | |
156 */ | |
157 protected void handleErrorDuringParseOrLoad(Option.Error error, String optionName) { | |
158 System.out.println("Error parsing option -" + optionName + ": " + error.getMessage()); | |
159 printHelp(System.out, 78); | |
160 System.exit(1); | |
161 } | |
162 | |
163 /** | |
164 * Parses a list of command line arguments, processing the leading options (i.e. arguments that start with '-') | |
165 * and returning the "leftover" arguments to the caller. The longest tail of {@code arguments} that starts with a non-option argument can be retrieved after parsing with {@link #getArguments()}. | |
166 * | |
167 * @param args | |
168 * the arguments | |
169 * @return this option set | |
170 */ | |
171 public OptionSet parseArguments(String[] args) { | |
172 // parse the options | |
173 int i = 0; | |
174 for (; i < args.length; i++) { | |
175 final String argument = args[i]; | |
176 if (argument.charAt(0) == '-') { | |
177 // is the beginning of a valid option. | |
178 final int index = argument.indexOf('='); | |
179 final String optionName = getOptionName(argument, index); | |
180 String value = getOptionValue(argument, index); | |
181 final Syntax syntax = optionSyntax.get(optionName); | |
182 // check the syntax of this option | |
183 try { | |
184 checkSyntax(optionName, syntax, value); | |
185 if (syntax == Syntax.CONSUMES_NEXT) { | |
186 value = args[++i]; | |
187 } | |
188 setValue(optionName, value); | |
189 } catch (Option.Error error) { | |
190 handleErrorDuringParseOrLoad(error, optionName); | |
191 } | |
192 } else { | |
193 // is not an option, therefore the start of arguments | |
194 break; | |
195 } | |
196 } | |
197 | |
198 final int left = args.length - i; | |
199 arguments = new String[left]; | |
200 System.arraycopy(args, i, arguments, 0, left); | |
201 return this; | |
202 } | |
203 | |
204 /** | |
205 * The {@code getArguments()} method gets the leftover command line options | |
206 * from the last call to {@code parseArguments}. | |
207 * | |
208 * @return the leftover command line options | |
209 */ | |
210 public String[] getArguments() { | |
211 if (arguments.length == 0) { | |
212 return arguments; | |
213 } | |
214 return Arrays.copyOf(arguments, arguments.length); | |
215 } | |
216 | |
217 /** | |
218 * Determines if this option set allows parsing or loading of unrecognized options. | |
219 * @return {@code true} if this option set allows unrecognized options | |
220 */ | |
221 public boolean allowsUnrecognizedOptions() { | |
222 return allowUnrecognizedOptions; | |
223 } | |
224 | |
225 /** | |
226 * The {@code loadSystemProperties()} method loads the value of the valid | |
227 * options from the systems properties with the specified prefix. | |
228 * | |
229 * @param prefix the prefix of each system property, used to disambiguate | |
230 * these options from other system properties. | |
231 * @return this option set | |
232 */ | |
233 public OptionSet loadSystemProperties(String prefix) { | |
234 final Properties systemProperties = System.getProperties(); | |
235 final Properties properties = new Properties(); | |
236 for (String key : systemProperties.stringPropertyNames()) { | |
237 if (key.startsWith(prefix)) { | |
238 properties.setProperty(key.substring(prefix.length()), systemProperties.getProperty(key)); | |
239 } | |
240 } | |
241 return loadProperties(properties, true); | |
242 } | |
243 | |
244 /** | |
245 * The {@code storeSystemProperties()} method stores these option values | |
246 * into the system properties. | |
247 * | |
248 * @param prefix the prefix to append to all option names when inserting them | |
249 * into the systems properties | |
250 */ | |
251 public void storeSystemProperties(String prefix) { | |
252 for (Map.Entry<String, String> entry : optionValues.entrySet()) { | |
253 System.setProperty(prefix + entry.getKey(), entry.getValue()); | |
254 } | |
255 } | |
256 | |
257 /** | |
258 * Loads the specified properties into this set of options. | |
259 * | |
260 * @param p | |
261 * the properties set to load into this set of options | |
262 * @param loadall | |
263 * true if this method should load all properties in the property set into this option set; false if this | |
264 * method should only load properties for options already in this option set | |
265 * @return this option set | |
266 */ | |
267 public OptionSet loadProperties(Properties p, boolean loadall) { | |
268 if (loadall) { | |
269 // if loadall is specified, load all properties in the set | |
270 for (Object object : p.keySet()) { | |
271 final String name = (String) object; | |
272 final String val = p.getProperty(name); | |
273 try { | |
274 setValue(name, val); | |
275 } catch (Option.Error error) { | |
276 handleErrorDuringParseOrLoad(error, name); | |
277 } | |
278 } | |
279 } else { | |
280 // if loadall is not specified, only load options that are in this option set. | |
281 for (Object o : p.keySet()) { | |
282 final String name = (String) o; | |
283 if (optionMap.containsKey(name)) { | |
284 final String val = p.getProperty(name); | |
285 try { | |
286 setValue(name, val); | |
287 } catch (Option.Error error) { | |
288 handleErrorDuringParseOrLoad(error, name); | |
289 } | |
290 } | |
291 } | |
292 } | |
293 return this; | |
294 } | |
295 | |
296 /** | |
297 * The {@code loadFile()} method parses properties from a file and loads them into this set of options. | |
298 * | |
299 * @param fname | |
300 * the filename from while to load the properties | |
301 * @param loadall | |
302 * true if this method should load all properties in the property set into this option set; false if this | |
303 * method should only load properties for options already in this option set | |
304 * @return this option set | |
305 * @throws java.io.IOException | |
306 * if there is a problem opening or reading the file | |
307 * @throws Option.Error | |
308 * if there is a problem parsing an option | |
309 */ | |
310 public OptionSet loadFile(String fname, boolean loadall) throws IOException, Option.Error { | |
311 final Properties defs = new Properties(); | |
312 final FileInputStream stream = new FileInputStream(new File(fname)); | |
313 defs.load(stream); | |
314 stream.close(); | |
315 return loadProperties(defs, loadall); | |
316 } | |
317 | |
318 /** | |
319 * Loads a set of options and {@linkplain #getArguments() arguments} from another option set. | |
320 * | |
321 * @param options the option set from which to load the option values | |
322 * @return this option set | |
323 */ | |
324 public OptionSet loadOptions(OptionSet options) { | |
325 for (Map.Entry<String, String> entry : options.optionValues.entrySet()) { | |
326 try { | |
327 setValue(entry.getKey(), entry.getValue()); | |
328 } catch (Option.Error error) { | |
329 handleErrorDuringParseOrLoad(error, entry.getKey()); | |
330 } | |
331 } | |
332 arguments = options.arguments; | |
333 return this; | |
334 } | |
335 | |
336 protected void checkSyntax(String optname, Syntax syntax, String value) { | |
337 if (syntax == Syntax.REQUIRES_BLANK && value != null) { | |
338 throw new Option.Error("syntax error: \"-" + optname + "\" required"); | |
339 } | |
340 if (syntax == Syntax.REQUIRES_EQUALS && value == null) { | |
341 throw new Option.Error("syntax error: \"-" + optname + "=value\" required"); | |
342 } | |
343 if (syntax == Syntax.CONSUMES_NEXT && value != null) { | |
344 throw new Option.Error("syntax error: \"-" + optname + " value\" required"); | |
345 } | |
346 } | |
347 | |
348 protected String getOptionName(String argument, int equalIndex) { | |
349 if (equalIndex < 0) { // naked option | |
350 return argument.substring(1, argument.length()); | |
351 } | |
352 return argument.substring(1, equalIndex); | |
353 } | |
354 | |
355 protected String getOptionValue(String argument, int equalIndex) { | |
356 if (equalIndex < 0) { // naked option | |
357 return null; | |
358 } | |
359 return argument.substring(equalIndex + 1); | |
360 } | |
361 | |
362 /** | |
363 * Adds the options of an {@link OptionSet} to this set. | |
364 * @param optionSet the set of options to add | |
365 */ | |
366 public void addOptions(OptionSet optionSet) { | |
367 for (Option<?> option : optionSet.getOptions()) { | |
368 final Syntax syntax = optionSet.optionSyntax.get(option.getName()); | |
369 addOption(option, syntax); | |
370 } | |
371 } | |
372 | |
373 /** | |
374 * The {@code addOption()} method adds an option with the {@link Syntax#REQUIRES_EQUALS} syntax to this option set. | |
375 * | |
376 * @param option the new option to add to this set | |
377 * @return the option passed as the argument, after it has been added to this option set | |
378 */ | |
379 public <T> Option<T> addOption(Option<T> option) { | |
380 return addOption(option, Syntax.REQUIRES_EQUALS); | |
381 } | |
382 | |
383 /** | |
384 * The {@code addOption()} method adds an option to this option set. | |
385 * | |
386 * @param option the new option to add to this set | |
387 * @param syntax the syntax of the option, which specifies how to parse the option | |
388 * from command line parameters | |
389 * @return the option passed as the argument, after it has been added to this option set | |
390 */ | |
391 public <T> Option<T> addOption(Option<T> option, Syntax syntax) { | |
392 final String name = option.getName(); | |
393 final Option existingOption = optionMap.put(name, option); | |
394 if (existingOption != null) { | |
395 throw ProgramError.unexpected("Cannot register more than one option under the same name: " + option.getName()); | |
396 } | |
397 optionSyntax.put(name, syntax); | |
398 return option; | |
399 } | |
400 | |
401 /** | |
402 * The {@code setSyntax()} method sets the syntax of a particular option. | |
403 * | |
404 * @param option the option for which to change the syntax | |
405 * @param syntax the new syntax for the instruction | |
406 */ | |
407 public void setSyntax(Option option, Syntax syntax) { | |
408 optionSyntax.put(option.getName(), syntax); | |
409 } | |
410 | |
411 /** | |
412 * The {@code setValue()} method sets the value of the specified option in | |
413 * this option set. If there is no option by the specified name, the name/value | |
414 * pair will simply be remembered. | |
415 * | |
416 * @param name the name of the option | |
417 * @param value the new value of the option as a string | |
418 * @throws Option.Error if {@code name} denotes an unrecognized option and this | |
419 */ | |
420 public void setValue(String name, String value) { | |
421 final String v = value == null ? "" : value; | |
422 final Option opt = optionMap.get(name); | |
423 if (opt != null) { | |
424 opt.setString(v); | |
425 } else { | |
426 if (!allowUnrecognizedOptions) { | |
427 throw new Option.Error("unrecognized option -" + name); | |
428 } | |
429 } | |
430 optionValues.put(name, v); | |
431 } | |
432 | |
433 public void setValuesAgain() { | |
434 for (String name : optionValues.keySet()) { | |
435 final Option opt = optionMap.get(name); | |
436 opt.setString(optionValues.get(name)); | |
437 } | |
438 } | |
439 | |
440 public String getStringValue(String name) { | |
441 return optionValues.get(name); | |
442 } | |
443 | |
444 /** | |
445 * The {@code hasOptionSpecified()} method checks whether an option with the specified | |
446 * name has been assigned to. An option as been "assigned to" if its value has been set | |
447 * by either parsing arguments (the {@code parseArguments() method} or loading properties | |
448 * from a file or the system properties. | |
449 * | |
450 * @param name the name of the option to query | |
451 * @return true if an option with the specified name has been set; false otherwise | |
452 */ | |
453 public boolean hasOptionSpecified(String name) { | |
454 return optionValues.containsKey(name); | |
455 } | |
456 | |
457 /** | |
458 * Retrieves the options from this option | |
459 * set, in the order in which they were added. | |
460 * @return an iterable collection of {@code Option} instances, sorted according to insertion order | |
461 */ | |
462 public Iterable<Option<?>> getOptions() { | |
463 return Utils.cast(optionMap.values()); | |
464 } | |
465 | |
466 /** | |
467 * The {@code getSortedOptions()} method retrieves the options from this option | |
468 * set, sorting all options by their names. | |
469 * @return an iterable collection of {@code Option} instances, sorted according to the name of each option | |
470 */ | |
471 public Iterable<Option<?>> getSortedOptions() { | |
472 final List<Option<?>> list = new LinkedList<Option<?>>(); | |
473 final TreeSet<String> tree = new TreeSet<String>(); | |
474 for (String string : optionMap.keySet()) { | |
475 tree.add(string); | |
476 } | |
477 for (String string : tree) { | |
478 list.add(optionMap.get(string)); | |
479 } | |
480 return list; | |
481 } | |
482 | |
483 /** | |
484 * Prints the help message header. | |
485 * | |
486 * @param stream the output stream to which to write the help text | |
487 */ | |
488 protected void printHelpHeader(PrintStream stream) { | |
489 } | |
490 | |
491 /** | |
492 * The {@code printHelp()} method prints a textual listing of these options and their syntax | |
493 * to the specified output stream. | |
494 * @param stream the output stream to which to write the help text | |
495 * @param width the length of the line to truncate | |
496 */ | |
497 public void printHelp(PrintStream stream, int width) { | |
498 printHelpHeader(stream); | |
499 for (Option<?> option : getSortedOptions()) { | |
500 if (option.type == OptionTypes.BLANK_BOOLEAN_TYPE && option instanceof FieldOption) { | |
501 FieldOption fopt = (FieldOption) option; | |
502 try { | |
503 if (!fopt.nullValue.equals(fopt.field.getBoolean(null))) { | |
504 // Don't show message for "-XX:+<opt>" option if the default is represented by the | |
505 // "-XX:-<opt>" option instead (and vice versa). | |
506 continue; | |
507 } | |
508 } catch (Exception e) { | |
509 } | |
510 } | |
511 | |
512 | |
513 final Option<Object> opt = Utils.cast(option); | |
514 stream.print(" " + getUsage(opt)); | |
515 final Object defaultValue = opt.getDefaultValue(); | |
516 if (defaultValue != null) { | |
517 stream.println(" (default: " + opt.getType().unparseValue(defaultValue) + ")"); | |
518 } else { | |
519 stream.println(); | |
520 } | |
521 final String help = opt.getHelp(); | |
522 if (help.length() > 0) { | |
523 stream.print(Strings.formatParagraphs(help, 8, 0, width)); | |
524 stream.println(); | |
525 } | |
526 } | |
527 } | |
528 | |
529 /** | |
530 * Prints a textual listing of these options and their values | |
531 * to the specified output stream. | |
532 * @param stream the output stream to which to write the text | |
533 * @param indent the number of spaces to indent | |
534 * @param verbose add each option's description when true | |
535 */ | |
536 public void printValues(PrintStream stream, int indent, boolean verbose) { | |
537 final String indentation = Strings.times(' ', indent); | |
538 for (Option<?> option : getSortedOptions()) { | |
539 if (option.type == OptionTypes.BLANK_BOOLEAN_TYPE && option instanceof FieldOption) { | |
540 FieldOption fopt = (FieldOption) option; | |
541 try { | |
542 if (!fopt.nullValue.equals(fopt.field.getBoolean(null))) { | |
543 // Don't show message for "-XX:+<opt>" option if the default is represented by the | |
544 // "-XX:-<opt>" option instead (and vice versa). | |
545 continue; | |
546 } | |
547 } catch (Exception e) { | |
548 } | |
549 } | |
550 | |
551 | |
552 final Option<Object> opt = Utils.cast(option); | |
553 stream.print(indentation + opt.getName() + ": " + opt.getValue()); | |
554 if (verbose) { | |
555 stream.println(opt.isAssigned() ? "" : " (default)"); | |
556 stream.println(" " + opt.getHelp()); | |
557 stream.println(" " + getUsage(opt)); | |
558 } | |
559 stream.println(); | |
560 } | |
561 } | |
562 | |
563 /** | |
564 * This method gets a usage string for a particular option that describes | |
565 * the range of valid string values that it accepts. | |
566 * @param option the option for which to get usage | |
567 * @return a string describing the usage syntax for the specified option | |
568 */ | |
569 public String getUsage(Option option) { | |
570 return optionSyntax.get(option.getName()).getUsage(option); | |
571 } | |
572 | |
573 /** | |
574 * This method adds all public static fields with appropriate types to the | |
575 * option set with the specified prefix. | |
576 * @param javaClass the java class containing the fields | |
577 * @param prefix the prefix to add to the options | |
578 * @param helpMap map from option names to the help message for the option (may be {@code null}) | |
579 */ | |
580 public void addFieldOptions(Class<?> javaClass, String prefix, Map<String, String> helpMap) { | |
581 for (final Field field : javaClass.getDeclaredFields()) { | |
582 int modifiers = field.getModifiers(); | |
583 if (Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers)) { | |
584 field.setAccessible(true); | |
585 final OptionSettings settings = field.getAnnotation(OptionSettings.class); | |
586 String help; | |
587 String name; | |
588 if (settings != null) { | |
589 help = settings.help(); | |
590 name = settings.name().isEmpty() ? field.getName().replace('_', '-') : settings.name(); | |
591 } else { | |
592 name = field.getName().replace('_', '-'); | |
593 help = helpMap != null ? helpMap.get(name) : ""; | |
594 if (help == null) { | |
595 help = ""; | |
596 } | |
597 } | |
598 addFieldOption(prefix, name, null, field, help); | |
599 } | |
600 } | |
601 } | |
602 | |
603 /** | |
604 * Adds a new option whose value is stored in the specified reflection field. | |
605 * @param prefix the name of the option | |
606 * @param name the name of the option | |
607 * @param object the object containing the field (if the field is not static) | |
608 * @param field the field to store the value | |
609 * @param help the help text for the option @return a new option that will modify the field when parsed | |
610 * @return the option created | |
611 */ | |
612 @SuppressWarnings("unchecked") | |
613 public Option<?> addFieldOption(String prefix, String name, Object object, Field field, String help) { | |
614 final Class<?> fieldType = field.getType(); | |
615 final Object defaultValue; | |
616 final String optionName = prefix + ":" + name; | |
617 try { | |
618 defaultValue = field.get(null); | |
619 } catch (IllegalAccessException e) { | |
620 return null; | |
621 } | |
622 if (fieldType == boolean.class) { | |
623 if (prefix.length() > 0) { | |
624 // setup a "-prefix+name" option | |
625 String plusName = prefix + ":+" + name; | |
626 FieldOption<Boolean> plusOption = new FieldOption<Boolean>(plusName, object, field, (Boolean) defaultValue, OptionTypes.BLANK_BOOLEAN_TYPE, help); | |
627 plusOption.nullValue = true; | |
628 addOption(plusOption, Syntax.REQUIRES_BLANK); | |
629 | |
630 // setup a "-prefix-name" option | |
631 String minusName = prefix + ":-" + name; | |
632 FieldOption<Boolean> minusOption = new FieldOption<Boolean>(minusName, object, field, (Boolean) defaultValue, OptionTypes.BLANK_BOOLEAN_TYPE, help); | |
633 minusOption.nullValue = false; | |
634 return addOption(minusOption, Syntax.REQUIRES_BLANK); | |
635 } | |
636 return addOption(new FieldOption<Boolean>(optionName, object, field, (Boolean) defaultValue, OptionTypes.BOOLEAN_TYPE, help)); | |
637 | |
638 } else if (fieldType == int.class) { | |
639 return addOption(new FieldOption<Integer>(optionName, object, field, (Integer) defaultValue, OptionTypes.INT_TYPE, help)); | |
640 } else if (fieldType == float.class) { | |
641 return addOption(new FieldOption<Float>(optionName, object, field, (Float) defaultValue, OptionTypes.FLOAT_TYPE, help)); | |
642 } else if (fieldType == long.class) { | |
643 return addOption(new FieldOption<Long>(optionName, object, field, (Long) defaultValue, OptionTypes.LONG_TYPE, help)); | |
644 } else if (fieldType == double.class) { | |
645 return addOption(new FieldOption<Double>(optionName, object, field, (Double) defaultValue, OptionTypes.DOUBLE_TYPE, help)); | |
646 } else if (fieldType == String.class) { | |
647 return addOption(new FieldOption<String>(optionName, object, field, (String) defaultValue, OptionTypes.STRING_TYPE, help)); | |
648 } else if (fieldType == File.class) { | |
649 return addOption(new FieldOption<File>(optionName, object, field, (File) defaultValue, OptionTypes.FILE_TYPE, help)); | |
650 } else if (fieldType.isEnum()) { | |
651 final Class<? extends Enum> enumClass = Utils.cast(fieldType); | |
652 return addOption(makeEnumFieldOption(optionName, object, field, defaultValue, enumClass, help)); | |
653 } | |
654 return null; | |
655 } | |
656 | |
657 private <T extends Enum<T>> FieldOption<T> makeEnumFieldOption(String name, Object object, Field field, Object defaultValue, Class<T> enumClass, String help) { | |
658 final OptionTypes.EnumType<T> optionType = new OptionTypes.EnumType<T>(enumClass); | |
659 final T defaultV = Utils.<T>cast(defaultValue); | |
660 return new FieldOption<T>(name, object, field, defaultV, optionType, help); | |
661 } | |
662 | |
663 public Option<String> newStringOption(String name, String defaultValue, String help) { | |
664 return addOption(new Option<String>(name, defaultValue, OptionTypes.STRING_TYPE, help)); | |
665 } | |
666 | |
667 public Option<Integer> newIntegerOption(String name, Integer defaultValue, String help) { | |
668 return addOption(new Option<Integer>(name, defaultValue, OptionTypes.INT_TYPE, help)); | |
669 } | |
670 | |
671 public Option<Long> newLongOption(String name, Long defaultValue, String help) { | |
672 return addOption(new Option<Long>(name, defaultValue, OptionTypes.LONG_TYPE, help)); | |
673 } | |
674 | |
675 public Option<Float> newFloatOption(String name, Float defaultValue, String help) { | |
676 return addOption(new Option<Float>(name, defaultValue, OptionTypes.FLOAT_TYPE, help)); | |
677 } | |
678 | |
679 public Option<Double> newDoubleOption(String name, Double defaultValue, String help) { | |
680 return addOption(new Option<Double>(name, defaultValue, OptionTypes.DOUBLE_TYPE, help)); | |
681 } | |
682 | |
683 public Option<List<String>> newStringListOption(String name, String defaultValue, String help) { | |
684 return addOption(new Option<List<String>>(name, defaultValue == null ? null : OptionTypes.COMMA_SEPARATED_STRING_LIST_TYPE.parseValue(defaultValue), OptionTypes.COMMA_SEPARATED_STRING_LIST_TYPE, help)); | |
685 } | |
686 | |
687 public Option<List<String>> newStringListOption(String name, String[] defaultValue, String help) { | |
688 List<String> list = null; | |
689 if (defaultValue != null) { | |
690 list = new ArrayList<String>(defaultValue.length); | |
691 list.addAll(Arrays.asList(defaultValue)); | |
692 } | |
693 return addOption(new Option<List<String>>(name, list, OptionTypes.COMMA_SEPARATED_STRING_LIST_TYPE, help)); | |
694 } | |
695 | |
696 public Option<List<String>> newStringListOption(String name, String defaultValue, char separator, String help) { | |
697 final OptionTypes.StringListType type = new OptionTypes.StringListType(separator); | |
698 return addOption(new Option<List<String>>(name, defaultValue == null ? null : type.parseValue(defaultValue), type, help)); | |
699 } | |
700 | |
701 public <T> Option<List<T>> newListOption(String name, String defaultValue, Option.Type<T> elementOptionType, char separator, String help) { | |
702 final OptionTypes.ListType<T> type = new OptionTypes.ListType<T>(separator, elementOptionType); | |
703 return addOption(new Option<List<T>>(name, defaultValue == null ? null : type.parseValue(defaultValue), type, help)); | |
704 } | |
705 | |
706 public Option<File> newFileOption(String name, String defaultValue, String help) { | |
707 return newFileOption(name, OptionTypes.FILE_TYPE.parseValue(defaultValue), help); | |
708 } | |
709 | |
710 public Option<File> newFileOption(String name, File defaultValue, String help) { | |
711 return addOption(new Option<File>(name, defaultValue, OptionTypes.FILE_TYPE, help)); | |
712 } | |
713 | |
714 public Option<URL> newURLOption(String name, URL defaultValue, String help) { | |
715 return addOption(new Option<URL>(name, defaultValue, OptionTypes.URL_TYPE, help)); | |
716 } | |
717 | |
718 public <T> Option<T> newInstanceOption(String name, Class<T> klass, T defaultValue, String help) { | |
719 return addOption(new Option<T>(name, defaultValue, OptionTypes.createInstanceOptionType(klass), help)); | |
720 } | |
721 | |
722 public <T> Option<List<T>> newListInstanceOption(String name, String defaultValue, Class<T> klass, char separator, String help) { | |
723 final OptionTypes.ListType<T> type = OptionTypes.createInstanceListOptionType(klass, separator); | |
724 return addOption(new Option<List<T>>(name, (defaultValue == null) ? null : type.parseValue(defaultValue), type, help)); | |
725 } | |
726 | |
727 public Option<Boolean> newBooleanOption(String name, Boolean defaultValue, String help) { | |
728 if (defaultValue != null && !defaultValue) { | |
729 return addOption(new Option<Boolean>(name, defaultValue, OptionTypes.BOOLEAN_TYPE, help), Syntax.EQUALS_OR_BLANK); | |
730 } | |
731 return addOption(new Option<Boolean>(name, defaultValue, OptionTypes.BOOLEAN_TYPE, help)); | |
732 } | |
733 | |
734 public <T> Option<T> newOption(String name, String defaultValue, Option.Type<T> type, String help) { | |
735 return newOption(name, type.parseValue(defaultValue), type, Syntax.REQUIRES_EQUALS, help); | |
736 } | |
737 | |
738 public <T> Option<T> newOption(String name, T defaultValue, Option.Type<T> type, Syntax syntax, String help) { | |
739 return addOption(new Option<T>(name, defaultValue, type, help), syntax); | |
740 } | |
741 | |
742 public <E extends Enum<E>> Option<E> newEnumOption(String name, E defaultValue, Class<E> enumClass, String help) { | |
743 return addOption(new Option<E>(name, defaultValue, new OptionTypes.EnumType<E>(enumClass), help)); | |
744 } | |
745 | |
746 public <E extends Enum<E>> Option<List<E>> newEnumListOption(String name, Iterable<E> defaultValue, Class<E> enumClass, String help) { | |
747 final List<E> list; | |
748 if (defaultValue == null) { | |
749 list = null; | |
750 } else if (defaultValue instanceof List) { | |
751 list = Utils.cast(defaultValue); | |
752 } else if (defaultValue instanceof Collection) { | |
753 final Collection<E> collection = Utils.cast(defaultValue); | |
754 list = new ArrayList<E>(collection); | |
755 } else { | |
756 list = new ArrayList<E>(); | |
757 for (E value : defaultValue) { | |
758 list.add(value); | |
759 } | |
760 } | |
761 final Option<List<E>> option = new Option<List<E>>(name, list, new OptionTypes.EnumListType<E>(enumClass, ','), help); | |
762 return addOption(option); | |
763 } | |
764 | |
765 public Option<File> newConfigOption(String name, File defaultFile, String help) { | |
766 return addOption(new Option<File>(name, defaultFile, new OptionTypes.ConfigFile(this), help)); | |
767 } | |
768 } |