1 package com.thaiopensource.util; 2 3 public class OptionParser { 4 private final String optionSpec; 5 private char optionChar = 0; 6 private String optionArg = null; 7 private int argIndex = 0; 8 private int currentOptionIndex = 0; 9 private final String [] args; 10 11 private static final char OPTION_CHAR = '-'; 12 13 public static class MissingArgumentException extends Exception { } 14 15 public static class InvalidOptionException extends Exception { } 16 17 public OptionParser(String optionSpec, String [] args) { 18 this.optionSpec = optionSpec; 19 this.args = new String [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 getOptionCharString() { 28 return new String (new char[]{optionChar}); 29 } 30 31 public String 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 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 [] getRemainingArgs() { 77 String [] tem = new String [args.length - argIndex]; 78 System.arraycopy(args, argIndex, tem, 0, tem.length); 79 return tem; 80 } 81 82 public static void main(String [] args) { 83 String optSpec = args[0]; 84 String [] tem = new String [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 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 |