001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2005, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.util;
037:
038: /**
039: * Methods to perform operations on <code>main()</code> arguments.
040: */
041: public final class MainArgs {
042: private MainArgs() {
043: }
044:
045: public static final int NOT_FOUND = -1;
046:
047: public static int parseInt(String[] args, String argName,
048: int defaultIfNoParam, int defaultIfNoValue) {
049: String intString = parseArgument(args, argName, Integer
050: .toString(defaultIfNoParam), Integer
051: .toString(defaultIfNoValue));
052: try {
053: return Integer.parseInt(intString);
054: } catch (NumberFormatException e) {
055: throw new IllegalArgumentException("-" + argName
056: + " parameter, specified as '" + intString
057: + "', requires integer argument");
058: }
059: }
060:
061: /**
062: * Searches the array of args for the value corresponding to a particular
063: * argument name. This method assumes that the argName doesn't include
064: * a "-", but adds one while looking through the array. For example, if a
065: * user is supposed to type "-port", the appropriate argName to supply to
066: * this method is just "port".
067: *
068: * This method also allows the specification
069: * of a default argument value, in case one was not specified.
070: *
071: * @param args Application arguments like those specified to the standard
072: * Java main function.
073: * @param argName Name of the argument, without any preceeding "-",
074: * i.e. "port" not "-port".
075: * @param defaultIfNoParam A default argument value,
076: * in case the parameter argName was not specified
077: * @param defaultIfNoValue A default argument value,
078: * in case the parameter argName was specified without a value
079: * @return The argument value found, or the default if none was found.
080: */
081: public static String parseArgument(String[] args, String argName,
082: String defaultIfNoParam, String defaultIfNoValue) {
083: int argIndex = findIndex(args, argName);
084: if (argIndex == NOT_FOUND) {
085: return defaultIfNoParam;
086: }
087: // check to see if the user supplied a value for the parameter;
088: // if not, return the supplied default
089: if (argIndex == args.length - 1 // last arg
090: || args[argIndex + 1].charAt(0) == '-' // start of new param
091: ) {
092: return defaultIfNoValue;
093: }
094: return args[argIndex + 1];
095: }
096:
097: public static int findIndex(String[] args, String argName) {
098:
099: String 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[] args, String argName) {
109: return findIndex(args, argName) != NOT_FOUND;
110: }
111:
112: public static boolean parseBoolean(String[] args, String argName,
113: boolean defaultIfNoParam, boolean defaultIfNoValue) {
114: String booleanString = parseArgument(args, argName, Boolean
115: .toString(defaultIfNoParam), Boolean
116: .toString(defaultIfNoValue));
117: try {
118: return Boolean.valueOf(booleanString).booleanValue();
119: } catch (NumberFormatException e) {
120: throw new IllegalArgumentException("-" + argName
121: + " parameter, specified as '" + booleanString
122: + "', requires boolean argument");
123: }
124: }
125: }
|