01: package jargs.examples.gnu;
02:
03: import jargs.gnu.CmdLineParser;
04:
05: public class OptionParserSubclassTest {
06:
07: private static class MyOptionsParser extends CmdLineParser {
08:
09: public static final Option VERBOSE = new CmdLineParser.Option.BooleanOption(
10: 'v', "verbose");
11:
12: public static final Option SIZE = new CmdLineParser.Option.IntegerOption(
13: 's', "size");
14:
15: public static final Option NAME = new CmdLineParser.Option.StringOption(
16: 'n', "name");
17:
18: public static final Option FRACTION = new CmdLineParser.Option.DoubleOption(
19: 'f', "fraction");
20:
21: public MyOptionsParser() {
22: super ();
23: addOption(VERBOSE);
24: addOption(SIZE);
25: addOption(NAME);
26: addOption(FRACTION);
27: }
28: }
29:
30: private static void printUsage() {
31: System.err
32: .println("usage: prog [{-v,--verbose}] [{-n,--name} a_name]"
33: + "[{-s,--size} a_number] [{-f,--fraction} a_float]");
34: }
35:
36: public static void main(String[] args) {
37: MyOptionsParser myOptions = new MyOptionsParser();
38:
39: try {
40: myOptions.parse(args);
41: } catch (CmdLineParser.UnknownOptionException e) {
42: System.err.println(e.getMessage());
43: printUsage();
44: System.exit(2);
45: } catch (CmdLineParser.IllegalOptionValueException e) {
46: System.err.println(e.getMessage());
47: printUsage();
48: System.exit(2);
49: }
50:
51: CmdLineParser.Option[] allOptions = new CmdLineParser.Option[] {
52: MyOptionsParser.VERBOSE, MyOptionsParser.NAME,
53: MyOptionsParser.SIZE, MyOptionsParser.FRACTION };
54:
55: for (int j = 0; j < allOptions.length; ++j) {
56: System.out.println(allOptions[j].longForm() + ": "
57: + myOptions.getOptionValue(allOptions[j]));
58: }
59:
60: String[] otherArgs = myOptions.getRemainingArgs();
61: System.out.println("remaining args: ");
62: for (int i = 0; i < otherArgs.length; ++i) {
63: System.out.println(otherArgs[i]);
64: }
65: System.exit(0);
66: }
67:
68: }
|