summaryrefslogtreecommitdiff
path: root/3rdParty/jargs-1.0/examples/gnu/OptionParserSubclassTest.java
blob: 551abf1fb902a6ebc2feffe58ff751b6962cc1a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package jargs.examples.gnu;

import jargs.gnu.CmdLineParser;

public class OptionParserSubclassTest {

    private static class MyOptionsParser extends CmdLineParser {

        public static final Option VERBOSE = new
            CmdLineParser.Option.BooleanOption('v',"verbose");

        public static final Option SIZE = new
            CmdLineParser.Option.IntegerOption('s',"size");

        public static final Option NAME = new
            CmdLineParser.Option.StringOption('n',"name");

        public static final Option FRACTION = new
            CmdLineParser.Option.DoubleOption('f',"fraction");

        public MyOptionsParser() {
            super();
            addOption(VERBOSE);
            addOption(SIZE);
            addOption(NAME);
            addOption(FRACTION);
        }
    }

    private static void printUsage() {
        System.err.println("usage: prog [{-v,--verbose}] [{-n,--name} a_name]"+
                           "[{-s,--size} a_number] [{-f,--fraction} a_float]");
    }

    public static void main( String[] args ) {
        MyOptionsParser myOptions = new MyOptionsParser();

        try {
            myOptions.parse(args);
        }
        catch ( CmdLineParser.UnknownOptionException e ) {
            System.err.println(e.getMessage());
            printUsage();
            System.exit(2);
        }
        catch ( CmdLineParser.IllegalOptionValueException e ) {
            System.err.println(e.getMessage());
            printUsage();
            System.exit(2);
        }

        CmdLineParser.Option[] allOptions =
            new CmdLineParser.Option[] { MyOptionsParser.VERBOSE,
                                         MyOptionsParser.NAME,
                                         MyOptionsParser.SIZE,
                                         MyOptionsParser.FRACTION };

        for ( int j = 0; j<allOptions.length; ++j ) {
            System.out.println(allOptions[j].longForm() + ": " +
                               myOptions.getOptionValue(allOptions[j]));
        }

        String[] otherArgs = myOptions.getRemainingArgs();
        System.out.println("remaining args: ");
        for ( int i = 0; i<otherArgs.length; ++i ) {
            System.out.println(otherArgs[i]);
        }
        System.exit(0);
    }

}