01: package org.incava.jagol;
02:
03: import java.io.*;
04: import java.util.*;
05:
06: /**
07: * Represents an option that is an boolean.
08: */
09: public class BooleanOption extends Option {
10: private Boolean value;
11:
12: public BooleanOption(String longName, String description) {
13: this (longName, description, null);
14: }
15:
16: public BooleanOption(String longName, String description,
17: Boolean value) {
18: super (longName, description);
19: this .value = value;
20: }
21:
22: /**
23: * Returns the value. This is null if it has not been set.
24: */
25: public Boolean getValue() {
26: return value;
27: }
28:
29: /**
30: * Sets the value.
31: */
32: public void setValue(Boolean value) {
33: this .value = value;
34: }
35:
36: /**
37: * Sets the value from the string, for a boolean type.
38: */
39: public void setValue(String value) throws InvalidTypeException {
40: tr.Ace.log("value: '" + value + "'");
41: String lcvalue = value.toLowerCase();
42: if (lcvalue.equals("yes") || lcvalue.equals("true")) {
43: setValue(Boolean.TRUE);
44: } else if (lcvalue.equals("no") || lcvalue.equals("false")) {
45: setValue(Boolean.FALSE);
46: } else {
47: throw new InvalidTypeException(
48: longName
49: + " expects boolean argument (yes/no/true/false), not '"
50: + value + "'");
51: }
52: }
53:
54: /**
55: * Sets from a list of command-line arguments. Returns whether this option
56: * could be set from the current head of the list.
57: */
58: public boolean set(String arg, List args) throws OptionException {
59: tr.Ace.log("arg: " + arg + "; args: " + args);
60:
61: if (arg.equals("--" + longName)) {
62: // args.remove(0);
63: setValue(Boolean.TRUE);
64: } else if (arg.equals("--no-" + longName)
65: || arg.equals("--no" + longName)) {
66: // args.remove(0);
67: setValue(Boolean.FALSE);
68: } else if (shortName != 0 && arg.equals("-" + shortName)) {
69: // args.remove(0);
70: setValue(Boolean.TRUE);
71: } else {
72: return false;
73: }
74: return true;
75: }
76:
77: public String toString() {
78: return value == null ? "" : value.toString();
79: }
80:
81: }
|