1+ /*
2+ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/OptionBuilder.java,v 1.2 2002/07/30 23:06:21 jkeyes Exp $
3+ * $Revision: 1.2 $
4+ * $Date: 2002/07/30 23:06:21 $
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+
162package org .apache .commons .cli ;
263
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://">http://</a>.</p>
69+ *
70+ * @author John Keyes ( jbjk at mac.com )
71+ * @since 1.0
72+ */
373public class OptionBuilder {
474
75+ /** long option */
576 private static String longopt ;
77+ /** option description */
678 private static String description ;
79+ /** has an argument? */
780 private static boolean arg ;
81+ /** is required? */
882 private static boolean required ;
83+ /** has multiple arguments */
984 private static boolean multipleArgs ;
85+ /** option type */
1086 private static Object type ;
1187
88+ /** option builder instance */
1289 private static OptionBuilder instance = new OptionBuilder ();
1390
1491 // private constructor
1592 private OptionBuilder () {
1693 }
1794
95+ /**
96+ * <p>Resets the member variables to their default values.</p>
97+ */
1898 private static void reset () {
1999 description = null ;
20100 longopt = null ;
@@ -24,51 +104,135 @@ private static void reset() {
24104 multipleArgs = false ;
25105 }
26106
107+ /**
108+ * <p>The next Option created will have the following long option value.</p>
109+ *
110+ * @param longopt the long option value
111+ * @return the OptionBuilder instance
112+ */
27113 public static OptionBuilder withLongOpt ( String longopt ) {
28114 instance .longopt = longopt ;
29115 return instance ;
30116 }
31117
118+ /**
119+ * <p>The next Option created will require an argument value.</p>
120+ *
121+ * @return the OptionBuilder instance
122+ */
32123 public static OptionBuilder hasArg ( ) {
33124 instance .arg = true ;
34125 return instance ;
35126 }
36127
128+ /**
129+ * <p>The next Option created will require an argument value if
130+ * <code>hasArg</code> is true.</p>
131+ *
132+ * @param hasArg if true then the Option has an argument value
133+ * @return the OptionBuilder instance
134+ */
135+ public static OptionBuilder hasArg ( boolean hasArg ) {
136+ instance .arg = hasArg ;
137+ return instance ;
138+ }
139+
140+ /**
141+ * <p>The next Option created will be required.</p>
142+ *
143+ * @return the OptionBuilder instance
144+ */
37145 public static OptionBuilder isRequired ( ) {
38146 instance .required = true ;
39147 return instance ;
40148 }
41149
150+ /**
151+ * <p>The next Option created will be required if <code>required</code>
152+ * is true.</p>
153+ *
154+ * @param required if true then the Option is required
155+ * @return the OptionBuilder instance
156+ */
157+ public static OptionBuilder isRequired ( boolean required ) {
158+ instance .required = required ;
159+ return instance ;
160+ }
161+
162+ /**
163+ * <p>The next Option created can have multiple argument values.</p>
164+ *
165+ * @return the OptionBuilder instance
166+ */
42167 public static OptionBuilder hasMultipleArgs ( ) {
43168 instance .multipleArgs = true ;
44169 return instance ;
45170 }
46171
172+ /**
173+ * <p>The next Option created will have a value that will be an instance
174+ * of <code>type</code>.</p>
175+ *
176+ * @param type the type of the Options argument value
177+ * @return the OptionBuilder instance
178+ */
47179 public static OptionBuilder withType ( Object type ) {
48180 instance .type = type ;
49181 return instance ;
50182 }
51183
184+ /**
185+ * <p>The next Option created will have the specified description</p>
186+ *
187+ * @param description a description of the Option's purpose
188+ * @return the OptionBuilder instance
189+ */
52190 public static OptionBuilder withDescription ( String description ) {
53191 instance .description = description ;
54192 return instance ;
55193 }
56194
195+ /**
196+ * <p>Create an Option using the current settings and with
197+ * the specified Option <code>char</code>.</p>
198+ *
199+ * @param opt the character representation of the Option
200+ * @return the Option instance
201+ * @throws IllegalArgumentException if <code>opt</code> is not
202+ * a valid character. See Option.
203+ */
57204 public static Option create ( char opt )
58205 throws IllegalArgumentException
59206 {
60207 return create ( String .valueOf ( opt ) );
61208 }
62209
210+ /**
211+ * <p>Create an Option using the current settings and with
212+ * the specified Option <code>char</code>.</p>
213+ *
214+ * @param opt the <code>java.lang.String</code> representation
215+ * of the Option
216+ * @return the Option instance
217+ * @throws IllegalArgumentException if <code>opt</code> is not
218+ * a valid character. See Option.
219+ */
63220 public static Option create ( String opt )
64221 throws IllegalArgumentException
65222 {
223+ // create the option
66224 Option option = new Option ( opt , arg , description );
225+
226+ // set the option properties
67227 option .setLongOpt ( longopt );
68228 option .setRequired ( required );
69229 option .setMultipleArgs ( multipleArgs );
70230 option .setType ( type );
231+
232+ // reset the OptionBuilder properties
71233 instance .reset ();
234+
235+ // return the Option instance
72236 return option ;
73237 }
74238}
0 commit comments