1 32 33 package com.jeantessier.commandline; 34 35 import java.util.*; 36 37 40 public class CommandLine implements Visitable { 41 private static final boolean DEFAULT_STRICT = true; 42 private static final ParameterStrategy DEFAULT_PARAMETER_STRATEGY = new AnyParameterStrategy(); 43 44 private boolean strict; 45 private ParameterStrategy parameterStrategy; 46 47 private List parameters = new LinkedList(); 48 private Map map = new TreeMap(); 49 50 public CommandLine() { 51 this(DEFAULT_STRICT, DEFAULT_PARAMETER_STRATEGY); 52 } 53 54 public CommandLine(boolean strict) { 55 this(strict, DEFAULT_PARAMETER_STRATEGY); 56 } 57 58 public CommandLine(ParameterStrategy parameterStrategy) { 59 this(DEFAULT_STRICT, parameterStrategy); 60 } 61 62 public CommandLine(boolean strict, ParameterStrategy parameterStrategy) { 63 setStrict(strict); 64 setParameterStrategy(parameterStrategy); 65 } 66 67 public boolean isStrict() { 68 return strict; 69 } 70 71 public void setStrict(boolean strict) { 72 this.strict = strict; 73 } 74 75 public ParameterStrategy getParameterStrategy() { 76 return parameterStrategy; 77 } 78 79 public void setParameterStrategy(ParameterStrategy parameterStrategy) { 80 this.parameterStrategy = parameterStrategy; 81 } 82 83 public void addSwitch(String name, CommandLineSwitch cls) { 84 map.put(name, cls); 85 } 86 87 public void addToggleSwitch(String name) { 88 addSwitch(name, new ToggleSwitch()); 89 } 90 91 public void addToggleSwitch(String name, boolean defaultValue) { 92 addSwitch(name, new ToggleSwitch(defaultValue)); 93 } 94 95 public void addSingleValueSwitch(String name) { 96 addSwitch(name, new SingleValueSwitch()); 97 } 98 99 public void addSingleValueSwitch(String name, boolean mandatory) { 100 addSwitch(name, new SingleValueSwitch(mandatory)); 101 } 102 103 public void addSingleValueSwitch(String name, String defaultValue) { 104 addSwitch(name, new SingleValueSwitch(defaultValue)); 105 } 106 107 public void addSingleValueSwitch(String name, String defaultValue, boolean mandatory) { 108 addSwitch(name, new SingleValueSwitch(defaultValue, mandatory)); 109 } 110 111 public void addOptionalValueSwitch(String name) { 112 addSwitch(name, new OptionalValueSwitch()); 113 } 114 115 public void addOptionalValueSwitch(String name, boolean mandatory) { 116 addSwitch(name, new OptionalValueSwitch(mandatory)); 117 } 118 119 public void addOptionalValueSwitch(String name, String defaultValue) { 120 addSwitch(name, new OptionalValueSwitch(defaultValue)); 121 } 122 123 public void addOptionalValueSwitch(String name, String defaultValue, boolean mandatory) { 124 addSwitch(name, new OptionalValueSwitch(defaultValue, mandatory)); 125 } 126 127 public void addMultipleValuesSwitch(String name) { 128 map.put(name, new MultipleValuesSwitch()); 129 } 130 131 public void addMultipleValuesSwitch(String name, boolean mandatory) { 132 map.put(name, new MultipleValuesSwitch(mandatory)); 133 } 134 135 public void addMultipleValuesSwitch(String name, String defaultValue) { 136 map.put(name, new MultipleValuesSwitch(defaultValue)); 137 } 138 139 public void addMultipleValuesSwitch(String name, String defaultValue, boolean mandatory) { 140 map.put(name, new MultipleValuesSwitch(defaultValue, mandatory)); 141 } 142 143 public CommandLineSwitch getSwitch(String name) { 144 return (CommandLineSwitch) map.get(name); 145 } 146 147 public boolean getToggleSwitch(String name) { 148 boolean result = false; 149 150 CommandLineSwitch cls = (CommandLineSwitch) map.get(name); 151 if (cls != null) { 152 result = ((Boolean ) cls.getValue()).booleanValue(); 153 } 154 155 return result; 156 } 157 158 public String getSingleSwitch(String name) { 159 return getStringSwitch(name); 160 } 161 162 public String getOptionalSwitch(String name) { 163 return getStringSwitch(name); 164 } 165 166 public List getMultipleSwitch(String name) { 167 return getListSwitch(name); 168 } 169 170 private String getStringSwitch(String name) { 171 String result = null; 172 173 CommandLineSwitch cls = (CommandLineSwitch) map.get(name); 174 if (cls != null) { 175 result = cls.getValue().toString(); 176 } 177 178 return result; 179 } 180 181 private List getListSwitch(String name) { 182 List result = null; 183 184 CommandLineSwitch cls = (CommandLineSwitch) map.get(name); 185 if (cls != null && cls.getValue() instanceof List) { 186 result = (List) cls.getValue(); 187 } 188 189 return result; 190 } 191 192 public boolean isPresent(String name) { 193 boolean result = false; 194 195 CommandLineSwitch cls = (CommandLineSwitch) map.get(name); 196 if (cls != null) { 197 result = cls.isPresent(); 198 } 199 200 return result; 201 } 202 203 public Set getKnownSwitches() { 204 return map.keySet(); 205 } 206 207 public Set getPresentSwitches() { 208 Set result = new TreeSet(); 209 210 Iterator i = getKnownSwitches().iterator(); 211 while (i.hasNext()) { 212 String name = (String ) i.next(); 213 CommandLineSwitch cls = (CommandLineSwitch) map.get(name); 214 215 if (cls.isPresent()) { 216 result.add(name); 217 } 218 } 219 220 return result; 221 } 222 223 public List getParameters() { 224 return parameters; 225 } 226 227 public void parse(String args[]) throws CommandLineException { 228 parameters = new LinkedList(); 229 230 int i=0; 231 while (i < args.length) { 232 if (args[i].startsWith("-")) { 233 String name = args[i].substring(1); 234 String value = null; 235 236 if (i+1 < args.length && !map.containsKey(args[i+1].substring(1))) { 237 value = args[i+1]; 238 } 239 240 CommandLineSwitch cls = (CommandLineSwitch) map.get(name); 241 242 if (cls == null) { 243 if (isStrict()) { 244 throw new CommandLineException("Unknown switch \"" + args[i] + "\""); 245 } else { 246 cls = new OptionalValueSwitch(); 247 map.put(name, cls); 248 } 249 } 250 251 i += cls.parse(name, value); 252 } else if (parameterStrategy.accept(args[i])) { 253 parameters.add(args[i]); 254 i++; 255 } else { 256 throw new CommandLineException("Invalid parameter \"" + args[i] + "\""); 257 } 258 } 259 260 Iterator j = map.keySet().iterator(); 262 while (j.hasNext()) { 263 String name = (String ) j.next(); 264 CommandLineSwitch cls = (CommandLineSwitch) map.get(name); 265 266 if (cls.isMandatory() && !cls.isPresent()) { 267 throw new CommandLineException("Missing mandatory switch \"" + name + "\""); 268 } 269 } 270 271 if (!parameterStrategy.isSatisfied()) { 273 throw new CommandLineException("Missing mandatory parameters"); 274 } 275 } 276 277 public void accept(Visitor visitor) { 278 visitor.visitCommandLine(this); 279 } 280 } 281 | Popular Tags |