KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > util > Getopts


1 package jimm.util;
2 import java.util.HashMap JavaDoc;
3
4 /**
5  * Getopts is similar to the UN*X getopt() system call. It parses an array of
6  * Strings (usually the command line), looking for specified option flags and
7  * values.
8  * <p>
9  * An instance of Getopts parses the whole args list at once, and stores the
10  * option flags and values that it finds.
11  *
12  * @author Jim Menard,
13  * <a HREF="mailto:jimm@io.com">jimm@io.com</a>
14  */

15 public class Getopts {
16 String JavaDoc[] argv;
17 HashMap JavaDoc options = new HashMap JavaDoc();
18 boolean errorFlag = false;
19
20 /**
21  * This constructor takes a list of legal options and a list of (usually
22  * command line) arguments. Each option in optionListString may be followed
23  * by a ':' to signify that option takes an argument.
24  *
25  * @param optionListString option chars with optional ':' specifying arg.
26  * For example, "ab:c" specifies three options, a, b, and c. Option b takes
27  * a (required) argument.
28  * @param args array of command line arguments
29  */

30 public Getopts(String JavaDoc optionListString, String JavaDoc[] args) {
31     String JavaDoc optChoices = optionListString;
32
33     for (int index = 0; index < args.length; ++index) {
34     String JavaDoc arg = args[index];
35     if (arg.startsWith("-")) {
36         char optionChar = arg.charAt(1);
37         int optionLoc = optChoices.indexOf(optionChar);
38         if (optionLoc == -1)
39         errorFlag = true;
40         else {
41         // Look for argument, if any
42
boolean hasArgument =
43             optChoices.length() > optionLoc + 1 &&
44             optChoices.charAt(optionLoc + 1) == ':';
45         if (hasArgument) {
46             String JavaDoc optarg = arg.substring(2);
47             if (optarg.equals("")) {
48             ++index;
49             try {
50                 optarg = args[index];
51             }
52             catch (Exception JavaDoc e) { // Catch ArrayOutOfBounds
53
optarg = "";
54                 errorFlag = true;
55             }
56             }
57             options.put(new Character JavaDoc(optionChar), optarg);
58         }
59         else {
60             // No arg, store empty string
61
options.put(new Character JavaDoc(optionChar), "");
62         }
63         }
64     }
65     else { // End of options. Store rest of args
66
argv = new String JavaDoc[args.length - index];
67         int offset = index;
68         while (index < args.length) {
69         argv[index - offset] = args[index];
70         ++index;
71         }
72         break;
73     }
74     }
75 }
76
77 /**
78  *
79  * Return true if there was an error while parsing the command line.
80  */

81 public boolean error() {
82     return errorFlag;
83 }
84
85 /**
86  * Returns existence of an option.
87  *
88  * @return true of option 'c' exists, else return false.
89  * @param c any character
90  */

91 public boolean hasOption(char c) {
92     if (options == null)
93     return false;
94     return options.containsKey(new Character JavaDoc(c));
95 }
96
97 /**
98  * Return an option or, if missing, the empty string.
99  *
100  * @return option string, or "" if error or option has no argument
101  * @param c the option whose value is returned
102  */

103 public String JavaDoc option(char c) {
104     return option(c, "");
105 }
106
107 /**
108  * Return an option or, if missing, a default value.
109  *
110  * @return option string, or defaultValue if error or option has no argument
111  * @param c the option whose value is returned
112  * @param defaultValue the value to return if there is no such option
113  */

114 public String JavaDoc option(char c, String JavaDoc defaultValue) {
115     if (options == null)
116     return defaultValue;
117
118     String JavaDoc s;
119     try {
120     Object JavaDoc o = options.get(new Character JavaDoc(c));
121     if (o == null || !(o instanceof String JavaDoc))
122         s = defaultValue;
123     else
124         s = (String JavaDoc)o;
125     }
126     catch (Exception JavaDoc e) {
127     s = defaultValue;
128     }
129     return s;
130 }
131
132 /**
133  * Return the remaining command-line arguments.
134  *
135  * @return an array of Strings
136  * @see #argc
137  * @see #argv
138  */

139 public String JavaDoc[] args() {
140     return argv;
141 }
142
143 /**
144  * Return the number of non-option args.
145  */

146 public int argc() {
147     if (argv == null)
148     return 0;
149     return argv.length;
150 }
151 /**
152  * Return a command line argument or "" if <var>argv</var> is
153  * <code>null</code>. Index starts at 0.
154  *
155  * @param index which argument to return
156  * @return the index'th arg or "" if <var>argv</var> is <code>null</code>
157  */

158 public String JavaDoc argv(int index) {
159     if (argv == null)
160     return "";
161
162     return argv[index];
163 }
164 }
165
Popular Tags