Source code: javatools/util/ArgumentProcessor.java
1 /*
2 * ArgumentProcessor.java
3 *
4 * Created on 1 gennaio 2002, 18.41
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.util;
26
27 /**
28 *
29 * @author Antonio Petrelli
30 * @version 0.0.1
31 */
32
33 import java.lang.*;
34
35 /** It's a class useful to process arguments passed via command line.
36 * @author Antonio Petrelli
37 * @version 0.0.1
38 */
39 public class ArgumentProcessor {
40
41 /** Creates new ArgumentProcessor */
42 public ArgumentProcessor() {
43 }
44
45 /** Sets the number of flags (parameters with with TRUE-FALSE values, used alone in command lines).
46 * @param numArgs Number of flags used.
47 * @throws IndexOutOfBoundsException If <CODE>numArgs</CODE> is less than 1.
48 */
49 public void setNumFlags(int numArgs) throws IndexOutOfBoundsException {
50 if (numArgs > 0) {
51 flagNames = new String [numArgs];
52 flagValues = new boolean [numArgs];
53 flagHelpText = new String [numArgs];
54 }
55 else
56 throw new IndexOutOfBoundsException();
57 }
58
59 /** Returns the number of flags.
60 * @return The number of flags used.
61 */
62 public int getNumFlags() {
63 return flagNames.length;
64 }
65
66 /** Sets the number of arguments (parameters with different values, used with to strings in command lines).
67 * @param numArgs Number of arguments used.
68 * @throws IndexOutOfBoundsException If <CODE>numArgs</CODE> is less than 1.
69 */
70 public void setNumArguments(int numArgs) throws IndexOutOfBoundsException {
71 if (numArgs > 0) {
72 argumentNames = new String [numArgs];
73 argumentValues = new String [numArgs];
74 argumentHelpText = new String [numArgs];
75 }
76 else
77 throw new IndexOutOfBoundsException();
78 }
79
80 /** Returns the number of arguments.
81 * @return The number of arguments used.
82 */
83 public int getNumArguments() {
84 return argumentNames.length;
85 }
86
87 /** Sets the name of a flag.
88 * @param numFlag The number of the flag. It has to be between <CODE>0</CODE> and <CODE>getNumFlags()-1</CODE>.
89 * @param flagName The name of the flag.
90 * @throws IndexOutOfBoundsException If <CODE>numFlag</CODE> is not valid.
91 */
92 public void setFlagName(int numFlag, String flagName) throws IndexOutOfBoundsException {
93 if (numFlag >= 0 && numFlag < flagNames.length)
94 flagNames[numFlag] = flagName;
95 else
96 throw new IndexOutOfBoundsException();
97 }
98
99 /** Returns the name of the flag.
100 * @param numFlag The position of the flag. It has to be between <CODE>0</CODE> and <CODE>getNumFlags()-1</CODE>.
101 * @return The name of the flag.
102 * @throws IndexOutOfBoundsException if <CODE>numFlag</CODE> is not valid.
103 */
104 public String getFlagName(int numFlag) throws IndexOutOfBoundsException {
105 if (numFlag >= 0 && numFlag < flagNames.length)
106 return flagNames[numFlag];
107 else
108 throw new IndexOutOfBoundsException();
109 }
110
111 /** Returns the position of the flag.
112 * @param flagName The name of the interested flag.
113 * @return The position of the flag, if existing. It has to be between <CODE>0</CODE> and <CODE>getNumFlags()-1</CODE>.
114 */
115 public int getFlagPos(String flagName) {
116 int i, flagLength, pos;
117
118 flagLength = flagNames.length;
119 i = 0;
120 pos = -1;
121 while (i < flagLength) {
122 if (flagNames[i].equals(flagName)) {
123 pos = i;
124 i = flagLength;
125 }
126 else
127 i++;
128 }
129 return pos;
130 }
131
132 /** Sets the value of a flag whose name is specified.
133 * @param flagName The name of the flag.
134 * @param flagValue The value of the flag.
135 */
136 public void setFlagValue(String flagName, boolean flagValue) {
137 int pos;
138
139 pos = getFlagPos(flagName);
140 if (pos >= 0) {
141 try {
142 setFlagValue(pos, flagValue);
143 }
144 catch (IndexOutOfBoundsException e) {
145 System.out.println("Not fatal: flag not correctly defined");
146 }
147 }
148 }
149
150 /** Sets the value of a flag whose position is specified.
151 * @param flagPos Position of the flag. It has to be between <CODE>0</CODE> and <CODE>getNumFlags()-1</CODE>.
152 * @param flagValue The value of the flag.
153 * @throws IndexOutOfBoundsException If <CODE>flagPos</CODE> is not valid.
154 */
155 public void setFlagValue(int flagPos, boolean flagValue) throws IndexOutOfBoundsException {
156 if (flagPos >= 0 && flagPos <= flagValues.length)
157 flagValues[flagPos] = flagValue;
158 else
159 throw new IndexOutOfBoundsException();
160 }
161
162 /** Returns the value of a flag whose name is specified.
163 * @param flagName The name of the flag.
164 * @return The value of the flag, if existing. False, otherwise.
165 */
166 public boolean getFlagValue(String flagName) {
167 int pos;
168
169 pos = getFlagPos(flagName);
170 if (pos >= 0) {
171 try {
172 return getFlagValue(pos);
173 }
174 catch (IndexOutOfBoundsException e) {
175 System.out.println("Not fatal: flag not correctly defined");
176 }
177 }
178 return false;
179 }
180
181 /** The value of a flag whose position is specified.
182 * @param flagPos The position of the flag. It has to be between <CODE>0</CODE> and <CODE>getNumFlags()-1</CODE>.
183 * @return The value of the flag, if existing.
184 * @throws IndexOutOfBoundsException If <CODE>flagPos</CODE> is not valid.
185 */
186 public boolean getFlagValue(int flagPos) throws IndexOutOfBoundsException {
187 if (flagPos >= 0 && flagPos <= flagValues.length)
188 return flagValues[flagPos];
189 else
190 throw new IndexOutOfBoundsException();
191 }
192
193 /** Sets the name of an argument.
194 * @param argPos The position of the argument. It has to be between <CODE>0</CODE> and <CODE>getNumArguments()-1</CODE>.
195 * @param argName The name of the argument.
196 * @throws IndexOutOfBoundsException if <CODE>argPos</CODE> is not valid.
197 */
198 public void setArgumentName(int argPos, String argName) throws IndexOutOfBoundsException {
199 if (argPos >= 0 && argPos < argumentNames.length)
200 argumentNames[argPos] = argName;
201 else
202 throw new IndexOutOfBoundsException();
203 }
204
205 /** Returns the name of an argument.
206 * @param argPos The position of the argument. It has to be between <CODE>0</CODE> and <CODE>getNumArguments()-1</CODE>.
207 * @return The name of the argument.
208 * @throws IndexOutOfBoundsException If <CODE>argPos</CODE> is not valid.
209 */
210 public String getArgumentName(int argPos) throws IndexOutOfBoundsException {
211 if (argPos >= 0 && argPos < argumentNames.length)
212 return argumentNames[argPos];
213 else
214 throw new IndexOutOfBoundsException();
215 }
216
217 /** Returns the position of an argument.
218 * @param argName The name of the argument.
219 * @return The position of the argument, if existing.
220 */
221 public int getArgumentPos(String argName) {
222 int i, argLength, pos;
223
224 argLength = argumentNames.length;
225 i = 0;
226 pos = -1;
227 while (i < argLength) {
228 if (argumentNames[i].equals(argName)) {
229 pos = i;
230 i = argLength;
231 }
232 else
233 i++;
234 }
235 return pos;
236 }
237
238 /** Sets the value of an argument, whose name is specified.
239 * @param argName The name of the argument.
240 * @param argValue The value of the argument.
241 */
242 public void setArgumentValue(String argName, String argValue) {
243 int pos;
244
245 pos = getArgumentPos(argName);
246 if (pos >= 0) {
247 try {
248 setArgumentValue(pos, argValue);
249 }
250 catch (IndexOutOfBoundsException e) {
251 System.out.println("Not fatal: argument not correctly defined");
252 }
253 }
254 }
255
256 /** Sets the value of an argument whose position is specified.
257 * @param argPos Position of the argument. It has to be between <CODE>0</CODE> and <CODE>getNumArguments()-1</CODE>.
258 * @param argValue Value of the argument.
259 * @throws IndexOutOfBoundsException If <CODE>argPos</CODE> is not valid.
260 */
261 public void setArgumentValue(int argPos, String argValue) throws IndexOutOfBoundsException {
262 if (argPos >= 0 && argPos < argumentNames.length)
263 argumentValues[argPos] = argValue;
264 else
265 throw new IndexOutOfBoundsException();
266 }
267
268 /** Return the value of an argument whose name is specified.
269 * @param argName Name of the argument.
270 * @return Value of the argument.
271 */
272 public String getArgumentValue(String argName) {
273 int pos;
274
275 pos = getArgumentPos(argName);
276 if (pos >= 0) {
277 try {
278 return getArgumentValue(pos);
279 }
280 catch (IndexOutOfBoundsException e) {
281 System.out.println("Not fatal: argument not correctly defined");
282 }
283 }
284 return "";
285 }
286
287 /** Returns the value of an argument whose position is specified.
288 * @param argPos Position of the argument. It has to be between <CODE>0</CODE> and <CODE>getNumArguments()-1</CODE>.
289 * @return The value of the argument.
290 * @throws IndexOutOfBoundsException If <CODE>argPos</CODE> is not valid.
291 */
292 public String getArgumentValue(int argPos) throws IndexOutOfBoundsException {
293 if (argPos >= 0 && argPos < argumentNames.length)
294 return argumentValues[argPos];
295 else
296 throw new IndexOutOfBoundsException();
297 }
298
299 /** Turns all flags to FALSE and all values to empty strings
300 */
301 public void cleanValues () {
302 int i, lengthArray;
303
304 lengthArray = flagValues.length;
305 for (i=0; i<lengthArray; i++)
306 flagValues[i] = false;
307 lengthArray = argumentValues.length;
308 for (i=0; i<lengthArray; i++)
309 argumentValues[i] = "";
310 }
311
312 /** Process the command line parameters.
313 * @param args The parameters passed via command line.
314 */
315 public void processArguments(String[] args) {
316 int i, pos, argsLength;
317
318 cleanValues();
319 try {
320 argsLength = args.length;
321 for (i=0; i<argsLength; i++) {
322 pos = getFlagPos(args[i]);
323 if (pos >= 0)
324 setFlagValue(pos, true);
325 else {
326 pos = getArgumentPos(args[i]);
327 if (pos >= 0 && i < argsLength) {
328 i++;
329 setArgumentValue(pos, args[i]);
330 }
331 }
332 }
333 }
334 catch (IndexOutOfBoundsException e) {
335 System.out.println("Not fatal: arguments/flags not correctly defined");
336 }
337 }
338
339 /** Sets help text for a flag whose name is specified.
340 * @param flagName The name of the flag.
341 * @param helpText The help text.
342 */
343 public void setFlagHelpText(String flagName, String helpText) {
344 int pos;
345
346 pos = getFlagPos(flagName);
347 if (pos >= 0) {
348 try {
349 setFlagHelpText(pos, helpText);
350 }
351 catch (IndexOutOfBoundsException e) {
352 System.out.println("Not fatal: flag not correctly defined");
353 }
354 }
355 }
356
357 /** Sets help text for a flag whose position is specified.
358 * @param numFlag Position of the flag. It has to be between <CODE>0</CODE> and <CODE>getNumFlags()-1</CODE>.
359 * @param helpText The help text.
360 */
361 public void setFlagHelpText (int numFlag, String helpText) {
362 if (numFlag >= 0 && numFlag < flagNames.length)
363 flagHelpText[numFlag] = helpText;
364 else
365 throw new IndexOutOfBoundsException();
366 }
367
368 /** Returns help text of a flag whose name is specified.
369 * @param flagName The name of the flag.
370 * @return The help text.
371 */
372 public String getFlagHelpText (String flagName) {
373 int pos;
374
375 pos = getFlagPos(flagName);
376 if (pos >= 0) {
377 try {
378 return getFlagHelpText(pos);
379 }
380 catch (IndexOutOfBoundsException e) {
381 System.out.println("Not fatal: flag not correctly defined");
382 }
383 }
384 return "";
385 }
386
387 /** Returns help text of a flag whose position is specified.
388 * @param numFlag The position of the flag. It has to be between <CODE>0</CODE> and <CODE>getNumFlags()-1</CODE>.
389 * @return The help text.
390 */
391 public String getFlagHelpText (int numFlag) {
392 if (numFlag >= 0 && numFlag < flagNames.length)
393 return flagHelpText[numFlag];
394 else
395 throw new IndexOutOfBoundsException();
396 }
397
398 /** Sets help text for an argument whose name is specified.
399 * @param argName The name of the argument.
400 * @param helpText The help text. Use <CODE>"%P%"</CODE> inside this string to represent the text which will be replaced with a given variable parameter name.
401 */
402 public void setArgumentHelpText(String argName, String helpText) {
403 int pos;
404
405 pos = getArgumentPos(argName);
406 if (pos >= 0) {
407 try {
408 setArgumentHelpText(pos, helpText);
409 }
410 catch (IndexOutOfBoundsException e) {
411 System.out.println("Not fatal: argument not correctly defined");
412 }
413 }
414 }
415
416 /** Sets help text for an argument whose position is specified.
417 * @param argPos The position of the argument. It has to be between <CODE>0</CODE> and <CODE>getNumArguments()-1</CODE>.
418 * @param helpText The help text. Use <CODE>"%P%"</CODE> inside this string to represent the text which will be replaced with a given variable parameter name.
419 */
420 public void setArgumentHelpText (int argPos, String helpText) {
421 if (argPos >= 0 && argPos < argumentNames.length)
422 argumentHelpText[argPos] = helpText;
423 else
424 throw new IndexOutOfBoundsException();
425 }
426
427 /** Returns the help text of an argument whose name is specified.
428 * @param argName The name of the argument.
429 * @param paramName The variable parameter name replaced inside the help text instead of the substring <CODE>"%P%"</CODE>.
430 * @return The help text.
431 */
432 public String getArgumentHelpText (String argName, String paramName) {
433 int pos;
434
435 pos = getFlagPos(argName);
436 if (pos >= 0) {
437 try {
438 return getArgumentHelpText(pos, paramName);
439 }
440 catch (IndexOutOfBoundsException e) {
441 System.out.println("Not fatal: argument not correctly defined");
442 }
443 }
444 return "";
445 }
446
447 /** Returns the help text whose position is specified.
448 * @param argPos The position of the argument. It has to be between <CODE>0</CODE> and <CODE>getNumArguments()-1</CODE>.
449 * @param paramName The variable parameter name replaced inside the help text instead of the substring <CODE>"%P%"</CODE>.
450 * @return The help text.
451 */
452 public String getArgumentHelpText (int argPos, String paramName) {
453 if (argPos >= 0 && argPos < argumentNames.length)
454 return SubstituteVariable.substitute(argumentHelpText[argPos], "%P%", paramName);
455 else
456 throw new IndexOutOfBoundsException();
457 }
458
459
460 private String[] flagNames;
461 private boolean[] flagValues;
462 private String[] argumentValues;
463 private String[] argumentNames;
464 private String[] flagHelpText;
465 private String[] argumentHelpText;
466
467 private String replaceParams (String text, String param) {
468 int pos;
469 String tempString;
470
471 tempString = text;
472 do {
473 pos = tempString.indexOf("%P%");
474 if (pos >= 0)
475 tempString = tempString.substring(0, pos)+param+tempString.substring(pos+3);
476 } while (pos >= 0);
477 return tempString;
478 }
479 }