1 package polyglot.pth; 2 3 import java.io.PrintStream ; 4 import java.util.LinkedList ; 5 import java.util.List ; 6 import java.util.StringTokenizer ; 7 8 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 inputFilenames = new LinkedList (); 16 17 protected int verbosity = 5; 19 20 protected String classpath = null; 22 23 protected String extraArgs = null; 25 26 protected String testFilter = null; 28 29 protected boolean showResultsOnly = false; 31 32 protected boolean testPreviouslyFailedOnly = false; 34 35 protected boolean deleteOutputFiles = true; 37 38 protected CommandLineOption[] commandLineOpts = { 41 new CommandLineOption(new String [] {"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 [] args) { 44 testFilter = ".*" + getStringArg(++index, args) + ".*"; 45 return index + 1; 46 } 47 }, 48 new CommandLineOption(new String [] {"p", "preserve"}, 49 "preserve output files between tests; default is to delete") { 50 protected int invoke(int index, String [] args) { 51 deleteOutputFiles = false; 52 return index + 1; 53 } 54 }, 55 new CommandLineOption(new String [] {"q", "question"}, 56 "show the latest results for the scripts; don't run any tests") { 57 protected int invoke(int index, String [] 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 [] args) { 65 testPreviouslyFailedOnly = true; 66 return index + 1; 67 } 68 }, 69 new CommandLineOption(new String [] {"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 [] args) { 72 verbosity = getIntArg(++index, args); 73 if (verbosity < 0 || verbosity > 9) { 74 throw new IllegalArgumentException ("Verbosity must be " + 75 "between 0 and 9 inclusive."); 76 } 77 return index + 1; 78 } 79 }, 80 new CommandLineOption(new String [] {"cp", "classpath"}, "path", 81 "set the class path for the Polyglot compiler.") { 82 protected int invoke(int index, String [] args) { 83 classpath = getStringArg(++index, args); 84 return index + 1; 85 } 86 }, 87 new CommandLineOption(new String [] {"args"}, "extraArgs", 88 "provide additional command line arguments to the Polyglot compiler.") { 89 protected int invoke(int index, String [] args) { 90 extraArgs = getStringArg(++index, args); 91 return index + 1; 92 } 93 }, 94 new CommandLineOption(new String [] {"h", "help", "?"}, 95 "Display this message.") { 96 protected int invoke(int index, String [] args) { 97 usage(System.out); 98 System.exit(0); 99 return index + 1; 100 } 101 }, 102 new CommandLineOption(new String [] {}, 103 "Files") { 104 protected int invoke(int index, String [] args) { return index; } 105 protected int parse(int index, String [] args) { 106 if (!args[index].startsWith("-")) inputFilenames.add(args[index++]); 107 return index; 108 } 109 }, 110 }; 111 112 protected void usage(PrintStream 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 out, CommandLineOption arg) { 123 if (arg.switches.length == 0) return; 124 125 out.print(" "); 126 int cur = 2; 128 129 for (int i = 0; i < arg.switches.length; i++) { 130 String 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 if (cur < USAGE_FLAG_WIDTH) { 145 printSpaces(out, USAGE_FLAG_WIDTH - cur); 146 } 147 else { 148 out.println(); 151 printSpaces(out, USAGE_FLAG_WIDTH); 152 } 153 cur = USAGE_FLAG_WIDTH; 154 155 StringTokenizer st = new StringTokenizer (arg.explanation); 157 while (st.hasMoreTokens()) { 158 String 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 out, int n) { 182 while (n-- > 0) { 183 out.print(' '); 184 } 185 } 186 187 protected void parseCommandLine(String [] args) { 188 int ind = 0; 189 if (args == null || args.length == 0) { 190 args = new String [] {"-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 ("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 [] switches; 212 final String additionalArg; 213 final String explanation; 214 215 public CommandLineOption(String swtch, String explanation) { 216 this(swtch, null, explanation); 217 } 218 public CommandLineOption(String swtch, String additionalArgs, String explanation) { 219 this.switches = new String [] {swtch}; 220 this.explanation = explanation; 221 this.additionalArg = additionalArgs; 222 } 223 public CommandLineOption(String [] switches, String explanation) { 224 this(switches, null, explanation); 225 } 226 public CommandLineOption(String [] switches, String additionalArgs, String 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 [] args) { 234 String 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 currOpt = currentInd; 244 return invoke(currentInd, args); 245 } 246 } 247 } 248 return currentInd; 249 } 250 251 protected abstract int invoke(int index, String [] args); 252 253 protected int getIntArg(int index, String [] args) { 254 try { 255 return Integer.parseInt(args[index]); 256 } 257 catch (ArrayIndexOutOfBoundsException e) { 258 throw new IllegalArgumentException ("Expected an integer for the " + 259 "option " + args[currOpt] + (additionalArg==null?"":(" " +additionalArg))); 260 } 261 catch (NumberFormatException e) { 262 throw new IllegalArgumentException ("Expected an integer, not " + args[index]); 263 } 264 } 265 protected String getStringArg(int index, String [] args) { 266 try { 267 return args[index]; 268 } 269 catch (ArrayIndexOutOfBoundsException e) { 270 throw new IllegalArgumentException ("Expected a string for the " + 271 "option " + args[currOpt] + (additionalArg==null?"":(" " +additionalArg))); 272 } 273 } 274 } 275 | Popular Tags |