1 22 package org.netbeans.lib.cvsclient.commandLine; 23 24 103 public class GetOpt { 104 105 private String [] theArgs = null; 106 private int argCount = 0; 107 private String optString = null; 108 109 public GetOpt(String [] args, String opts) { 110 theArgs = args; 111 argCount = theArgs.length; 112 optString = opts; 113 } 114 115 public boolean optErr = false; 117 118 public int processArg(String arg, int n) { 119 int value; 120 try { 121 value = Integer.parseInt(arg); 122 } 123 catch (NumberFormatException e) { 124 if (optErr) 125 System.err.println("processArg cannot process " + arg + " as an integer"); return n; 128 } 129 return value; 130 } 131 132 public int tryArg(int k, int n) { 133 int value; 134 try { 135 value = processArg(theArgs[k], n); 136 } 137 catch (ArrayIndexOutOfBoundsException e) { 138 if (optErr) 139 System.err.println("tryArg: no theArgs[" + k + "]"); return n; 141 } 142 return value; 143 } 144 145 public long processArg(String arg, long n) { 146 long value; 147 try { 148 value = Long.parseLong(arg); 149 } 150 catch (NumberFormatException e) { 151 if (optErr) 152 System.err.println("processArg cannot process " + arg + " as a long"); return n; 155 } 156 return value; 157 } 158 159 public long tryArg(int k, long n) { 160 long value; 161 try { 162 value = processArg(theArgs[k], n); 163 } 164 catch (ArrayIndexOutOfBoundsException e) { 165 if (optErr) 166 System.err.println("tryArg: no theArgs[" + k + "]"); return n; 168 } 169 return value; 170 } 171 172 public double processArg(String arg, double d) { 173 double value; 174 try { 175 value = Double.valueOf(arg).doubleValue(); 176 } 177 catch (NumberFormatException e) { 178 if (optErr) 179 System.err.println("processArg cannot process " + arg + " as a double"); return d; 182 } 183 return value; 184 } 185 186 public double tryArg(int k, double d) { 187 double value; 188 try { 189 value = processArg(theArgs[k], d); 190 } 191 catch (ArrayIndexOutOfBoundsException e) { 192 if (optErr) 193 System.err.println("tryArg: no theArgs[" + k + "]"); return d; 195 } 196 return value; 197 } 198 199 public float processArg(String arg, float f) { 200 float value; 201 try { 202 value = Float.valueOf(arg).floatValue(); 203 } 204 catch (NumberFormatException e) { 205 if (optErr) 206 System.err.println("processArg cannot process " + arg + " as a float"); return f; 209 } 210 return value; 211 } 212 213 public float tryArg(int k, float f) { 214 float value; 215 try { 216 value = processArg(theArgs[k], f); 217 } 218 catch (ArrayIndexOutOfBoundsException e) { 219 if (optErr) 220 System.err.println("tryArg: no theArgs[" + k + "]"); return f; 222 } 223 return value; 224 } 225 226 public boolean processArg(String arg, boolean b) { 227 return Boolean.valueOf(arg).booleanValue(); 229 } 230 231 public boolean tryArg(int k, boolean b) { 232 boolean value; 233 try { 234 value = processArg(theArgs[k], b); 235 } 236 catch (ArrayIndexOutOfBoundsException e) { 237 if (optErr) 238 System.err.println("tryArg: no theArgs[" + k + "]"); return b; 240 } 241 return value; 242 } 243 244 public String tryArg(int k, String s) { 245 String value; 246 try { 247 value = theArgs[k]; 248 } 249 catch (ArrayIndexOutOfBoundsException e) { 250 if (optErr) 251 System.err.println("tryArg: no theArgs[" + k + "]"); return s; 253 } 254 return value; 255 } 256 257 private static void writeError(String msg, char ch) { 258 System.err.println("GetOpt: " + msg + " -- " + ch); } 260 261 public static final int optEOF = -1; 262 263 private int optIndex = 0; 264 265 public int optIndexGet() { 266 return optIndex; 267 } 268 269 public void optIndexSet(int i) { 270 optIndex = i; 271 } 272 273 private String optArg = null; 274 275 public String optArgGet() { 276 return optArg; 277 } 278 279 private int optPosition = 1; 280 281 public int getopt() { 282 optArg = null; 283 if (theArgs == null || optString == null) 284 return optEOF; 285 if (optIndex < 0 || optIndex >= argCount) 286 return optEOF; 287 String thisArg = theArgs[optIndex]; 288 int argLength = thisArg.length(); 289 if (argLength <= 1 || thisArg.charAt(0) != '-') { 291 return optEOF; 293 } 294 else if (thisArg.equals("--")) { optIndex++; 297 return optEOF; 298 } 299 char ch = thisArg.charAt(optPosition); 301 int pos = optString.indexOf(ch); 303 if (pos == -1 || ch == ':') { 304 if (optErr) { 305 writeError("illegal option", ch); } 307 ch = '?'; 308 } 309 else { if (pos < optString.length() - 1 && optString.charAt(pos + 1) == ':') { 311 if (optPosition != argLength - 1) { 312 optArg = thisArg.substring(optPosition + 1); 314 optPosition = argLength - 1; } 316 else { optIndex++; 318 if (optIndex < argCount 319 && (theArgs[optIndex].charAt(0) != '-' || 320 theArgs[optIndex].length() >= 2 && 321 (optString.indexOf(theArgs[optIndex].charAt(1)) == -1 322 || theArgs[optIndex].charAt(1) == ':'))) { 323 optArg = theArgs[optIndex]; 324 } 325 else { 326 if (optErr) { 327 writeError("option requires an argument", ch); } 329 optArg = null; 330 ch = ':'; } 332 } 333 } 334 } 335 optPosition++; 338 if (optPosition >= argLength) { 339 optIndex++; 340 optPosition = 1; 341 } 342 return ch; 343 } 344 345 public static void main(String [] args) { GetOpt go = new GetOpt(args, "Uab:f:h:w:"); 347 go.optErr = true; 348 int ch = -1; 349 boolean usagePrint = false; int aflg = 0; boolean bflg = false; String filename = "out"; int width = 80; double height = 1; while ((ch = go.getopt()) != go.optEOF) { 357 if ((char)ch == 'U') 358 usagePrint = true; 359 else if ((char)ch == 'a') 360 aflg++; 361 else if ((char)ch == 'b') 362 bflg = go.processArg(go.optArgGet(), bflg); 363 else if ((char)ch == 'f') 364 filename = go.optArgGet(); 365 else if ((char)ch == 'h') 366 height = go.processArg(go.optArgGet(), height); 367 else if ((char)ch == 'w') 368 width = go.processArg(go.optArgGet(), width); 369 else 370 System.exit(1); } if (usagePrint) { 373 System.out.println("Usage: -a -b bool -f file -h height -w width"); System.exit(0); 375 } 376 System.out.println("These are all the command line arguments " + "before processing with GetOpt:"); for (int i = 0; i < args.length; i++) { 379 System.out.print(" " + args[i]); } 381 System.out.println(); 382 System.out.println("-U " + usagePrint); System.out.println("-a " + aflg); System.out.println("-b " + bflg); System.out.println("-f " + filename); System.out.println("-h " + height); System.out.println("-w " + width); for (int k = go.optIndexGet(); k < args.length; k++) { 390 System.out.println("normal argument " + k + " is " + args[k]); } 392 } 393 } 394 395 420 421 | Popular Tags |