1 package org.incava.jagol; 2 3 import java.io.*; 4 import java.util.*; 5 6 7 10 public class OptionSet 11 { 12 private List options = new ArrayList(); 13 14 private List rcFiles = new ArrayList(); 15 16 private String appName; 17 18 private String description; 19 20 public OptionSet(String appName, String description) 21 { 22 this.appName = appName; 23 this.description = description; 24 } 25 26 29 public String getAppName() 30 { 31 return appName; 32 } 33 34 37 public String getDescription() 38 { 39 return description; 40 } 41 42 45 public void add(Option opt) 46 { 47 options.add(opt); 48 } 49 50 53 public void addRunControlFile(String name) 54 { 55 tr.Ace.log("adding rc file: " + name); 56 rcFiles.add(name); 57 } 58 59 63 public String [] process(String [] args) 64 { 65 tr.Ace.log("args: " + args); 66 67 processRunControlFiles(); 68 69 return processCommandLine(args); 70 } 71 72 75 protected void processRunControlFiles() 76 { 77 tr.Ace.log(""); 78 Iterator it = rcFiles.iterator(); 79 while (it.hasNext()) { 80 String rcFileName = (String )it.next(); 81 tr.Ace.log("processing: " + rcFileName); 82 try { 83 Properties props = new Properties(); 84 85 int tildePos = rcFileName.indexOf('~'); 86 if (tildePos != -1) { 87 rcFileName = rcFileName.substring(0, tildePos) + System.getProperty("user.home") + rcFileName.substring(tildePos + 1); 88 } 89 90 props.load(new FileInputStream(rcFileName)); 91 92 tr.Ace.log("properties: " + props); 93 Iterator pit = props.keySet().iterator(); 94 while (pit.hasNext()) { 95 String key = (String )pit.next(); 96 String value = (String )props.get(key); 97 tr.Ace.log(key + " => " + value); 98 Iterator oit = options.iterator(); 99 boolean processed = false; 100 while (!processed && oit.hasNext()) { 101 Option opt = (Option)oit.next(); 102 tr.Ace.log("option: " + opt.getLongName()); 103 if (opt.getLongName().equals(key)) { 104 tr.Ace.log("option matches: " + opt); 105 processed = true; 106 try { 107 opt.setValue(value); 108 } 109 catch (OptionException oe) { 110 tr.Ace.log("option exception: " + oe); 111 System.err.println("error: " + oe.getMessage()); 112 } 113 } 114 } 115 } 116 } 117 catch (IOException ioe) { 118 tr.Ace.log("exception: " + ioe); 119 } 121 } 122 } 123 124 128 protected String [] processCommandLine(String [] args) 129 { 130 tr.Ace.log("args: " + args); 131 132 List argList = new ArrayList(); 133 for (int i = 0; i < args.length; ++i) { 134 argList.add(args[i]); 135 } 136 137 tr.Ace.log("arg list: " + argList); 138 139 while (argList.size() > 0) { 140 String arg = (String )argList.get(0); 141 142 tr.Ace.log("arg: " + arg); 143 144 if (arg.equals("--")) { 145 argList.remove(0); 146 break; 147 } 148 else if (arg.charAt(0) == '-') { 149 tr.Ace.log("got leading dash"); 150 argList.remove(0); 151 152 Iterator oit = options.iterator(); 153 boolean processed = false; 154 while (!processed && oit.hasNext()) { 155 Option opt = (Option)oit.next(); 156 tr.Ace.log("option: " + opt); 157 try { 158 processed = opt.set(arg, argList); 159 tr.Ace.log("processed: " + processed); 160 } 161 catch (OptionException oe) { 162 tr.Ace.log("option exception: " + oe); 163 System.err.println("error: " + oe.getMessage()); 164 } 165 } 166 167 if (!processed) { 168 tr.Ace.log("argument not processed: '" + arg + "'"); 169 if (arg.equals("--help") || arg.equals("-h")) { 170 showUsage(); 171 } 172 else if (rcFiles.size() > 0 && arg.equals("--help-config")) { 173 showConfig(); 174 } 175 else { 176 System.err.println("invalid option: " + arg + " (-h will show valid options)"); 177 } 178 179 break; 180 } 181 } 182 else { 183 break; 184 } 185 } 186 187 String [] unprocessed = (String [])argList.toArray(new String [0]); 188 189 tr.Ace.log("args", args); 190 tr.Ace.log("unprocessed", unprocessed); 191 192 return unprocessed; 193 } 194 195 protected void showUsage() 196 { 197 tr.Ace.log("generating help"); 198 199 System.out.println("Usage: " + appName + " [options] file..."); 200 System.out.println(description); 201 System.out.println(); 202 System.out.println("Options:"); 203 204 tr.Ace.log("options: " + options); 205 206 List tags = new ArrayList(); 207 208 Iterator it = options.iterator(); 209 while (it.hasNext()) { 210 Option opt = (Option)it.next(); 211 tr.Ace.log("opt: " + opt); 212 StringBuffer buf = new StringBuffer (" "); 213 214 if (opt.getShortName() == 0) { 215 buf.append(" "); 216 } 217 else { 218 buf.append("-" + opt.getShortName() + ", "); 219 } 220 221 buf.append("--" + opt.getLongName()); 222 223 tags.add(buf.toString()); 224 } 225 226 int widest = -1; 227 Iterator tit = tags.iterator(); 228 while (tit.hasNext()) { 229 String tag = (String )tit.next(); 230 widest = Math.max(tag.length(), widest); 231 } 232 233 it = options.iterator(); 234 tit = tags.iterator(); 235 while (it.hasNext()) { 236 Option opt = (Option)it.next(); 237 String tag = (String )tit.next(); 238 tr.Ace.log("opt: " + opt); 239 240 System.out.print(tag); 241 for (int i = tag.length(); i < widest + 2; ++i) { 242 System.out.print(" "); 243 } 244 245 System.out.println(opt.getDescription()); 246 } 247 248 if (rcFiles.size() > 0) { 249 System.out.println("For an example configure file, run --help-config"); 250 System.out.println(); 251 System.out.println("Configuration File" + (rcFiles.size() > 1 ? "s" : "") + ":"); 252 Iterator rit = rcFiles.iterator(); 253 while (rit.hasNext()) { 254 String rcFileName = (String )rit.next(); 255 System.out.println(" " + rcFileName); 256 } 257 } 258 } 259 260 protected void showConfig() 261 { 262 tr.Ace.log("generating config"); 263 264 Iterator it = options.iterator(); 265 while (it.hasNext()) { 266 Option opt = (Option)it.next(); 267 System.out.println("# " + opt.getDescription()); 268 System.out.println(opt.getLongName() + " = " + opt.toString()); 269 System.out.println(); 270 } 271 } 272 } 273 | Popular Tags |