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

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