001package jargs.examples.gnu;
002
003import jargs.gnu.CmdLineParser;
004
005public class OptionParserSubclassTest {
006
007    private static class MyOptionsParser extends CmdLineParser {
008
009        public static final Option VERBOSE = new
010            CmdLineParser.Option.BooleanOption('v',"verbose");
011
012        public static final Option SIZE = new
013            CmdLineParser.Option.IntegerOption('s',"size");
014
015        public static final Option NAME = new
016            CmdLineParser.Option.StringOption('n',"name");
017
018        public static final Option FRACTION = new
019            CmdLineParser.Option.DoubleOption('f',"fraction");
020
021        public MyOptionsParser() {
022            super();
023            addOption(VERBOSE);
024            addOption(SIZE);
025            addOption(NAME);
026            addOption(FRACTION);
027        }
028    }
029
030    private static void printUsage() {
031        System.err.println("usage: prog [{-v,--verbose}] [{-n,--name} a_name]"+
032                           "[{-s,--size} a_number] [{-f,--fraction} a_float]");
033    }
034
035    public static void main( String[] args ) {
036        MyOptionsParser myOptions = new MyOptionsParser();
037
038        try {
039            myOptions.parse(args);
040        }
041        catch ( CmdLineParser.UnknownOptionException e ) {
042            System.err.println(e.getMessage());
043            printUsage();
044            System.exit(2);
045        }
046        catch ( CmdLineParser.IllegalOptionValueException e ) {
047            System.err.println(e.getMessage());
048            printUsage();
049            System.exit(2);
050        }
051
052        CmdLineParser.Option[] allOptions =
053            new CmdLineParser.Option[] { MyOptionsParser.VERBOSE,
054                                         MyOptionsParser.NAME,
055                                         MyOptionsParser.SIZE,
056                                         MyOptionsParser.FRACTION };
057
058        for ( int j = 0; j<allOptions.length; ++j ) {
059            System.out.println(allOptions[j].longForm() + ": " +
060                               myOptions.getOptionValue(allOptions[j]));
061        }
062
063        String[] otherArgs = myOptions.getRemainingArgs();
064        System.out.println("remaining args: ");
065        for ( int i = 0; i<otherArgs.length; ++i ) {
066            System.out.println(otherArgs[i]);
067        }
068        System.exit(0);
069    }
070
071}