1 package de.uni_hamburg.eggink.autojar; 2 3 19 20 import java.io.*; 21 import java.util.*; 22 23 73 74 public class Getopt 75 { 76 private String [] argv; 77 private ArrayList arglist; 78 private String optarg, opts; 79 private int ichr = 0, optind = 0; 80 private char optopt; 81 private boolean opterr = true; 82 83 85 89 90 private Getopt(String opts) 91 { 92 this.opts = opts; 93 arglist = new ArrayList(); 94 } 95 96 98 104 105 public Getopt(String filename, String opts, boolean opterr) 106 throws IOException 107 { 108 this(filename, opts); 109 this.opterr = opterr; 110 } 111 112 114 115 116 public Getopt(String filename, String opts) 117 throws IOException 118 { 119 this(opts); 120 addArgsFromFile(filename); 121 } 122 123 125 133 134 public Getopt(String [] argv, String opts, boolean opterr) 135 throws IOException 136 { 137 this(argv, opts); 138 this.opterr = opterr; 139 } 140 141 143 144 145 public Getopt(String [] argv, String opts) 146 throws IOException 147 { 148 this(opts); 149 150 for (int i = 0; i < argv.length; ++i) 151 { 152 String arg = argv[i]; 153 154 if (arg.startsWith("@")) 155 addArgsFromFile(arg.substring(1)); 156 else 157 arglist.add(arg); 158 } 159 160 this.argv = (String [])arglist.toArray(new String [0]); 161 } 162 163 165 private void addArgsFromFile(String name) 166 throws IOException 167 { 168 BufferedReader reader = new BufferedReader(new FileReader(new File(name))); 169 String zeile; 170 171 while ((zeile = reader.readLine()) != null) 172 arglist.add(zeile); 173 174 reader.close(); 175 } 176 177 179 180 181 public String getOptarg() 182 { 183 return optarg; 184 } 185 186 188 191 192 public int getOption() 193 { 194 char c; 195 int iopt; 196 197 if (ichr == 0) 198 { 199 201 if (optind >= argv.length || argv[optind].charAt(0) != '-') 202 return -1; 203 204 if (argv[optind].equals("-") || argv[optind].equals("--")) 205 { 206 ++optind; 207 return -1; 208 } 209 } 210 211 213 c = argv[optind].charAt(++ichr); 214 215 if (c == ':' || (iopt = opts.indexOf(c)) < 0) 216 { 217 if (opterr) 218 System.err.println("+++ Illegal option: " + c); 219 220 if (++ichr >= argv[optind].length()) 221 { 222 ++optind; 223 ichr = 0; 224 } 225 226 optopt = c; 227 optarg = null; 228 return '?'; 229 } 230 231 if (iopt + 1 < opts.length() && opts.charAt(iopt + 1) == ':') 232 { 233 235 if (++ichr < argv[optind].length()) 236 optarg = argv[optind++].substring(ichr); 237 else if (++optind >= argv.length) 238 { 239 if (opterr) 240 System.err.println("+++ Option " + c + " requires an argument"); 241 242 ichr = 0; 243 optopt = c; 244 return '?'; 245 } 246 else 247 optarg = argv[optind++]; 248 249 ichr = 0; 250 } 251 else 252 { 253 255 if (ichr + 1 >= argv[optind].length()) 256 { 257 ++optind; 258 ichr = 0; 259 } 260 261 optarg = null; 262 } 263 264 return c; 265 } 266 267 269 270 271 public char getOptopt() 272 { 273 return optopt; 274 } 275 276 278 279 280 public String [] getParms() 281 { 282 String [] parms = new String [argv.length - optind]; 283 284 for (int i = 0; optind + i < argv.length; ++i) 285 parms[i] = argv[optind + i]; 286 287 return parms; 288 } 289 290 292 315 316 } 317 318 | Popular Tags |