home *** CD-ROM | disk | FTP | other *** search
- package jargs.gnu;
-
- import java.util.Hashtable;
- import java.util.Locale;
- import java.util.Vector;
-
- public class CmdLineParser {
- private String[] remainingArgs = null;
- private Hashtable options = new Hashtable(10);
- private Hashtable values = new Hashtable(10);
-
- public final Option addOption(Option opt) {
- if (opt.shortForm() != null) {
- this.options.put("-" + opt.shortForm(), opt);
- }
-
- this.options.put("--" + opt.longForm(), opt);
- return opt;
- }
-
- public final Option addStringOption(char shortForm, String longForm) {
- return this.addOption(new Option.StringOption(shortForm, longForm));
- }
-
- public final Option addStringOption(String longForm) {
- return this.addOption(new Option.StringOption(longForm));
- }
-
- public final Option addIntegerOption(char shortForm, String longForm) {
- return this.addOption(new Option.IntegerOption(shortForm, longForm));
- }
-
- public final Option addIntegerOption(String longForm) {
- return this.addOption(new Option.IntegerOption(longForm));
- }
-
- public final Option addLongOption(char shortForm, String longForm) {
- return this.addOption(new Option.LongOption(shortForm, longForm));
- }
-
- public final Option addLongOption(String longForm) {
- return this.addOption(new Option.LongOption(longForm));
- }
-
- public final Option addDoubleOption(char shortForm, String longForm) {
- return this.addOption(new Option.DoubleOption(shortForm, longForm));
- }
-
- public final Option addDoubleOption(String longForm) {
- return this.addOption(new Option.DoubleOption(longForm));
- }
-
- public final Option addBooleanOption(char shortForm, String longForm) {
- return this.addOption(new Option.BooleanOption(shortForm, longForm));
- }
-
- public final Option addBooleanOption(String longForm) {
- return this.addOption(new Option.BooleanOption(longForm));
- }
-
- public final Object getOptionValue(Option o) {
- return this.getOptionValue(o, (Object)null);
- }
-
- public final Object getOptionValue(Option o, Object def) {
- Vector v = (Vector)this.values.get(o.longForm());
- if (v == null) {
- return def;
- } else if (v.isEmpty()) {
- return null;
- } else {
- Object result = v.elementAt(0);
- v.removeElementAt(0);
- return result;
- }
- }
-
- public final Vector getOptionValues(Option option) {
- Vector result = new Vector();
-
- while(true) {
- Object o = this.getOptionValue(option, (Object)null);
- if (o == null) {
- return result;
- }
-
- result.addElement(o);
- }
- }
-
- public final String[] getRemainingArgs() {
- return this.remainingArgs;
- }
-
- public final void parse(String[] argv) throws IllegalOptionValueException, UnknownOptionException {
- this.parse(argv, Locale.getDefault());
- }
-
- public final void parse(String[] argv, Locale locale) throws IllegalOptionValueException, UnknownOptionException {
- Vector otherArgs = new Vector();
- int position = 0;
- this.values = new Hashtable(10);
-
- while(position < argv.length) {
- String curArg = argv[position];
- if (curArg.startsWith("-")) {
- if (curArg.equals("--")) {
- ++position;
- break;
- }
-
- String valueArg = null;
- if (curArg.startsWith("--")) {
- int equalsPos = curArg.indexOf("=");
- if (equalsPos != -1) {
- valueArg = curArg.substring(equalsPos + 1);
- curArg = curArg.substring(0, equalsPos);
- }
- } else if (curArg.length() > 2) {
- for(int i = 1; i < curArg.length(); ++i) {
- Option opt = (Option)this.options.get("-" + curArg.charAt(i));
- if (opt == null) {
- throw new UnknownSuboptionException(curArg, curArg.charAt(i));
- }
-
- if (opt.wantsValue()) {
- throw new NotFlagException(curArg, curArg.charAt(i));
- }
-
- this.addValue(opt, opt.getValue((String)null, locale));
- }
-
- ++position;
- continue;
- }
-
- Option opt = (Option)this.options.get(curArg);
- if (opt == null) {
- throw new UnknownOptionException(curArg);
- }
-
- Object value = null;
- if (opt.wantsValue()) {
- if (valueArg == null) {
- ++position;
- if (position < argv.length) {
- valueArg = argv[position];
- }
- }
-
- value = opt.getValue(valueArg, locale);
- } else {
- value = opt.getValue((String)null, locale);
- }
-
- this.addValue(opt, value);
- ++position;
- } else {
- otherArgs.addElement(curArg);
- ++position;
- }
- }
-
- while(position < argv.length) {
- otherArgs.addElement(argv[position]);
- ++position;
- }
-
- this.remainingArgs = new String[otherArgs.size()];
- otherArgs.copyInto(this.remainingArgs);
- }
-
- private void addValue(Option opt, Object value) {
- String lf = opt.longForm();
- Vector v = (Vector)this.values.get(lf);
- if (v == null) {
- v = new Vector();
- this.values.put(lf, v);
- }
-
- v.addElement(value);
- }
- }
-