KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > MainArgs


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37
38 package net.sourceforge.cruisecontrol.util;
39
40 /**
41  * Methods to perform operations on <code>main()</code> arguments.
42  */

43 public final class MainArgs {
44     private MainArgs() { }
45
46     public static final int NOT_FOUND = -1;
47
48     public static int parseInt(String JavaDoc[] args, String JavaDoc argName, int defaultIfNoParam, int defaultIfNoValue) {
49         String JavaDoc intString = parseArgument(args,
50                                          argName,
51                                          Integer.toString(defaultIfNoParam),
52                                          Integer.toString(defaultIfNoValue));
53         try {
54             return Integer.parseInt(intString);
55         } catch (NumberFormatException JavaDoc e) {
56             throw new IllegalArgumentException JavaDoc(
57                 "-" + argName + " parameter, specified as '" + intString + "', requires integer argument");
58         }
59     }
60
61     /**
62      * Searches the array of args for the value corresponding to a particular
63      * argument name. This method assumes that the argName doesn't include
64      * a "-", but adds one while looking through the array. For example, if a
65      * user is supposed to type "-port", the appropriate argName to supply to
66      * this method is just "port".
67      *
68      * This method also allows the specification
69      * of a default argument value, in case one was not specified.
70      *
71      * @param args Application arguments like those specified to the standard
72      * Java main function.
73      * @param argName Name of the argument, without any preceeding "-",
74      * i.e. "port" not "-port".
75      * @param defaultIfNoParam A default argument value,
76      * in case the parameter argName was not specified
77      * @param defaultIfNoValue A default argument value,
78      * in case the parameter argName was specified without a value
79      * @return The argument value found, or the default if none was found.
80      */

81     public static String JavaDoc parseArgument(String JavaDoc[] args, String JavaDoc argName,
82                                        String JavaDoc defaultIfNoParam, String JavaDoc defaultIfNoValue) {
83         int argIndex = findIndex(args, argName);
84         if (argIndex == NOT_FOUND) {
85             return defaultIfNoParam;
86         }
87         // check to see if the user supplied a value for the parameter;
88
// if not, return the supplied default
89
if (argIndex == args.length - 1 // last arg
90
|| args[argIndex + 1].charAt(0) == '-' // start of new param
91
) {
92             return defaultIfNoValue;
93         }
94         return args[argIndex + 1];
95     }
96
97     public static int findIndex(String JavaDoc[] args, String JavaDoc argName) {
98
99         String JavaDoc searchString = "-" + argName;
100         for (int i = 0; i < args.length; i++) {
101             if (args[i].equals(searchString)) {
102                 return i;
103             }
104         }
105         return NOT_FOUND;
106     }
107
108     public static boolean argumentPresent(String JavaDoc[] args, String JavaDoc argName) {
109         return findIndex(args, argName) != NOT_FOUND;
110     }
111 }
112
Popular Tags