1 package org.jdesktop.demo; 2 3 import java.text.NumberFormat ; 4 import java.text.ParseException ; 5 import java.util.Hashtable ; 6 import java.util.Vector ; 7 import java.util.Enumeration ; 8 import java.util.Locale ; 9 10 23 public class CmdLineParser { 24 25 28 public static abstract class OptionException extends Exception { 29 OptionException(String msg) { super(msg); } 30 } 31 32 38 public static class UnknownOptionException extends OptionException { 39 UnknownOptionException( String optionName ) { 40 super("unknown option '" + optionName + "'"); 41 this.optionName = optionName; 42 } 43 44 47 public String getOptionName() { return this.optionName; } 48 private String optionName = null; 49 } 50 51 57 public static class IllegalOptionValueException extends OptionException { 58 public IllegalOptionValueException( Option opt, String value ) { 59 super("illegal value '" + value + "' for option -" + 60 opt.shortForm() + "/--" + opt.longForm()); 61 this.option = opt; 62 this.value = value; 63 } 64 65 68 public Option getOption() { return this.option; } 69 70 73 public String getValue() { return this.value; } 74 private Option option; 75 private String value; 76 } 77 78 81 public static abstract class Option { 82 83 protected Option( char shortForm, String longForm, 84 boolean wantsValue ) { 85 if ( longForm == null ) 86 throw new IllegalArgumentException ("null arg forms not allowed"); 87 this.shortForm = new String (new char[]{shortForm}); 88 this.longForm = longForm; 89 this.wantsValue = wantsValue; 90 } 91 92 public String shortForm() { return this.shortForm; } 93 94 public String longForm() { return this.longForm; } 95 96 99 public boolean wantsValue() { return this.wantsValue; } 100 101 public final Object getValue( String arg, Locale locale ) 102 throws IllegalOptionValueException { 103 if ( this.wantsValue ) { 104 if ( arg == null ) { 105 throw new IllegalOptionValueException(this, ""); 106 } 107 return this.parseValue(arg, locale); 108 } 109 else { 110 return Boolean.TRUE; 111 } 112 } 113 114 118 protected Object parseValue( String arg, Locale locale ) 119 throws IllegalOptionValueException { 120 return null; 121 } 122 123 private String shortForm = null; 124 private String longForm = null; 125 private boolean wantsValue = false; 126 127 public static class BooleanOption extends Option { 128 public BooleanOption( char shortForm, String longForm ) { 129 super(shortForm, longForm, false); 130 } 131 } 132 133 136 public static class IntegerOption extends Option { 137 public IntegerOption( char shortForm, String longForm ) { 138 super(shortForm, longForm, true); 139 } 140 protected Object parseValue( String arg, Locale locale ) 141 throws IllegalOptionValueException { 142 try { 143 return new Integer (arg); 144 } 145 catch (NumberFormatException e) { 146 throw new IllegalOptionValueException(this, arg); 147 } 148 } 149 } 150 151 154 public static class DoubleOption extends Option { 155 public DoubleOption( char shortForm, String longForm ) { 156 super(shortForm, longForm, true); 157 } 158 protected Object parseValue( String arg, Locale locale ) 159 throws IllegalOptionValueException { 160 try { 161 NumberFormat format = NumberFormat.getNumberInstance(locale); 162 Number num = (Number )format.parse(arg); 163 return new Double (num.doubleValue()); 164 } 165 catch (ParseException e) { 166 throw new IllegalOptionValueException(this, arg); 167 } 168 } 169 } 170 171 174 public static class StringOption extends Option { 175 public StringOption( char shortForm, String longForm ) { 176 super(shortForm, longForm, true); 177 } 178 protected Object parseValue( String arg, Locale locale ) { 179 return arg; 180 } 181 } 182 } 183 184 187 public final Option addOption( Option opt ) { 188 this.options.put("-" + opt.shortForm(), opt); 189 this.options.put("--" + opt.longForm(), opt); 190 return opt; 191 } 192 193 197 public final Option addStringOption( char shortForm, String longForm ) { 198 Option opt = new Option.StringOption(shortForm, longForm); 199 addOption(opt); 200 return opt; 201 } 202 203 207 public final Option addIntegerOption( char shortForm, String longForm ) { 208 Option opt = new Option.IntegerOption(shortForm, longForm); 209 addOption(opt); 210 return opt; 211 } 212 213 217 public final Option addDoubleOption( char shortForm, String longForm ) { 218 Option opt = new Option.DoubleOption(shortForm, longForm); 219 addOption(opt); 220 return opt; 221 } 222 223 227 public final Option addBooleanOption( char shortForm, String longForm ) { 228 Option opt = new Option.BooleanOption(shortForm, longForm); 229 addOption(opt); 230 return opt; 231 } 232 233 237 public final Object getOptionValue( Option o ) { 238 return values.get(o.longForm()); 239 } 240 241 245 public final Object getOptionValue( Option o, Object defaultValue ) { 246 Object val = values.get(o.longForm()); 247 return val == null ? defaultValue : val; 248 } 249 250 253 public final String [] getRemainingArgs() { 254 return this.remainingArgs; 255 } 256 257 262 public final void parse( String [] argv ) 263 throws IllegalOptionValueException, UnknownOptionException { 264 parse(argv, Locale.getDefault()); 265 } 266 267 272 public final void parse( String [] argv, Locale locale ) 273 throws IllegalOptionValueException, UnknownOptionException { 274 Vector otherArgs = new Vector (); 275 int position = 0; 276 this.values = new Hashtable (10); 277 while ( position < argv.length ) { 278 String curArg = argv[position]; 279 if ( curArg.startsWith("-") ) { 280 if ( curArg.equals("--") ) { position += 1; 282 break; 283 } 284 String valueArg = null; 285 if ( curArg.startsWith("--") ) { int equalsPos = curArg.indexOf("="); 287 if ( equalsPos != -1 ) { 288 valueArg = curArg.substring(equalsPos+1); 289 curArg = curArg.substring(0,equalsPos); 290 } 291 } 292 Option opt = (Option)this.options.get(curArg); 293 if ( opt == null ) { 294 throw new UnknownOptionException(curArg); 295 } 296 Object value = null; 297 if ( opt.wantsValue() ) { 298 if ( valueArg == null ) { 299 position += 1; 300 valueArg = null; 301 if ( position < argv.length ) { 302 valueArg = argv[position]; 303 } 304 } 305 value = opt.getValue(valueArg, locale); 306 } 307 else { 308 value = opt.getValue(null, locale); 309 } 310 this.values.put(opt.longForm(), value); 311 position += 1; 312 } 313 else { 314 break; 315 } 316 } 317 for ( ; position < argv.length; ++position ) { 318 otherArgs.addElement(argv[position]); 319 } 320 321 this.remainingArgs = new String [otherArgs.size()]; 322 int i = 0; 323 for (Enumeration e = otherArgs.elements(); e.hasMoreElements(); ++i) { 324 this.remainingArgs[i] = (String )e.nextElement(); 325 } 326 } 327 328 private String [] remainingArgs = null; 329 private Hashtable options = new Hashtable (10); 330 private Hashtable values = new Hashtable (10); 331 } 332 | Popular Tags |