home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / ccs_util.jar / com / commerceone / util / cmdline / CmdLineApp$ArgumentParser.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-12-09  |  3.6 KB  |  103 lines

  1. package com.commerceone.util.cmdline;
  2.  
  3. import java.util.Hashtable;
  4. import java.util.Vector;
  5.  
  6. public final class CmdLineApp$ArgumentParser extends Hashtable {
  7.    private Vector vectRegisteredValues;
  8.    String[] remainingArgs;
  9.    // $FF: synthetic field
  10.    final CmdLineApp this$0;
  11.  
  12.    protected void registerOption(String opt, String succinctExplain, String verboseExplain, boolean isBoolean) {
  13.       if (!this.isRegistered(opt)) {
  14.          this.vectRegisteredValues.addElement(new CmdLineApp.RegisteredOption(this.this$0, opt, succinctExplain, verboseExplain, isBoolean, true));
  15.       }
  16.  
  17.    }
  18.  
  19.    protected void registerOption(String opt, String succinctExplain, String verboseExplain, boolean isBoolean, boolean shouldDisplay) {
  20.       if (!this.isRegistered(opt)) {
  21.          this.vectRegisteredValues.addElement(new CmdLineApp.RegisteredOption(this.this$0, opt, succinctExplain, verboseExplain, isBoolean, shouldDisplay));
  22.       }
  23.  
  24.    }
  25.  
  26.    public CmdLineApp$ArgumentParser(CmdLineApp this$0) {
  27.       (this.this$0 = this$0).getClass();
  28.       this.vectRegisteredValues = new Vector();
  29.    }
  30.  
  31.    public boolean isRegistered(String option) {
  32.       for(int i = 0; i < this.vectRegisteredValues.size(); ++i) {
  33.          Object object = this.vectRegisteredValues.elementAt(i);
  34.          if (object instanceof CmdLineApp.RegisteredOption) {
  35.             CmdLineApp.RegisteredOption foundOption = (CmdLineApp.RegisteredOption)object;
  36.             if (foundOption != null && foundOption.getOption().equals(option)) {
  37.                return true;
  38.             }
  39.          }
  40.       }
  41.  
  42.       return false;
  43.    }
  44.  
  45.    private int handleArg(String flag, String[] args, int currArgNum) {
  46.       StringBuffer val = new StringBuffer();
  47.       if (currArgNum >= args.length) {
  48.          ((Hashtable)this).put(flag, val.toString());
  49.          return currArgNum;
  50.       } else if (args[currArgNum].startsWith("-")) {
  51.          ((Hashtable)this).put(flag, val.toString());
  52.          return currArgNum;
  53.       } else if (this.isBoolean(flag)) {
  54.          ((Hashtable)this).put(flag, val.toString());
  55.          return currArgNum;
  56.       } else {
  57.          val.append(args[currArgNum]);
  58.          ++currArgNum;
  59.          ((Hashtable)this).put(flag, val.toString());
  60.          return currArgNum;
  61.       }
  62.    }
  63.  
  64.    public Vector getRegisteredValues() {
  65.       return this.vectRegisteredValues;
  66.    }
  67.  
  68.    public void init(String[] args) {
  69.       if (args != null) {
  70.          int tokenNum = 0;
  71.  
  72.          while(tokenNum < args.length) {
  73.             if (args[tokenNum].startsWith("-")) {
  74.                tokenNum = this.handleArg(args[tokenNum].substring(1), args, tokenNum + 1);
  75.             } else {
  76.                this.remainingArgs = new String[args.length - tokenNum];
  77.                System.arraycopy(args, tokenNum, this.remainingArgs, 0, args.length - tokenNum);
  78.                tokenNum = args.length;
  79.             }
  80.          }
  81.       }
  82.  
  83.    }
  84.  
  85.    public boolean isBoolean(String option) {
  86.       for(int i = 0; i < this.vectRegisteredValues.size(); ++i) {
  87.          Object object = this.vectRegisteredValues.elementAt(i);
  88.          if (object instanceof CmdLineApp.RegisteredOption) {
  89.             CmdLineApp.RegisteredOption foundOption = (CmdLineApp.RegisteredOption)object;
  90.             if (foundOption != null && foundOption.getOption().equals(option)) {
  91.                return foundOption.getIsBoolean();
  92.             }
  93.          }
  94.       }
  95.  
  96.       return false;
  97.    }
  98.  
  99.    public String[] getRemainingArgs() {
  100.       return this.remainingArgs;
  101.    }
  102. }
  103.