KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > util > OptionParser


1 package com.thaiopensource.util;
2
3 public class OptionParser {
4   private final String JavaDoc optionSpec;
5   private char optionChar = 0;
6   private String JavaDoc optionArg = null;
7   private int argIndex = 0;
8   private int currentOptionIndex = 0;
9   private final String JavaDoc[] args;
10
11   private static final char OPTION_CHAR = '-';
12
13   public static class MissingArgumentException extends Exception JavaDoc { }
14
15   public static class InvalidOptionException extends Exception JavaDoc { }
16
17   public OptionParser(String JavaDoc optionSpec, String JavaDoc[] args) {
18     this.optionSpec = optionSpec;
19     this.args = new String JavaDoc[args.length];
20     System.arraycopy(args, 0, this.args, 0, args.length);
21   }
22
23   public char getOptionChar() {
24     return optionChar;
25   }
26
27   public String JavaDoc getOptionCharString() {
28     return new String JavaDoc(new char[]{optionChar});
29   }
30
31   public String JavaDoc getOptionArg() {
32     return optionArg;
33   }
34
35   public boolean moveToNextOption()
36     throws InvalidOptionException, MissingArgumentException {
37     if (currentOptionIndex > 0
38     && currentOptionIndex == args[argIndex].length()) {
39       currentOptionIndex = 0;
40       argIndex++;
41     }
42     if (currentOptionIndex == 0) {
43       if (argIndex >= args.length)
44     return false;
45       String JavaDoc arg = args[argIndex];
46       if (arg.length() < 2 || arg.charAt(0) != OPTION_CHAR)
47     return false;
48       if (arg.length() == 2 && arg.charAt(1) == OPTION_CHAR) {
49     argIndex++;
50     return false;
51       }
52       currentOptionIndex = 1;
53     }
54     optionChar = args[argIndex].charAt(currentOptionIndex++);
55     optionArg = null;
56     int i = optionSpec.indexOf(optionChar);
57     if (i < 0 || (optionChar == ':' && i > 0))
58       throw new InvalidOptionException();
59     if (i + 1 < optionSpec.length() && optionSpec.charAt(i + 1) == ':') {
60       if (currentOptionIndex < args[argIndex].length()) {
61     optionArg = args[argIndex].substring(currentOptionIndex);
62     currentOptionIndex = 0;
63     argIndex++;
64       }
65       else if (argIndex + 1 < args.length) {
66     optionArg = args[++argIndex];
67     ++argIndex;
68     currentOptionIndex = 0;
69       }
70       else
71     throw new MissingArgumentException();
72     }
73     return true;
74   }
75
76   public String JavaDoc[] getRemainingArgs() {
77     String JavaDoc[] tem = new String JavaDoc[args.length - argIndex];
78     System.arraycopy(args, argIndex, tem, 0, tem.length);
79     return tem;
80   }
81
82   public static void main(String JavaDoc[] args) {
83     String JavaDoc optSpec = args[0];
84     String JavaDoc[] tem = new String JavaDoc[args.length - 1];
85     System.arraycopy(args, 1, tem, 0, tem.length);
86     args = tem;
87     OptionParser opts = new OptionParser(optSpec, args);
88     try {
89       while (opts.moveToNextOption()) {
90     System.err.print("option " + opts.getOptionChar());
91     String JavaDoc arg = opts.getOptionArg();
92     if (arg == null)
93       System.err.println(" (no argument)");
94     else
95       System.err.println(" arg=" + arg);
96       }
97       args = opts.getRemainingArgs();
98       for (int i = 0; i < args.length; i++)
99     System.err.println("arg=" + args[i]);
100     }
101     catch (OptionParser.MissingArgumentException e) {
102       System.err.println("missing argument for option " + opts.getOptionChar());
103     }
104     catch (OptionParser.InvalidOptionException e) {
105       System.err.println("invalid option " + opts.getOptionChar());
106     }
107   }
108 }
109
Popular Tags