1 /*
2 * (C) 2002 David Carr david@carr.name
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package net.sourceforge.mflow;
20
21 import java.util.ArrayList;
22 import java.util.Hashtable;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 /***
28 * A utility class to handle command-line arguments.
29 *
30 * @author <a href="mailto:david@carr.name">David Carr</a>
31 * @version $Revision: 1.3 $
32 */
33 class ArgumentMaster {
34 /***
35 * Class field used for when a value comes before any parameter
36 */
37 protected static final String DEFAULT_KEY = "_%$DEFAULT$%_";
38
39 /***
40 * Class field used for when a parameter has no values
41 */
42 protected static final Boolean DEFAULT_VALUE = new Boolean(true);
43
44 /***
45 * Method to get the default key for comparison
46 *
47 * @return the default key, a String
48 */
49 static String getDefaultKey() {
50 return DEFAULT_KEY;
51 }
52
53 /***
54 * Private storage for the passed arguments, parsed into a Hashtable
55 */
56 private Map args;
57
58 /***
59 * Whether or not to treat parameters as case-sensitive
60 */
61 private boolean caseSensitive;
62
63 /***
64 * Default Constructor. Not very useful, as it means there are no arguments
65 * whatsoever. Parameters are treated as case-sensitive.
66 */
67 public ArgumentMaster() {
68 this(true);
69 }
70
71 /***
72 * Constructor. Not very useful, as it means there are no arguments
73 * whatsoever.
74 *
75 * @param aCaseSensitive whether to treat parameters as case-sensitive
76 */
77 public ArgumentMaster(final boolean aCaseSensitive) {
78 this.caseSensitive = aCaseSensitive;
79 this.args = new Hashtable();
80 }
81
82 /***
83 * Constructor that takes an array of Strings, as passed through the
84 * command-line. Parameters are treated as case-sensitive.
85 *
86 * @param aArgs the command-line arguments to be parsed
87 */
88 public ArgumentMaster(final String[] aArgs) {
89 this(aArgs, true);
90 }
91
92 /***
93 * Constructor that takes an array of Strings, as passed through the
94 * command-line.
95 *
96 * @param aArgs the command-line arguments to be parsed
97 * @param aCaseSensitive whether to treat parameters as case-sensitive
98 */
99 public ArgumentMaster(final String[] aArgs, final boolean aCaseSensitive) {
100 this.caseSensitive = aCaseSensitive;
101 this.args = new Hashtable();
102 if (aArgs == null) {
103 return;
104 }
105 String curParam = null;
106 List curVals = null;
107 for (int i = 0; i < aArgs.length; i++) {
108 String arg = aArgs[i];
109 if (arg != null) {
110 if (arg.startsWith("-") || arg.startsWith("/")) {
111 if (curParam != null) {
112 List oldVals = (List) this.args.get(curParam);
113 if (oldVals == null) {
114 if (curVals.size() > 0) {
115 this.args.put(curParam, curVals);
116 } else {
117 this.args.put(curParam, DEFAULT_VALUE);
118 }
119 } else {
120 oldVals.addAll(curVals);
121 }
122 }
123 if (this.caseSensitive) {
124 curParam = arg.substring(1);
125 } else {
126 curParam = arg.substring(1).toUpperCase();
127 }
128 curVals = new ArrayList();
129 } else if (arg.length() > 0) {
130 if (curParam == null) {
131 curParam = DEFAULT_KEY;
132 curVals = new ArrayList();
133 }
134 curVals.add(arg);
135 }
136 }
137 }
138 if (curParam != null) {
139 List oldVals = (ArrayList) this.args.get(curParam);
140 if (oldVals == null) {
141 if (curVals.size() > 0) {
142 this.args.put(curParam, curVals);
143 } else {
144 this.args.put(curParam, DEFAULT_VALUE);
145 }
146 } else {
147 oldVals.addAll(curVals);
148 }
149 }
150 }
151
152 /***
153 * Tests if the specified parameter was in the arguments.
154 *
155 * @param param the parameter to test for
156 * @return true if it was present, false if it wasn't
157 */
158 public final boolean containsParameter(final String param) {
159 if (this.caseSensitive) {
160 return this.args.containsKey(param);
161 } else {
162 return this.args.containsKey(param.toUpperCase());
163 }
164 }
165
166 /***
167 * Gets any values specified without a parameter.
168 *
169 * @return a <code>List</code> containing the values, or null if there aren't
170 * any
171 */
172 public final List getDefaultValues() {
173 return getValues(DEFAULT_KEY);
174 }
175
176 /***
177 * Gets an <code>Iterator</code> of the parameters in the command-line
178 *
179 * @return an <code>Iterator</code> of the parameters; No values are
180 * included.
181 */
182 public final Iterator getParams() {
183 return this.args.keySet().iterator();
184 }
185
186 /***
187 * Gets the values of the specified parameter.
188 *
189 * @param param the parameter to get values for.
190 * @return a <code>List</code> containing the values, or null if there aren't
191 * any
192 */
193 public final List getValues(final String param) {
194 Object o;
195 if (this.caseSensitive) {
196 o = this.args.get(param);
197 } else {
198 o = this.args.get(param.toUpperCase());
199 }
200 if (o == null) {
201 return null; //if the parameter wasn't in the hashtable, no values
202 }
203 if (o instanceof List) {
204 return (List) o; //got the values
205 }
206 return null; //otherwise, it was a parameter without values
207 }
208
209 /***
210 * Accessor for whether the class is in case-sensitive mode
211 *
212 * @return the current mode
213 */
214 public final boolean isCaseSensitive() {
215 return this.caseSensitive;
216 }
217
218 /***
219 * Returns the number of parameters.
220 *
221 * @return the number of parameters
222 */
223 public final int size() {
224 return this.args.size();
225 }
226
227 /***
228 * Returns a string representation of the command-line arguments.
229 *
230 * @return the command-line arguments, replacing all initial '/' in parameters
231 * with '-' and treating for case
232 */
233 public final String toString() {
234 StringBuffer sb = new StringBuffer();
235 Iterator keys = this.args.keySet().iterator();
236 String curKey;
237 boolean isFirst = true;
238 int i;
239 List vals;
240 while (keys.hasNext()) {
241 curKey = (String) keys.next();
242 if (curKey.equals(DEFAULT_KEY)) {
243 if (isFirst) {
244 isFirst = false;
245 } else {
246 sb.append(" ");
247 }
248 vals = getValues(curKey);
249 if (vals != null) {
250 for (i = 0; i < vals.size(); i++) {
251 sb.append(" ");
252 sb.append(vals.get(i));
253 }
254 }
255 } else {
256 if (isFirst) {
257 isFirst = false;
258 } else {
259 sb.append(" ");
260 }
261 sb.append("-");
262 sb.append(curKey);
263
264 vals = getValues(curKey);
265 if (vals != null) {
266 for (i = 0; i < vals.size(); i++) {
267 sb.append(" ");
268 sb.append(vals.get(i));
269 }
270 }
271 }
272 }
273 return sb.toString();
274 }
275 }
This page was automatically generated by Maven