KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.incava.jagol;
2
3 import java.io.*;
4 import java.util.*;
5
6
7 /**
8  * Base class of all options, except for booleans.
9  */

10 public abstract class NonBooleanOption extends Option
11 {
12     public NonBooleanOption(String JavaDoc longName, String JavaDoc description)
13     {
14         super(longName, description);
15     }
16
17     /**
18      * Sets from a list of command-line arguments. Returns whether this option
19      * could be set from the current head of the list.
20      */

21     public boolean set(String JavaDoc arg, List args) throws OptionException
22     {
23         // String arg = (String)args.get(0);
24

25         tr.Ace.log("considering: " + arg);
26         
27         if (arg.equals("--" + longName)) {
28             tr.Ace.log("matched long name");
29
30             // args.remove(0);
31
if (args.size() == 0) {
32                 throw new InvalidTypeException(longName + " expects following " + getType() + " argument");
33             }
34             else {
35                 String JavaDoc value = (String JavaDoc)args.remove(0);
36                 setValue(value);
37             }
38         }
39         else if (arg.startsWith("--" + longName + "=")) {
40             tr.Ace.log("matched long name + equals");
41
42             // args.remove(0);
43
int pos = ("--" + longName + "=").length();
44             tr.Ace.log("position: " + pos);
45             if (pos >= arg.length()) {
46                 throw new InvalidTypeException(longName + " expects argument of type " + getType());
47             }
48             else {
49                 String JavaDoc value = arg.substring(pos);
50                 setValue(value);
51             }
52         }
53         else if (shortName != 0 && arg.equals("-" + shortName)) {
54             tr.Ace.log("matched short name");
55
56             // args.remove(0);
57
if (args.size() == 0) {
58                 throw new InvalidTypeException(shortName + " expects following " + getType() + " argument");
59             }
60             else {
61                 String JavaDoc value = (String JavaDoc)args.remove(0);
62                 setValue(value);
63             }
64         }
65         else {
66             tr.Ace.log("not a match");
67             return false;
68         }
69         tr.Ace.log("matched");
70         return true;
71     }
72
73     /**
74      * Returns the option type.
75      */

76     protected abstract String JavaDoc getType();
77
78 }
79
Popular Tags