KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > cli > OptionBuilder


1 /*
2  * $Header: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/OptionBuilder.java,v 1.12 2002/10/15 22:50:45 jkeyes Exp $
3  * $Revision: 1.12 $
4  * $Date: 2002/10/15 22:50:45 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62 package org.apache.commons.cli;
63
64 /**
65  * <p>OptionBuilder allows the user to create Options using descriptive
66  * methods.</p>
67  * <p>Details on the Builder pattern can be found at
68  * <a HREF="http://c2.com/cgi-bin/wiki?BuilderPattern">http://c2.com/cgi-bin/wiki?BuilderPattern</a>.</p>
69  *
70  * @author John Keyes ( john at integralsource.com )
71  * @since 1.0
72  */

73 public class OptionBuilder {
74
75     /** long option */
76     private static String JavaDoc longopt;
77     /** option description */
78     private static String JavaDoc description;
79     /** argument name */
80     private static String JavaDoc argName;
81     /** is required? */
82     private static boolean required;
83     /** the number of arguments */
84     private static int numberOfArgs = Option.UNINITIALIZED;
85     /** option type */
86     private static Object JavaDoc type;
87     /** option can have an optional argument value */
88     private static boolean optionalArg;
89     /** value separator for argument value */
90     private static char valuesep;
91
92     /** option builder instance */
93     private static OptionBuilder instance = new OptionBuilder();
94
95     // private constructor
96
private OptionBuilder() {
97     }
98
99     /**
100      * <p>Resets the member variables to their default values.</p>
101      */

102     private static void reset() {
103         description = null;
104         argName = null;
105         longopt = null;
106         type = null;
107         required = false;
108         numberOfArgs = Option.UNINITIALIZED;
109
110         // PMM 9/6/02 - these were missing
111
optionalArg = false;
112         valuesep = (char) 0;
113     }
114
115     /**
116      * <p>The next Option created will have the following long option value.</p>
117      *
118      * @param longopt the long option value
119      * @return the OptionBuilder instance
120      */

121     public static OptionBuilder withLongOpt( String JavaDoc longopt ) {
122         instance.longopt = longopt;
123         return instance;
124     }
125
126     /**
127      * <p>The next Option created will require an argument value.</p>
128      *
129      * @return the OptionBuilder instance
130      */

131     public static OptionBuilder hasArg( ) {
132         instance.numberOfArgs = 1;
133         return instance;
134     }
135
136     /**
137      * <p>The next Option created will require an argument value if
138      * <code>hasArg</code> is true.</p>
139      *
140      * @param hasArg if true then the Option has an argument value
141      * @return the OptionBuilder instance
142      */

143     public static OptionBuilder hasArg( boolean hasArg ) {
144         instance.numberOfArgs = ( hasArg == true ) ? 1 : Option.UNINITIALIZED;
145         return instance;
146     }
147
148     /**
149      * <p>The next Option created will have the specified argument value
150      * name.</p>
151      *
152      * @param name the name for the argument value
153      * @return the OptionBuilder instance
154      */

155     public static OptionBuilder withArgName( String JavaDoc name ) {
156         instance.argName = name;
157         return instance;
158     }
159
160     /**
161      * <p>The next Option created will be required.</p>
162      *
163      * @return the OptionBuilder instance
164      */

165     public static OptionBuilder isRequired( ) {
166         instance.required = true;
167         return instance;
168     }
169
170     /**
171      * <p>The next Option created uses <code>sep</code> as a means to
172      * separate argument values.</p>
173      *
174      * <b>Example:</b>
175      * <pre>
176      * Option opt = OptionBuilder.withValueSeparator( ':' )
177      * .create( 'D' );
178      *
179      * CommandLine line = parser.parse( args );
180      * String propertyName = opt.getValue( 0 );
181      * String propertyValue = opt.getValue( 1 );
182      * </pre>
183      *
184      * @return the OptionBuilder instance
185      */

186     public static OptionBuilder withValueSeparator( char sep ) {
187         instance.valuesep = sep;
188         return instance;
189     }
190
191     /**
192      * <p>The next Option created uses '<code>=</code>' as a means to
193      * separate argument values.</p>
194      *
195      * <b>Example:</b>
196      * <pre>
197      * Option opt = OptionBuilder.withValueSeparator( )
198      * .create( 'D' );
199      *
200      * CommandLine line = parser.parse( args );
201      * String propertyName = opt.getValue( 0 );
202      * String propertyValue = opt.getValue( 1 );
203      * </pre>
204      *
205      * @return the OptionBuilder instance
206      */

207     public static OptionBuilder withValueSeparator( ) {
208         instance.valuesep = '=';
209         return instance;
210     }
211
212     /**
213      * <p>The next Option created will be required if <code>required</code>
214      * is true.</p>
215      *
216      * @param required if true then the Option is required
217      * @return the OptionBuilder instance
218      */

219     public static OptionBuilder isRequired( boolean required ) {
220         instance.required = required;
221         return instance;
222     }
223
224     /**
225      * <p>The next Option created can have unlimited argument values.</p>
226      *
227      * @return the OptionBuilder instance
228      */

229     public static OptionBuilder hasArgs( ) {
230         instance.numberOfArgs = Option.UNLIMITED_VALUES;
231         return instance;
232     }
233
234     /**
235      * <p>The next Option created can have <code>num</code>
236      * argument values.</p>
237      *
238      * @param num the number of args that the option can have
239      * @return the OptionBuilder instance
240      */

241     public static OptionBuilder hasArgs( int num ) {
242         instance.numberOfArgs = num;
243         return instance;
244     }
245
246     /**
247      * <p>The next Option can have an optional argument.</p>
248      *
249      * @return the OptionBuilder instance
250      */

251     public static OptionBuilder hasOptionalArg( ) {
252         instance.numberOfArgs = 1;
253         instance.optionalArg = true;
254         return instance;
255     }
256
257     /**
258      * <p>The next Option can have an unlimited number of
259      * optional arguments.</p>
260      *
261      * @return the OptionBuilder instance
262      */

263     public static OptionBuilder hasOptionalArgs( ) {
264         instance.numberOfArgs = Option.UNLIMITED_VALUES;
265         instance.optionalArg = true;
266         return instance;
267     }
268
269     /**
270      * <p>The next Option can have the specified number of
271      * optional arguments.</p>
272      *
273      * @param numArgs - the maximum number of optional arguments
274      * the next Option created can have.
275      * @return the OptionBuilder instance
276      */

277     public static OptionBuilder hasOptionalArgs( int numArgs ) {
278         instance.numberOfArgs = numArgs;
279         instance.optionalArg = true;
280         return instance;
281     }
282
283     /**
284      * <p>The next Option created will have a value that will be an instance
285      * of <code>type</code>.</p>
286      *
287      * @param type the type of the Options argument value
288      * @return the OptionBuilder instance
289      */

290     public static OptionBuilder withType( Object JavaDoc type ) {
291         instance.type = type;
292         return instance;
293     }
294
295     /**
296      * <p>The next Option created will have the specified description</p>
297      *
298      * @param description a description of the Option's purpose
299      * @return the OptionBuilder instance
300      */

301     public static OptionBuilder withDescription( String JavaDoc description ) {
302         instance.description = description;
303         return instance;
304     }
305
306     /**
307      * <p>Create an Option using the current settings and with
308      * the specified Option <code>char</code>.</p>
309      *
310      * @param opt the character representation of the Option
311      * @return the Option instance
312      * @throws IllegalArgumentException if <code>opt</code> is not
313      * a valid character. See Option.
314      */

315     public static Option create( char opt )
316     throws IllegalArgumentException JavaDoc
317     {
318         return create( String.valueOf( opt ) );
319     }
320
321     /**
322      * <p>Create an Option using the current settings</p>
323      *
324      * @return the Option instance
325      * @throws IllegalArgumentException if <code>longOpt</code> has
326      * not been set.
327      */

328     public static Option create()
329     throws IllegalArgumentException JavaDoc
330     {
331         if( longopt == null ) {
332             throw new IllegalArgumentException JavaDoc( "must specify longopt" );
333         }
334
335         return create( " " );
336     }
337
338     /**
339      * <p>Create an Option using the current settings and with
340      * the specified Option <code>char</code>.</p>
341      *
342      * @param opt the <code>java.lang.String</code> representation
343      * of the Option
344      * @return the Option instance
345      * @throws IllegalArgumentException if <code>opt</code> is not
346      * a valid character. See Option.
347      */

348     public static Option create( String JavaDoc opt )
349     throws IllegalArgumentException JavaDoc
350     {
351         // create the option
352
Option option = new Option( opt, description );
353
354         // set the option properties
355
option.setLongOpt( longopt );
356         option.setRequired( required );
357         option.setOptionalArg( optionalArg );
358         option.setArgs( numberOfArgs );
359         option.setType( type );
360         option.setValueSeparator( valuesep );
361         option.setArgName( argName );
362         // reset the OptionBuilder properties
363
instance.reset();
364
365         // return the Option instance
366
return option;
367     }
368 }
Popular Tags