KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > jagol > BooleanOption


1 package org.incava.jagol;
2
3 import java.io.*;
4 import java.util.*;
5
6
7 /**
8  * Represents an option that is an boolean.
9  */

10 public class BooleanOption extends Option
11 {
12     private Boolean JavaDoc value;
13     
14     public BooleanOption(String JavaDoc longName, String JavaDoc description)
15     {
16         this(longName, description, null);
17     }
18
19     public BooleanOption(String JavaDoc longName, String JavaDoc description, Boolean JavaDoc value)
20     {
21         super(longName, description);
22         this.value = value;
23     }
24
25     /**
26      * Returns the value. This is null if it has not been set.
27      */

28     public Boolean JavaDoc getValue()
29     {
30         return value;
31     }
32
33     /**
34      * Sets the value.
35      */

36     public void setValue(Boolean JavaDoc value)
37     {
38         this.value = value;
39     }
40
41     /**
42      * Sets the value from the string, for a boolean type.
43      */

44     public void setValue(String JavaDoc value) throws InvalidTypeException
45     {
46         tr.Ace.log("value: '" + value + "'");
47         String JavaDoc lcvalue = value.toLowerCase();
48         if (lcvalue.equals("yes") || lcvalue.equals("true")) {
49             setValue(Boolean.TRUE);
50         }
51         else if (lcvalue.equals("no") || lcvalue.equals("false")) {
52             setValue(Boolean.FALSE);
53         }
54         else {
55             throw new InvalidTypeException(longName + " expects boolean argument (yes/no/true/false), not '" + value + "'");
56         }
57     }
58
59     /**
60      * Sets from a list of command-line arguments. Returns whether this option
61      * could be set from the current head of the list.
62      */

63     public boolean set(String JavaDoc arg, List args) throws OptionException
64     {
65         tr.Ace.log("arg: " + arg + "; args: " + args);
66         
67         if (arg.equals("--" + longName)) {
68             // args.remove(0);
69
setValue(Boolean.TRUE);
70         }
71         else if (arg.equals("--no-" + longName) || arg.equals("--no" + longName)) {
72             // args.remove(0);
73
setValue(Boolean.FALSE);
74         }
75         else if (shortName != 0 && arg.equals("-" + shortName)) {
76             // args.remove(0);
77
setValue(Boolean.TRUE);
78         }
79         else {
80             return false;
81         }
82         return true;
83     }
84
85     public String JavaDoc toString()
86     {
87         return value == null ? "" : value.toString();
88     }
89
90 }
91
Popular Tags