001: package jargs.examples.gnu;
002:
003: import java.util.Enumeration;
004: import java.util.Vector;
005:
006: import jargs.gnu.CmdLineParser;
007:
008: public class OptionTest {
009:
010: private static void printUsage() {
011: System.err
012: .println("Usage: OptionTest [-d,--debug] [{-v,--verbose}] [{--alt}] [{--name} a_name]\n"
013: + " [{-s,--size} a_number] [{-f,--fraction} a_float] [a_nother]");
014: }
015:
016: public static void main(String[] args) {
017:
018: // First, you must create a CmdLineParser, and add to it the
019: // appropriate Options.
020:
021: // To start with, we add the Options -d, -v, -s, and -f, with aliases
022: // --debug, --verbose, --size, and --fraction respectively.
023:
024: // The -d and -v options have no associated value -- they are either
025: // present, or they are not. The -s and -f options take integer and
026: // double-precision floating-point values respectively.
027:
028: CmdLineParser parser = new CmdLineParser();
029: CmdLineParser.Option debug = parser.addBooleanOption('d',
030: "debug");
031: CmdLineParser.Option verbose = parser.addBooleanOption('v',
032: "verbose");
033: CmdLineParser.Option size = parser
034: .addIntegerOption('s', "size");
035: CmdLineParser.Option fraction = parser.addDoubleOption('f',
036: "fraction");
037:
038: // Options may have just a long form with no corresponding short form.
039: // Here, we add --alt and --name options.
040:
041: CmdLineParser.Option alt = parser.addBooleanOption("alt");
042: CmdLineParser.Option name = parser.addStringOption("name");
043:
044: // Next, you must parse the user-provided command line arguments, and
045: // catch any errors therein.
046:
047: // Options may appear on the command line in any order, and may even
048: // appear after some or all of the non-option arguments.
049:
050: // If the user needs to specify non-option arguments that start with a
051: // minus, then they may indicate the end of the parsable options with
052: // -- , like this:
053:
054: // prog -f 20 -- -10 -fred
055:
056: // The -f 20 will be parsed as the fraction option, with the value 20.
057: // The -10 and -fred arguments will be regarded as non-option
058: // arguments, and passed through getRemainingArgs as unparsed Strings.
059:
060: // Short boolean options may be specified separately (-d -v) or
061: // together (-dv).
062:
063: // Options with values may be given on the command line as -f 1.0 or
064: // --fraction=1.0.
065:
066: try {
067: parser.parse(args);
068: } catch (CmdLineParser.OptionException e) {
069: System.err.println(e.getMessage());
070: printUsage();
071: System.exit(2);
072: }
073:
074: // For options that may be specified only zero or one time, the value
075: // of that option may be extracted as shown below. If the options
076: // were not specified, the corresponding values will be null.
077:
078: Boolean debugValue = (Boolean) parser.getOptionValue(debug);
079: String nameValue = (String) parser.getOptionValue(name);
080:
081: // Alternatively, you may specify a default value. This will be
082: // returned (instead of null) when the command line argument is
083: // missing.
084:
085: Boolean altValue = (Boolean) parser.getOptionValue(alt,
086: Boolean.FALSE);
087: Integer sizeValue = (Integer) parser.getOptionValue(size,
088: new Integer(42));
089:
090: // If your application requires it, options may be specified more than
091: // once. In this case, you may get all the values specified by the
092: // user, as a Vector:
093:
094: Vector fractionValues = parser.getOptionValues(fraction);
095:
096: // Alternatively, you may make the loop explicit:
097:
098: int verbosity = 0;
099: while (true) {
100: Boolean verboseValue = (Boolean) parser
101: .getOptionValue(verbose);
102:
103: if (verboseValue == null) {
104: break;
105: } else {
106: verbosity++;
107: }
108: }
109:
110: // The remaining command-line arguments -- those that do not start
111: // with a minus sign -- can be captured like this:
112:
113: String[] otherArgs = parser.getRemainingArgs();
114:
115: // For testing purposes, we just print out the option values and
116: // remaining command-line arguments. In a real program, of course,
117: // one would pass them to a function that does something more useful.
118:
119: System.out.println("debug: " + debugValue);
120: System.out.println("alt: " + altValue);
121: System.out.println("size: " + sizeValue);
122: System.out.println("name: " + nameValue);
123:
124: System.out.println("verbosity: " + verbosity);
125:
126: Enumeration e = fractionValues.elements();
127: while (e.hasMoreElements()) {
128: System.out.println("fraction: " + (Double) e.nextElement());
129: }
130:
131: System.out.println("remaining args: ");
132: for (int i = 0; i < otherArgs.length; ++i) {
133: System.out.println(otherArgs[i]);
134: }
135:
136: System.exit(0);
137: }
138: }
|