001 package org.apache.fulcrum.yaafi.cli;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 /**
023 * Extremly simply command line parsing class.
024 */
025
026 public class Getopt
027 {
028 /** the prefix for determining command line parameters, e.g "-" or "--" */
029 private String prefix;
030
031 /** the command line parameters */
032 private String[] args;
033
034 /**
035 * Constructor
036 * @param args the command line parameters
037 */
038 public Getopt( String[] args )
039 {
040 this(args,"--");
041 }
042
043 /**
044 * Constructor.
045 *
046 * @param args the command line parameters
047 * @param prefix the prefix for command line paramters
048 */
049 public Getopt( String[] args, String prefix )
050 {
051 this.prefix = prefix;
052
053 if( args == null )
054 {
055 this.args = new String[0];
056 }
057 else
058 {
059 this.args = args;
060 }
061 }
062
063 /**
064 * @param option the option we are looking for
065 * @return is the given option contained in the command line arguments?
066 */
067
068 public boolean contains( String option )
069 {
070 return( this.find(option) >= 0 ? true : false );
071 }
072
073 /**
074 * @return the number of command line arguments
075 */
076 public int length()
077 {
078 return this.args.length;
079 }
080
081 /**
082 * Returns the string value for the given option.
083 * @param option the option
084 * @return the associated value
085 */
086 public String getStringValue( String option )
087 {
088 return this.getValue(option);
089 }
090
091 /**
092 * Returns the string value for the given option.
093 * @param option the option
094 * @param defaultValue the default value if the option is not defined
095 * @return the associated value
096 */
097 public String getStringValue( String option, String defaultValue )
098 {
099 return this.getValue(option,defaultValue);
100 }
101
102 /**
103 * Returns the boolean value for the given option.
104 * @param option the option
105 * @return the associated value
106 */
107
108 public boolean getBooleanValue( String option )
109 {
110 return Boolean.valueOf(this.getValue(option)).booleanValue();
111 }
112
113 /**
114 * Returns the boolean value for the given option.
115 * @param option the option
116 * @param defaultValue the default value if the option is not defined
117 * @return the associated value
118 */
119 public boolean getBooleanValue( String option, boolean defaultValue )
120 {
121 String temp = Boolean.toString(defaultValue);
122 return Boolean.valueOf(this.getValue(option,temp)).booleanValue();
123 }
124
125 /**
126 * Get the given argument.
127 * @param index the index of the command line argument
128 * @return the commandl ine argument
129 */
130 private String getArg( int index )
131 {
132 return this.args[index];
133 }
134
135 /**
136 * @option the option
137 * @return the index of the give option or -1 otherwise
138 */
139 private int find( String option )
140 {
141 String strOption = this.prefix + option;
142
143 // Iterate through all command line arguments and look for "-[chOption]"
144
145 for( int i = 0; i < args.length; i++)
146 {
147 if ( args[i].equals( strOption ) )
148 {
149 return i;
150 }
151 }
152
153 return -1;
154 }
155
156 /**
157 * Determines if a value is defined for the given option
158 * @param option the given option
159 * @return true if a value is defined
160 */
161 private boolean hasValue( int index )
162 {
163 String value = null;
164
165 if( (index+1) < this.length() )
166 {
167 value = this.getArg(index+1);
168
169 if( value.startsWith(this.prefix) )
170 {
171 return false;
172 }
173 else
174 {
175 return true;
176 }
177 }
178 else
179 {
180 return false;
181 }
182 }
183
184 /**
185 * Get the value of a command line option
186 * @param option the option
187 * @param defaultValue the default value if the option was not found
188 * @return the value of the option
189 */
190 private String getValue( String option )
191 {
192 String value = this.getValue(option,null);
193
194 if( value == null )
195 {
196 // the options is there but no value was defined by the caller
197 String msg = "No value supplied for " + this.prefix + option;
198 throw new IllegalArgumentException( msg );
199 }
200 else
201 {
202 return value;
203 }
204 }
205
206 /**
207 * Get the value of a command line option
208 * @param option the option
209 * @param defaultValue the default value if the option was not found
210 * @return the value of the option
211 */
212 private String getValue( String option, String defaultValue )
213 {
214 int index = this.find(option);
215
216 if( index < 0 )
217 {
218 // the option is not found
219 return defaultValue;
220 }
221
222 if( this.hasValue(index) )
223 {
224 // a value is available for this option
225 return this.getArg(index+1);
226 }
227 else
228 {
229 // the options is there but no value was defined by the caller
230 String msg = "No value supplied for " + this.prefix + option;
231 throw new IllegalArgumentException( msg );
232 }
233 }
234 }
235