home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / plugins / researchdriver / res / process.jar / jargs / gnu / CmdLineParser.class (.txt) next >
Encoding:
Java Class File  |  2005-04-09  |  5.9 KB  |  184 lines

  1. package jargs.gnu;
  2.  
  3. import java.util.Hashtable;
  4. import java.util.Locale;
  5. import java.util.Vector;
  6.  
  7. public class CmdLineParser {
  8.    private String[] remainingArgs = null;
  9.    private Hashtable options = new Hashtable(10);
  10.    private Hashtable values = new Hashtable(10);
  11.  
  12.    public final Option addOption(Option opt) {
  13.       if (opt.shortForm() != null) {
  14.          this.options.put("-" + opt.shortForm(), opt);
  15.       }
  16.  
  17.       this.options.put("--" + opt.longForm(), opt);
  18.       return opt;
  19.    }
  20.  
  21.    public final Option addStringOption(char shortForm, String longForm) {
  22.       return this.addOption(new Option.StringOption(shortForm, longForm));
  23.    }
  24.  
  25.    public final Option addStringOption(String longForm) {
  26.       return this.addOption(new Option.StringOption(longForm));
  27.    }
  28.  
  29.    public final Option addIntegerOption(char shortForm, String longForm) {
  30.       return this.addOption(new Option.IntegerOption(shortForm, longForm));
  31.    }
  32.  
  33.    public final Option addIntegerOption(String longForm) {
  34.       return this.addOption(new Option.IntegerOption(longForm));
  35.    }
  36.  
  37.    public final Option addLongOption(char shortForm, String longForm) {
  38.       return this.addOption(new Option.LongOption(shortForm, longForm));
  39.    }
  40.  
  41.    public final Option addLongOption(String longForm) {
  42.       return this.addOption(new Option.LongOption(longForm));
  43.    }
  44.  
  45.    public final Option addDoubleOption(char shortForm, String longForm) {
  46.       return this.addOption(new Option.DoubleOption(shortForm, longForm));
  47.    }
  48.  
  49.    public final Option addDoubleOption(String longForm) {
  50.       return this.addOption(new Option.DoubleOption(longForm));
  51.    }
  52.  
  53.    public final Option addBooleanOption(char shortForm, String longForm) {
  54.       return this.addOption(new Option.BooleanOption(shortForm, longForm));
  55.    }
  56.  
  57.    public final Option addBooleanOption(String longForm) {
  58.       return this.addOption(new Option.BooleanOption(longForm));
  59.    }
  60.  
  61.    public final Object getOptionValue(Option o) {
  62.       return this.getOptionValue(o, (Object)null);
  63.    }
  64.  
  65.    public final Object getOptionValue(Option o, Object def) {
  66.       Vector v = (Vector)this.values.get(o.longForm());
  67.       if (v == null) {
  68.          return def;
  69.       } else if (v.isEmpty()) {
  70.          return null;
  71.       } else {
  72.          Object result = v.elementAt(0);
  73.          v.removeElementAt(0);
  74.          return result;
  75.       }
  76.    }
  77.  
  78.    public final Vector getOptionValues(Option option) {
  79.       Vector result = new Vector();
  80.  
  81.       while(true) {
  82.          Object o = this.getOptionValue(option, (Object)null);
  83.          if (o == null) {
  84.             return result;
  85.          }
  86.  
  87.          result.addElement(o);
  88.       }
  89.    }
  90.  
  91.    public final String[] getRemainingArgs() {
  92.       return this.remainingArgs;
  93.    }
  94.  
  95.    public final void parse(String[] argv) throws IllegalOptionValueException, UnknownOptionException {
  96.       this.parse(argv, Locale.getDefault());
  97.    }
  98.  
  99.    public final void parse(String[] argv, Locale locale) throws IllegalOptionValueException, UnknownOptionException {
  100.       Vector otherArgs = new Vector();
  101.       int position = 0;
  102.       this.values = new Hashtable(10);
  103.  
  104.       while(position < argv.length) {
  105.          String curArg = argv[position];
  106.          if (curArg.startsWith("-")) {
  107.             if (curArg.equals("--")) {
  108.                ++position;
  109.                break;
  110.             }
  111.  
  112.             String valueArg = null;
  113.             if (curArg.startsWith("--")) {
  114.                int equalsPos = curArg.indexOf("=");
  115.                if (equalsPos != -1) {
  116.                   valueArg = curArg.substring(equalsPos + 1);
  117.                   curArg = curArg.substring(0, equalsPos);
  118.                }
  119.             } else if (curArg.length() > 2) {
  120.                for(int i = 1; i < curArg.length(); ++i) {
  121.                   Option opt = (Option)this.options.get("-" + curArg.charAt(i));
  122.                   if (opt == null) {
  123.                      throw new UnknownSuboptionException(curArg, curArg.charAt(i));
  124.                   }
  125.  
  126.                   if (opt.wantsValue()) {
  127.                      throw new NotFlagException(curArg, curArg.charAt(i));
  128.                   }
  129.  
  130.                   this.addValue(opt, opt.getValue((String)null, locale));
  131.                }
  132.  
  133.                ++position;
  134.                continue;
  135.             }
  136.  
  137.             Option opt = (Option)this.options.get(curArg);
  138.             if (opt == null) {
  139.                throw new UnknownOptionException(curArg);
  140.             }
  141.  
  142.             Object value = null;
  143.             if (opt.wantsValue()) {
  144.                if (valueArg == null) {
  145.                   ++position;
  146.                   if (position < argv.length) {
  147.                      valueArg = argv[position];
  148.                   }
  149.                }
  150.  
  151.                value = opt.getValue(valueArg, locale);
  152.             } else {
  153.                value = opt.getValue((String)null, locale);
  154.             }
  155.  
  156.             this.addValue(opt, value);
  157.             ++position;
  158.          } else {
  159.             otherArgs.addElement(curArg);
  160.             ++position;
  161.          }
  162.       }
  163.  
  164.       while(position < argv.length) {
  165.          otherArgs.addElement(argv[position]);
  166.          ++position;
  167.       }
  168.  
  169.       this.remainingArgs = new String[otherArgs.size()];
  170.       otherArgs.copyInto(this.remainingArgs);
  171.    }
  172.  
  173.    private void addValue(Option opt, Object value) {
  174.       String lf = opt.longForm();
  175.       Vector v = (Vector)this.values.get(lf);
  176.       if (v == null) {
  177.          v = new Vector();
  178.          this.values.put(lf, v);
  179.       }
  180.  
  181.       v.addElement(value);
  182.    }
  183. }
  184.