Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Options.java

00001 package gov.nasa.arc.ase.util;
00002 
00003 import java.io.*;
00004 import java.lang.reflect.*;
00005 import java.util.*;
00006 
00007 public class Options {
00008   private Hashtable parsers;
00009   private Hashtable printers;
00010   private Class mainClass;
00011   private String defaultParam;
00012   private boolean multipleParam;
00013 
00014   public Options(Class m) {
00015     init(m, null, false);
00016   }  
00017   public Options(Class m, String p) {
00018     init(m, p, false);
00019   }  
00020   public Options(Class m, String p, boolean u) {
00021     init(m, p, u);
00022   }  
00023   public void addParser(String type, String name) {
00024     Class[] arguments = new Class[1];
00025     arguments[0] = String.class;
00026     try {
00027       parsers.put(type, getClass().getMethod(name, arguments));
00028     } catch(NoSuchMethodException e) {
00029       e.printStackTrace();
00030       System.exit(1);
00031     }
00032   }  
00033   public void addPrinter(String type, String name) {
00034     Class[] arguments = new Class[1];
00035     arguments[0] = Object.class;
00036     try {
00037       printers.put(type, getClass().getMethod(name, arguments));
00038     } catch(NoSuchMethodException e) {
00039       e.printStackTrace();
00040       System.exit(1);
00041     }
00042   }  
00043   public Class getMainClass() {
00044     return mainClass;
00045   }  
00046   private void init(Class m, String p, boolean u) {
00047     mainClass = m;
00048     defaultParam = p;
00049     multipleParam = u;
00050 
00051     parsers = new Hashtable();
00052     printers = new Hashtable();
00053     addParser("java.lang.String", "parseString");
00054     addParser("int", "parseInt");
00055   }  
00056   public String[] parse(String[] args) {
00057     boolean noDefault = defaultParam == null;
00058     boolean inParams = false;
00059     Vector params = new Vector();
00060 
00061     for(int i = 0; i < args.length; i++) {
00062       String arg = args[i];
00063       if(arg.startsWith("-") && !inParams) {
00064     boolean found = false;
00065     Field[] f= getClass().getFields();
00066 
00067     if(arg.equals("--"))
00068       inParams = found = true;
00069 
00070     for(int j = 0; j < f.length && !found; j++)
00071       if(Modifier.isPublic(f[i].getModifiers())) {
00072         String name = f[j].getName().replace('_', '-');
00073         String type = f[j].getType().getName();
00074 
00075         try {
00076           if(type.equals("boolean")) {
00077         if(arg.equals("-" + name)) {
00078           f[j].setBoolean(this, true);
00079           found = true;
00080         }
00081         if(arg.equals("-no-" + name)) {
00082           f[j].setBoolean(this, false);
00083           found = true;
00084         }
00085           } else if(arg.equals("-" + name)) {
00086         String value = null;
00087 
00088         found = true;
00089         i++;
00090         if(i != args.length)
00091           value = args[i];
00092         Method parser = (Method)parsers.get(type);
00093         if(parser == null) {
00094           Debug.println(Debug.ERROR, mainClass.getName() + ": invalid type -- " + type);
00095           System.exit(1);
00096         }
00097 
00098         Object[] arguments = new Object[1];
00099         arguments[0] = value;
00100         try {
00101           f[j].set(this, parser.invoke(this, arguments));
00102         } catch(InvocationTargetException e) {
00103           if(value == null && e.getTargetException() instanceof IllegalArgumentException) {
00104             Debug.println(Debug.ERROR, mainClass.getName() + ": option requires an argument -- " + arg);
00105             System.exit(1);
00106           }
00107           e.printStackTrace();
00108           System.exit(1);
00109         }
00110           }
00111         } catch(IllegalAccessException e) {
00112           e.printStackTrace();
00113           System.exit(1);
00114         }
00115       }
00116     if(!found) {
00117       Debug.println(Debug.ERROR, mainClass.getName() + ": invalid option -- " + arg);
00118       System.exit(1);
00119     }
00120       } else {
00121     if(noDefault) {
00122       Debug.println(Debug.ERROR, mainClass.getName() + ": invalid parameter -- " + arg);
00123       System.exit(1);
00124     } else {
00125       noDefault = !multipleParam;
00126       params.addElement(arg);
00127     }
00128       }
00129     }
00130     String[] stringParams = new String[params.size()];
00131     for(int i = 0; i < params.size(); i++) {
00132       stringParams[i] = (String)params.elementAt(i);
00133       //System.out.println(stringParams[i]);
00134     }
00135 
00136     return stringParams;
00137   }  
00138   public Object parseInt(String s) {
00139     if(s == null) throw new IllegalArgumentException();
00140     return new Integer(Integer.parseInt(s));
00141   }  
00142   public Object parseString(String s) {
00143     if(s == null) throw new IllegalArgumentException();
00144     return s;
00145   }  
00146   public void print() {
00147     Field[] f = getClass().getFields();
00148     int maxlen = 0;
00149     for(int i = 0; i < f.length; i++)
00150       if(Modifier.isPublic(f[i].getModifiers())) {
00151     int l = f[i].getName().length();
00152     if(maxlen < l)
00153       maxlen = l;
00154       }
00155 
00156     for(int i = 0; i < f.length; i++)
00157       if(Modifier.isPublic(f[i].getModifiers())) {
00158     String name = f[i].getName();
00159     while(name.length() != maxlen)
00160       name = name + " ";
00161     Debug.print(Debug.WARNING, name + ": ");
00162     try {
00163       Object o = f[i].get(this);
00164       String type = f[i].getType().getName();
00165       Method printer = (Method)printers.get(type);
00166       String s;
00167       if(o == null)
00168         s = "null";
00169       else
00170         s = o.toString();
00171 
00172       if(printer != null) {
00173         Object[] arguments = new Object[1];
00174         arguments[0] = o;
00175         try {
00176           s = (String)printer.invoke(this, arguments);
00177         } catch(InvocationTargetException e) {
00178         }
00179       }
00180       Debug.println(Debug.WARNING, s);
00181     } catch(IllegalAccessException e) {
00182       e.printStackTrace();
00183       System.exit(1);
00184     }
00185       }
00186   }  
00187   public void print(PrintStream out) {
00188     Field[] f = getClass().getFields();
00189     int maxlen = 0;
00190     for(int i = 0; i < f.length; i++)
00191       if(Modifier.isPublic(f[i].getModifiers())) {
00192     int l = f[i].getName().length();
00193     if(maxlen < l)
00194       maxlen = l;
00195       }
00196 
00197     for(int i = 0; i < f.length; i++)
00198       if(Modifier.isPublic(f[i].getModifiers())) {
00199     String name = f[i].getName();
00200     while(name.length() != maxlen)
00201       name = name + " ";
00202     out.print(name + ": ");
00203     try {
00204       Object o = f[i].get(this);
00205       String type = f[i].getType().getName();
00206       Method printer = (Method)printers.get(type);
00207       String s;
00208       if(o == null)
00209         s = "null";
00210       else
00211         s = o.toString();
00212 
00213       if(printer != null) {
00214         Object[] arguments = new Object[1];
00215         arguments[0] = o;
00216         try {
00217           s = (String)printer.invoke(this, arguments);
00218         } catch(InvocationTargetException e) {
00219         }
00220       }
00221       out.println(s);
00222     } catch(IllegalAccessException e) {
00223       e.printStackTrace();
00224       System.exit(1);
00225     }
00226       }
00227   }  
00228   public void usage() {
00229     Debug.println(Debug.ERROR, "usage: ");
00230     Debug.println(Debug.ERROR);
00231 
00232     Debug.print(Debug.ERROR, mainClass.getName() + " ");
00233     if(defaultParam != null) {
00234       Debug.print(Debug.ERROR, "<" + defaultParam + ">");
00235       if(multipleParam)
00236     Debug.print(Debug.ERROR, " ...");
00237     }
00238     Debug.println(Debug.ERROR);
00239 
00240     Field[] f = getClass().getFields();
00241     for(int i = 0; i < f.length; i++)
00242       if(Modifier.isPublic(f[i].getModifiers())) {
00243     String name = f[i].getName().replace('_', '-');
00244     String type = f[i].getType().getName();
00245 
00246     Debug.print(Debug.ERROR, "\t-" + name);
00247     if(type.equals("boolean"))
00248       Debug.print(Debug.ERROR, " | -no-" + name);
00249     else
00250       Debug.print(Debug.ERROR, " <" + name + ">");
00251     Debug.println(Debug.ERROR);
00252       }
00253     Debug.println(Debug.ERROR);
00254     Debug.println(Debug.ERROR,"The default values are:");
00255     Debug.println(Debug.ERROR,"-----------------------");
00256     print();
00257     System.exit(1);
00258   }  
00259 }

Generated at Thu Feb 7 06:50:58 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001