KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > pth > Options


1 package polyglot.pth;
2
3 import java.io.PrintStream JavaDoc;
4 import java.util.LinkedList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7
8 /**
9  *
10  */

11 public class Options {
12     private final static int USAGE_FLAG_WIDTH = 21;
13     private final static int USAGE_SCREEN_WIDTH = 75;
14     
15     protected List JavaDoc inputFilenames = new LinkedList JavaDoc();
16     
17     // verbosity level, from 0 to 9.
18
protected int verbosity = 5;
19     
20     // classpath for the compiler.
21
protected String JavaDoc classpath = null;
22     
23     // Extra command line args for the compiler
24
protected String JavaDoc extraArgs = null;
25     
26     // filter for tests
27
protected String JavaDoc testFilter = null;
28     
29     // show latest test results only
30
protected boolean showResultsOnly = false;
31     
32     // test only the ones that failed last time
33
protected boolean testPreviouslyFailedOnly = false;
34
35     // test only the ones that failed last time
36
protected boolean deleteOutputFiles = true;
37     
38     // array of the possible command line options.
39
// the order in the array is the order that they will be applied in.
40
protected CommandLineOption[] commandLineOpts = {
41         new CommandLineOption(new String JavaDoc[] {"f", "filter"}, "regexp",
42                            "only execute the tests for which the regular expression regexp occurs somewhere in the test's name.") {
43                protected int invoke(int index, String JavaDoc[] args) {
44                    testFilter = ".*" + getStringArg(++index, args) + ".*";
45                    return index + 1;
46                }
47          },
48         new CommandLineOption(new String JavaDoc[] {"p", "preserve"},
49                            "preserve output files between tests; default is to delete") {
50                protected int invoke(int index, String JavaDoc[] args) {
51                    deleteOutputFiles = false;
52                    return index + 1;
53                }
54          },
55         new CommandLineOption(new String JavaDoc[] {"q", "question"},
56                            "show the latest results for the scripts; don't run any tests") {
57                protected int invoke(int index, String JavaDoc[] args) {
58                    showResultsOnly = true;
59                    return index + 1;
60                }
61          },
62         new CommandLineOption("s",
63                            "only execute the tests which did not succeed last time they were executed.") {
64                protected int invoke(int index, String JavaDoc[] args) {
65                    testPreviouslyFailedOnly = true;
66                    return index + 1;
67                }
68          },
69         new CommandLineOption(new String JavaDoc[] {"v"}, "n",
70                            "set verbosity level to n. n should be between 0 (quiet) and 9 (most verbose).") {
71                protected int invoke(int index, String JavaDoc[] args) {
72                    verbosity = getIntArg(++index, args);
73                    if (verbosity < 0 || verbosity > 9) {
74                        throw new IllegalArgumentException JavaDoc("Verbosity must be " +
75                              "between 0 and 9 inclusive.");
76                    }
77                    return index + 1;
78                }
79          },
80         new CommandLineOption(new String JavaDoc[] {"cp", "classpath"}, "path",
81                            "set the class path for the Polyglot compiler.") {
82                protected int invoke(int index, String JavaDoc[] args) {
83                    classpath = getStringArg(++index, args);
84                    return index + 1;
85                }
86          },
87         new CommandLineOption(new String JavaDoc[] {"args"}, "extraArgs",
88                            "provide additional command line arguments to the Polyglot compiler.") {
89                protected int invoke(int index, String JavaDoc[] args) {
90                    extraArgs = getStringArg(++index, args);
91                    return index + 1;
92                }
93          },
94         new CommandLineOption(new String JavaDoc[] {"h", "help", "?"},
95                            "Display this message.") {
96                protected int invoke(int index, String JavaDoc[] args) {
97                    usage(System.out);
98                    System.exit(0);
99                    return index + 1;
100                }
101          },
102         new CommandLineOption(new String JavaDoc[] {},
103                            "Files") {
104                    protected int invoke(int index, String JavaDoc[] args) { return index; }
105                    protected int parse(int index, String JavaDoc[] args) {
106                        if (!args[index].startsWith("-")) inputFilenames.add(args[index++]);
107                        return index;
108                    }
109          },
110     };
111     
112     protected void usage(PrintStream JavaDoc out) {
113         out.println("Polyglot Test Harness");
114         out.println("Usage: pth [options] scriptFile ...");
115         out.println("where options include: ");
116         
117         for (int i = 0; i < commandLineOpts.length; i++) {
118             usageForSwitch(out, commandLineOpts[i]);
119         }
120     }
121
122     protected void usageForSwitch(PrintStream JavaDoc out, CommandLineOption arg) {
123         if (arg.switches.length == 0) return;
124               
125         out.print(" ");
126         // cur is where the cursor is on the screen.
127
int cur = 2;
128
129         for (int i = 0; i < arg.switches.length; i++) {
130             String JavaDoc flag = arg.switches[i];
131             out.print("-" + flag);
132             cur += 1 + flag.length();
133             if (arg.additionalArg != null) {
134                 out.print(" " + arg.additionalArg);
135                 cur += 1 + arg.additionalArg.length();
136             }
137             if (i < arg.switches.length-1) {
138                 out.print(", ");
139                 cur += 2;
140             }
141         }
142         
143         // print space to get up to indentation level
144
if (cur < USAGE_FLAG_WIDTH) {
145             printSpaces(out, USAGE_FLAG_WIDTH - cur);
146         }
147         else {
148             // the flag is long. Get a new line before printing the
149
// description.
150
out.println();
151             printSpaces(out, USAGE_FLAG_WIDTH);
152         }
153         cur = USAGE_FLAG_WIDTH;
154         
155         // break up the description.
156
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(arg.explanation);
157         while (st.hasMoreTokens()) {
158             String JavaDoc s = st.nextToken();
159             if (cur + s.length() > USAGE_SCREEN_WIDTH) {
160                 out.println();
161                 printSpaces(out, USAGE_FLAG_WIDTH);
162                 cur = USAGE_FLAG_WIDTH;
163             }
164             out.print(s);
165             cur += s.length();
166             if (st.hasMoreTokens()) {
167                 if (cur + 1 > USAGE_SCREEN_WIDTH) {
168                     out.println();
169                     printSpaces(out, USAGE_FLAG_WIDTH);
170                     cur = USAGE_FLAG_WIDTH;
171                 }
172                 else {
173                     out.print(" ");
174                     cur++;
175                 }
176             }
177         }
178         out.println();
179     }
180     
181     protected static void printSpaces(PrintStream JavaDoc out, int n) {
182         while (n-- > 0) {
183             out.print(' ');
184         }
185     }
186
187     protected void parseCommandLine(String JavaDoc[] args) {
188         int ind = 0;
189         if (args == null || args.length == 0) {
190             args = new String JavaDoc[] {"-h"};
191         }
192         while (ind < args.length) {
193             int newInd = ind;
194             for (int i = 0; i < commandLineOpts.length && newInd == ind; i++) {
195                 CommandLineOption a = commandLineOpts[i];
196                 newInd = a.parse(ind, args);
197             }
198             
199             if (newInd == ind) {
200                 throw new IllegalArgumentException JavaDoc("Unknown switch: " +
201                      args[ind] + "\nTry -h for help.");
202             }
203             ind = newInd;
204         }
205     }
206     
207     
208 }
209
210 abstract class CommandLineOption {
211     final String JavaDoc[] switches;
212     final String JavaDoc additionalArg;
213     final String JavaDoc explanation;
214     
215     public CommandLineOption(String JavaDoc swtch, String JavaDoc explanation) {
216         this(swtch, null, explanation);
217     }
218     public CommandLineOption(String JavaDoc swtch, String JavaDoc additionalArgs, String JavaDoc explanation) {
219         this.switches = new String JavaDoc[] {swtch};
220         this.explanation = explanation;
221         this.additionalArg = additionalArgs;
222     }
223     public CommandLineOption(String JavaDoc[] switches, String JavaDoc explanation) {
224         this(switches, null, explanation);
225     }
226     public CommandLineOption(String JavaDoc[] switches, String JavaDoc additionalArgs, String JavaDoc explanation) {
227         this.switches = switches;
228         this.explanation = explanation;
229         this.additionalArg = additionalArgs;
230     }
231     
232     private int currOpt;
233     protected int parse(int currentInd, String JavaDoc[] args) {
234         String JavaDoc s = args[currentInd];
235         if (s.startsWith("-")) {
236             while (s.startsWith("-")) {
237                 s = s.substring(1);
238             }
239             
240             for (int i = 0; i < this.switches.length; i++) {
241                 if (this.switches[i].equals(s)) {
242                     // the command line matches.
243
currOpt = currentInd;
244                     return invoke(currentInd, args);
245                 }
246             }
247         }
248         return currentInd;
249     }
250     
251     protected abstract int invoke(int index, String JavaDoc[] args);
252     
253     protected int getIntArg(int index, String JavaDoc[] args) {
254         try {
255             return Integer.parseInt(args[index]);
256         }
257         catch (ArrayIndexOutOfBoundsException JavaDoc e) {
258             throw new IllegalArgumentException JavaDoc("Expected an integer for the " +
259                 "option " + args[currOpt] + (additionalArg==null?"":(" " +additionalArg)));
260         }
261         catch (NumberFormatException JavaDoc e) {
262             throw new IllegalArgumentException JavaDoc("Expected an integer, not " + args[index]);
263         }
264     }
265     protected String JavaDoc getStringArg(int index, String JavaDoc[] args) {
266         try {
267             return args[index];
268         }
269         catch (ArrayIndexOutOfBoundsException JavaDoc e) {
270             throw new IllegalArgumentException JavaDoc("Expected a string for the " +
271                   "option " + args[currOpt] + (additionalArg==null?"":(" " +additionalArg)));
272         }
273     }
274 }
275
Popular Tags