001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.options; 018 019import java.util.ArrayList; 020 021import org.conqat.lib.commons.string.StringUtils; 022 023/** 024 * A very simple class for parsing command line parameters. 025 * <p> 026 * A typical command line looks like this: 027 * <p> 028 * <code>-dir src -count occurrences TEST -dde</code> 029 * <p> 030 * 031 * In this example the minus symbol ('-') is the <i>parameter prefix </i>, 032 * <code>dir</code>,<code>count</code> and <code>dde</code> are <i>parameters 033 * </i>. Whereas <code>dir</code> has a single <i>value </i> <code>src</code> 034 * and <code>count</code> has the two <i>value </i> <code>occurrences</code> and 035 * <code>TEST</code>. 036 * <p> 037 * Typical method calls would have the following results. 038 * <table> 039 * <tr> 040 * <th>method call</th> 041 * <th>result</th> 042 * </tr> 043 * <tr> 044 * <td><code>hasParameter("dde")</code></td> 045 * <td><code>true</code></td> 046 * </tr> 047 * <tr> 048 * <td><code>hasParameterAndValue("dde")</code></td> 049 * <td><code>false</code></td> 050 * </tr> 051 * <tr> 052 * <td><code>hasParameter("TEST")</code></td> 053 * <td><code>false</code></td> 054 * </tr> 055 * <tr> 056 * <td><code>getValue("src")</code></td> 057 * <td>"dir"</td> 058 * </tr> 059 * <tr> 060 * <td><code>getValue("count")</code></td> 061 * <td>"occurrences"</td> 062 * </tr> 063 * <tr> 064 * <td><code>getValues("count")</code></td> 065 * <td>["occurrences", "TEST"]</td> 066 * </tr> 067 * </table> 068 * 069 * @deprecated Use the CommandLine class instead. 070 * 071 * @author Florian Deissenboeck 072 */ 073@Deprecated 074public class CmdLine { 075 /** Parameter store. */ 076 private final String[] parameters; 077 078 /** The prefix. */ 079 private final String parameterPrefix; 080 081 /** 082 * Create new <code>CmdLine</code> -object from command line arguments. 083 * Parameter prefix is "-". 084 * 085 * @param params 086 * command line arguments as provided in <code>main()</code> 087 * -method. 088 */ 089 public CmdLine(String[] params) { 090 this(params, "-"); 091 092 } 093 094 /** 095 * Create new <code>CmdLine</code> -object from command line arguments. 096 * 097 * @param params 098 * command line arguments as provided in <code>main()</code> 099 * -method. 100 * @param parameterPrefix 101 * parameter prefix 102 */ 103 public CmdLine(String[] params, String parameterPrefix) { 104 this.parameters = params; 105 this.parameterPrefix = parameterPrefix; 106 107 } 108 109 /** 110 * Get number of parameters. 111 * 112 * @return number of parameters. 113 */ 114 public int getParameterCount() { 115 return parameters.length; 116 } 117 118 /** 119 * Get the values for a parameter. 120 * 121 * @param parameterName 122 * name of the parameter. 123 * @return the values associated with this parameter. If the parameter is 124 * not present or doesn't habe a value <code>null</code> is 125 * returned. 126 */ 127 public String[] getValues(String parameterName) { 128 if (!hasParameter(parameterName)) { 129 return null; 130 } 131 int index = StringUtils.indexOf(parameters, parameterPrefix + parameterName); 132 ArrayList<String> result = new ArrayList<String>(); 133 for (int i = index + 1; i < parameters.length; i++) { 134 String current = parameters[i].trim(); 135 if (!isValue(current)) { 136 break; 137 } 138 result.add(current); 139 } 140 if (result.size() == 0) { 141 return null; 142 } 143 return result.toArray(new String[0]); 144 } 145 146 /** 147 * Get the value for a parameter. 148 * 149 * @param parameterName 150 * name of the parameter. 151 * @return the value associated with this parameter. If the parameter is not 152 * present or doesn't habe a value <code>null</code> is returned. 153 */ 154 public String getValue(String parameterName) { 155 if (!hasParameter(parameterName)) { 156 return null; 157 } 158 int index = StringUtils.indexOf(parameters, parameterPrefix + parameterName); 159 String result = parameters[index + 1].trim(); 160 if (!isValue(result)) { 161 return null; 162 } 163 return result; 164 } 165 166 /** 167 * Checks if this command line has a certain parameter. 168 * 169 * @param parameterName 170 * name of the parameter 171 * @return <code>true</code> if parameter is present, <code>false</code> 172 * otherwise. 173 */ 174 public boolean hasParameter(String parameterName) { 175 int index = StringUtils.indexOf(parameters, parameterPrefix + parameterName); 176 return (index != -1); 177 } 178 179 /** 180 * Checks if this command line has a certain parameter with at least one 181 * value. 182 * 183 * @param parameterName 184 * name of the parameter 185 * @return <code>true</code> if parameter and value is present, 186 * <code>false</code> otherwise. 187 */ 188 public boolean hasParameterAndValue(String parameterName) { 189 int index = StringUtils.indexOf(parameters, parameterPrefix + parameterName); 190 if (index < 0) { 191 return false; 192 } 193 if (index >= parameters.length - 1) { 194 return false; 195 } 196 return isValue(parameters[index + 1]); 197 } 198 199 /** 200 * Check is a certain string is a value, i.e. is no parameter. 201 * 202 * @param string 203 * the string in question. 204 * @return <code>true</code> it is a value, <code>false</code> otherwise. 205 */ 206 private boolean isValue(String string) { 207 return (string.trim().indexOf(parameterPrefix) != 0); 208 } 209 210}