1 19 20 package edu.umd.cs.findbugs.config; 21 22 import java.io.BufferedReader ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStreamReader ; 26 import java.io.OutputStream ; 27 import java.io.PrintStream ; 28 import java.nio.charset.Charset ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 import edu.umd.cs.findbugs.annotations.SuppressWarnings; 38 39 40 43 public abstract class CommandLine { 44 private List <String > optionList; 45 private Set <String > requiresArgumentSet; 46 private Map <String , String > optionDescriptionMap; 47 private Map <String , String > optionExtraPartSynopsisMap; 48 private Map <String , String > argumentDescriptionMap; 49 int maxWidth; 50 51 public CommandLine() { 52 this.optionList = new LinkedList <String >(); 53 this.requiresArgumentSet = new HashSet <String >(); 54 this.optionDescriptionMap = new HashMap <String , String >(); 55 this.optionExtraPartSynopsisMap = new HashMap <String , String >(); 56 this.argumentDescriptionMap = new HashMap <String , String >(); 57 this.maxWidth = 0; 58 } 59 60 68 public void addSwitch(String option, String description) { 69 optionList.add(option); 70 optionDescriptionMap.put(option, description); 71 72 if (option.length() > maxWidth) 73 maxWidth = option.length(); 74 } 75 76 84 public void addSwitchWithOptionalExtraPart(String option, String optionExtraPartSynopsis, 85 String description) { 86 optionList.add(option); 87 optionExtraPartSynopsisMap.put(option, optionExtraPartSynopsis); 88 optionDescriptionMap.put(option, description); 89 90 int length = option.length() + optionExtraPartSynopsis.length() + 3; 92 if (length > maxWidth) 93 maxWidth = length; 94 } 95 96 103 public void addOption(String option, String argumentDesc, String description) { 104 optionList.add(option); 105 optionDescriptionMap.put(option, description); 106 requiresArgumentSet.add(option); 107 argumentDescriptionMap.put(option, argumentDesc); 108 109 int width = option.length() + 3 + argumentDesc.length(); 110 if (width > maxWidth) 111 maxWidth = width; 112 } 113 114 124 public static String [] expandOptionFiles( 125 String [] argv, boolean ignoreComments, boolean ignoreBlankLines) 126 throws IOException { 127 ArrayList <String > resultList = new ArrayList <String >(); 128 129 for (String arg : argv) { 130 if (!arg.startsWith("@")) { 131 resultList.add(arg); 132 continue; 133 } 134 135 BufferedReader reader = null; 136 try { 137 reader = new BufferedReader (new InputStreamReader ( 138 new FileInputStream (arg.substring(1)), Charset.forName("UTF-8"))); 139 String line; 140 while ((line = reader.readLine()) != null) { 141 line = line.trim(); 142 143 if (ignoreComments && line.startsWith("#")) 144 continue; 145 146 if (ignoreBlankLines && line.equals("")) 147 continue; 148 149 resultList.add(line); 150 } 151 } finally { 152 if (reader != null) { 153 try { 154 reader.close(); 155 } catch (IOException ignore) { 156 } 158 } 159 } 160 } 161 162 return resultList.toArray(new String [resultList.size()]); 163 } 164 165 public static class HelpRequestedException extends Exception { 166 167 } 168 @SuppressWarnings ("DM_EXIT") 169 public int parse(String argv[], int minArgs, int maxArgs, String usage) { 170 try { 171 int count = parse(argv); 172 int remaining = argv.length - count; 173 if (remaining < minArgs || remaining > maxArgs) { 174 System.out.println(usage); 175 System.out.println("Expected " +minArgs + "..." + maxArgs 176 + " file arguments, found " + remaining); 177 System.out.println("Options:"); 178 printUsage(System.out); 179 System.exit(1); 180 } 181 return count; 182 } catch (HelpRequestedException e) { 183 185 } catch (RuntimeException e) { 186 e.printStackTrace(); 187 } catch (IOException e) { 188 e.printStackTrace(); 189 } 190 System.out.println(usage); 191 System.out.println("Options:"); 192 printUsage(System.out); 193 System.exit(1); 194 return -1; 195 } 196 207 public int parse(String argv[]) throws IOException , HelpRequestedException { 208 int arg = 0; 209 210 while (arg < argv.length) { 211 String option = argv[arg]; 212 if (option.equals("-help")) 213 throw new HelpRequestedException(); 214 if (!option.startsWith("-")) 215 break; 216 217 String optionExtraPart = ""; 218 int colon = option.indexOf(':'); 219 if (colon >= 0) { 220 optionExtraPart = option.substring(colon + 1); 221 option = option.substring(0, colon); 222 } 223 224 if (optionDescriptionMap.get(option) == null) 225 throw new IllegalArgumentException ("Unknown option: " + option); 226 227 if (requiresArgumentSet.contains(option)) { 228 ++arg; 229 if (arg >= argv.length) 230 throw new IllegalArgumentException ("Option " + option + " requires an argument"); 231 String argument = argv[arg]; 232 handleOptionWithArgument(option, argument); 233 ++arg; 234 } else { 235 handleOption(option, optionExtraPart); 236 ++arg; 237 } 238 } 239 240 return arg; 241 } 242 243 251 protected abstract void handleOption(String option, String optionExtraPart) 252 throws IOException ; 253 254 260 protected abstract void handleOptionWithArgument(String option, String argument) throws IOException ; 261 262 267 public void printUsage(OutputStream os) { 268 PrintStream out = new PrintStream (os); 269 for (String option : optionList) { 270 out.print(" "); 271 272 StringBuffer buf = new StringBuffer (); 273 buf.append(option); 274 if (optionExtraPartSynopsisMap.get(option) != null) { 275 String optionExtraPartSynopsis = optionExtraPartSynopsisMap.get(option); 276 buf.append("[:"); 277 buf.append(optionExtraPartSynopsis); 278 buf.append("]"); 279 } 280 if (requiresArgumentSet.contains(option)) { 281 buf.append(" <"); 282 buf.append(argumentDescriptionMap.get(option)); 283 buf.append(">"); 284 } 285 printField(out, buf.toString(), maxWidth + 1); 286 287 out.println(optionDescriptionMap.get(option)); 288 } 289 out.flush(); 290 } 291 292 private static final String SPACES = " "; 293 294 private static void printField(PrintStream out, String s, int width) { 295 if (s.length() > width) throw new IllegalArgumentException (); 296 int nSpaces = width - s.length(); 297 out.print(s); 298 while (nSpaces > 0) { 299 int n = Math.min(SPACES.length(), nSpaces); 300 out.print(SPACES.substring(0, n)); 301 nSpaces -= n; 302 } 303 } 304 } 305 306 | Popular Tags |