1 52 53 package org.exolab.jms.util; 54 55 import java.util.Hashtable ; 56 import java.util.Vector ; 57 58 59 69 public class CommandLine { 70 71 75 private Vector _switches = new Vector (); 76 77 80 private Hashtable _options = new Hashtable (); 81 82 88 public CommandLine(String [] args) { 89 processCommandLine(args); 90 } 91 92 95 public CommandLine() { 96 } 97 98 104 public boolean exists(String name) { 105 return _switches.contains(name) || _options.containsKey(name); 106 } 107 108 114 public boolean isSwitch(String name) { 115 return _switches.contains(name); 116 } 117 118 124 public boolean isParameter(String name) { 125 return _options.containsKey(name); 126 } 127 128 135 public String value(String name) { 136 String result = null; 137 138 if (_options.containsKey(name)) { 139 result = (String ) _options.get(name); 140 } 141 142 return result; 143 } 144 145 153 public String value(String name, String defaultValue) { 154 String result = value(name); 155 return (result != null) ? result : defaultValue; 156 } 157 158 169 public boolean add(String name, String value) { 170 return add(name, value, true); 171 } 172 173 187 public boolean add(String name, String value, boolean overwrite) { 188 boolean result = false; 189 190 if (value == null) { 191 if ((_switches.contains(name)) && 193 (overwrite)) { 194 _switches.addElement(name); 195 result = true; 196 } else if (!_switches.contains(name)) { 197 _switches.addElement(name); 198 result = true; 199 } 200 } else { 201 if ((_options.containsKey(name)) && 203 (overwrite)) { 204 _options.put(name, value); 205 result = true; 206 } else if (!_options.containsKey(name)) { 207 _options.put(name, value); 208 result = true; 209 } 210 } 211 212 return result; 213 } 214 215 222 private void processCommandLine(String [] args) { 223 boolean prev_was_hyphen = false; 224 String prev_key = null; 225 226 for (int index = 0; index < args.length; index++) { 227 if (args[index].startsWith("-")) { 228 if (prev_was_hyphen) { 234 add(prev_key, null); 235 } 236 237 prev_key = args[index].substring(1); 238 prev_was_hyphen = true; 239 240 if (index == args.length - 1) { 244 add(prev_key, null); 245 break; 246 } 247 } else { 248 if (prev_key != null) { 251 add(prev_key, args[index]); 252 prev_key = null; 253 } 254 prev_was_hyphen = false; 255 } 256 } 257 } 258 259 } 260 | Popular Tags |