01: package org.incava.jagol;
02:
03: import java.io.*;
04: import java.util.*;
05:
06: /**
07: * Base class of all options, except for booleans.
08: */
09: public abstract class NonBooleanOption extends Option {
10: public NonBooleanOption(String longName, String description) {
11: super (longName, description);
12: }
13:
14: /**
15: * Sets from a list of command-line arguments. Returns whether this option
16: * could be set from the current head of the list.
17: */
18: public boolean set(String arg, List args) throws OptionException {
19: // String arg = (String)args.get(0);
20:
21: tr.Ace.log("considering: " + arg);
22:
23: if (arg.equals("--" + longName)) {
24: tr.Ace.log("matched long name");
25:
26: // args.remove(0);
27: if (args.size() == 0) {
28: throw new InvalidTypeException(longName
29: + " expects following " + getType()
30: + " argument");
31: } else {
32: String value = (String) args.remove(0);
33: setValue(value);
34: }
35: } else if (arg.startsWith("--" + longName + "=")) {
36: tr.Ace.log("matched long name + equals");
37:
38: // args.remove(0);
39: int pos = ("--" + longName + "=").length();
40: tr.Ace.log("position: " + pos);
41: if (pos >= arg.length()) {
42: throw new InvalidTypeException(longName
43: + " expects argument of type " + getType());
44: } else {
45: String value = arg.substring(pos);
46: setValue(value);
47: }
48: } else if (shortName != 0 && arg.equals("-" + shortName)) {
49: tr.Ace.log("matched short name");
50:
51: // args.remove(0);
52: if (args.size() == 0) {
53: throw new InvalidTypeException(shortName
54: + " expects following " + getType()
55: + " argument");
56: } else {
57: String value = (String) args.remove(0);
58: setValue(value);
59: }
60: } else {
61: tr.Ace.log("not a match");
62: return false;
63: }
64: tr.Ace.log("matched");
65: return true;
66: }
67:
68: /**
69: * Returns the option type.
70: */
71: protected abstract String getType();
72:
73: }
|