Skip to content

Commit fbc0bd0

Browse files
author
John Keyes
committed
integrated PatternBuilder
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129792 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7f90b98 commit fbc0bd0

9 files changed

Lines changed: 217 additions & 161 deletions

File tree

src/java/org/apache/commons/cli/Option.java

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private boolean isValidChar( char c )
163163
public Option(String opt, boolean hasArg, String description)
164164
throws IllegalArgumentException
165165
{
166-
this(opt, null, hasArg, description, false, false);
166+
this(opt, null, hasArg, description);
167167
}
168168

169169
/**
@@ -176,69 +176,13 @@ public Option(String opt, boolean hasArg, String description)
176176
*/
177177
public Option(String opt, String longOpt, boolean hasArg, String description)
178178
throws IllegalArgumentException
179-
{
180-
this(opt, longOpt, hasArg, description, false, false );
181-
}
182-
183-
/**
184-
* Creates an Option using the specified parameters.
185-
*
186-
* @param opt short representation of the option
187-
* @param longOpt the long representation of the option
188-
* @param hasArg specifies whether the Option takes an argument or not
189-
* @param description describes the function of the option
190-
* @param required specifies whether the option is required or not
191-
*/
192-
public Option(String opt, String longOpt, boolean hasArg, String description,
193-
boolean required )
194-
throws IllegalArgumentException
195-
{
196-
this(opt, longOpt, hasArg, description, required, false );
197-
}
198-
199-
/**
200-
* Creates an Option using the specified parameters.
201-
*
202-
* @param opt short representation of the option
203-
* @param longOpt the long representation of the option
204-
* @param hasArg specifies whether the Option takes an argument or not
205-
* @param description describes the function of the option
206-
* @param required specifies whether the option is required or not
207-
* @param multipleArgs specifies whether the option has multiple argument
208-
* values
209-
*/
210-
public Option(String opt, String longOpt, boolean hasArg, String description,
211-
boolean required, boolean multipleArgs )
212-
throws IllegalArgumentException
213-
{
214-
this(opt, longOpt, hasArg, description, required, multipleArgs, null );
215-
}
216-
217-
/**
218-
* Creates an Option using the specified parameters.
219-
*
220-
* @param opt short representation of the option
221-
* @param longOpt the long representation of the option
222-
* @param hasArg specifies whether the Option takes an argument or not
223-
* @param description describes the function of the option
224-
* @param required specifies whether the option is required or not
225-
* @param multipleArgs specifies whether the option has multiple argument
226-
* values
227-
* @param type specifies the type of the option
228-
*/
229-
public Option(String opt, String longOpt, boolean hasArg, String description,
230-
boolean required, boolean multipleArgs, Object type )
231-
throws IllegalArgumentException
232179
{
233180
validateOption( opt );
234181

235182
this.opt = opt;
236183
this.longOpt = longOpt;
237184
this.hasArg = hasArg;
238185
this.description = description;
239-
this.required = required;
240-
this.multipleArgs = multipleArgs;
241-
this.type = type;
242186
}
243187

244188
/** <p>Retrieve the name of this Option</p>

src/java/org/apache/commons/cli/OptionBuilder.java

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,100 @@
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+
162
package 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+
*/
373
public 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
}

src/java/org/apache/commons/cli/Options.java

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public Options addOptionGroup( OptionGroup group ) {
128128
* @return the resulting Options instance
129129
*/
130130
public Options addOption(String opt, boolean hasArg, String description) {
131-
addOption( opt, null, hasArg, description, false );
131+
addOption( opt, null, hasArg, description );
132132
return this;
133133
}
134134

@@ -142,58 +142,7 @@ public Options addOption(String opt, boolean hasArg, String description) {
142142
* @return the resulting Options instance
143143
*/
144144
public Options addOption(String opt, String longOpt, boolean hasArg, String description) {
145-
addOption( opt, longOpt, hasArg, description, false );
146-
return this;
147-
}
148-
149-
/** <p>Add an option that contains a short-name and a long-name</p>
150-
* <p>It may be specified as requiring an argument.</p>
151-
*
152-
* @param opt Short single-character name of the option.
153-
* @param longOpt Long multi-character name of the option.
154-
* @param hasArg flag signally if an argument is required after this option
155-
* @param description Self-documenting description
156-
* @param required specifies if this option is required
157-
* @return the resulting Options instance
158-
*/
159-
public Options addOption(String opt, String longOpt, boolean hasArg, String description,
160-
boolean required) {
161-
addOption( new Option(opt, longOpt, hasArg, description, required) );
162-
return this;
163-
}
164-
165-
/** <p>Add an option that contains a short-name and a long-name</p>
166-
* <p>It may be specified as requiring an argument.</p>
167-
*
168-
* @param opt Short single-character name of the option.
169-
* @param longOpt Long multi-character name of the option.
170-
* @param hasArg flag signally if an argument is required after this option
171-
* @param description Self-documenting description
172-
* @param required specifies if this option is required
173-
* @param multipleArgs specifies if this option can accept multiple argument values
174-
* @return the resulting Options instance
175-
*/
176-
public Options addOption(String opt, String longOpt, boolean hasArg, String description,
177-
boolean required, boolean multipleArgs) {
178-
addOption( new Option(opt, longOpt, hasArg, description, required, multipleArgs) );
179-
return this;
180-
}
181-
182-
/** <p>Add an option that contains a short-name and a long-name</p>
183-
* <p>It may be specified as requiring an argument.</p>
184-
*
185-
* @param opt Short single-character name of the option.
186-
* @param longOpt Long multi-character name of the option.
187-
* @param hasArg flag signally if an argument is required after this option
188-
* @param description Self-documenting description
189-
* @param required specifies if this option is required
190-
* @param multipleArgs specifies if this option can accept multiple argument values
191-
* @param type specifies the type for the value of the option
192-
* @return the resulting Options instance
193-
*/
194-
public Options addOption(String opt, String longOpt, boolean hasArg, String description,
195-
boolean required, boolean multipleArgs, Object type) {
196-
addOption( new Option(opt, longOpt, hasArg, description, required, multipleArgs, type) );
145+
addOption( new Option( opt, longOpt, hasArg, description ) );
197146
return this;
198147
}
199148

0 commit comments

Comments
 (0)