KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Options.java,v 1.5 2002/06/06 22:32:37 bayard Exp $
3  * $Revision: 1.5 $
4  * $Date: 2002/06/06 22:32:37 $
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 import java.util.ArrayList JavaDoc;
65 import java.util.Collection JavaDoc;
66 import java.util.Collections JavaDoc;
67 import java.util.HashMap JavaDoc;
68 import java.util.Iterator JavaDoc;
69 import java.util.List JavaDoc;
70 import java.util.Map JavaDoc;
71 import java.util.*;
72
73 /** <p>Main entry-point into the library.</p>
74  *
75  * <p>Options represents a collection of {@link Option} objects, which
76  * describe the possible options for a command-line.<p>
77  *
78  * <p>It may flexibly parse long and short options, with or without
79  * values. Additionally, it may parse only a portion of a commandline,
80  * allowing for flexible multi-stage parsing.<p>
81  *
82  * @see org.apache.commons.cli.CommandLine
83  *
84  * @author bob mcwhirter (bob @ werken.com)
85  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
86  * @version $Revision: 1.5 $
87  */

88 public class Options {
89
90     /** a map of the options with the character key */
91     private Map JavaDoc shortOpts = new HashMap JavaDoc();
92
93     /** a map of the options with the long key */
94     private Map JavaDoc longOpts = new HashMap JavaDoc();
95
96     /** a map of the required options */
97     private List JavaDoc requiredOpts = new ArrayList JavaDoc();
98     
99     /** a map of the option groups */
100     private Map JavaDoc optionGroups = new HashMap JavaDoc();
101
102     /** <p>Construct a new Options descriptor</p>
103      */

104     public Options() {
105     }
106
107     /**
108      * <p>Add the specified option group.</p>
109      *
110      * @param group the OptionGroup that is to be added
111      * @return the resulting Options instance
112      */

113     public Options addOptionGroup( OptionGroup group ) {
114         Iterator JavaDoc options = group.getOptions().iterator();
115
116         if( group.isRequired() ) {
117             requiredOpts.add( group );
118         }
119
120         while( options.hasNext() ) {
121             Option option = (Option)options.next();
122             // an Option cannot be required if it is in an
123
// OptionGroup, either the group is required or
124
// nothing is required
125
option.setRequired( false );
126             addOption( option );
127
128             optionGroups.put( option.getOpt(), group );
129         }
130
131         return this;
132     }
133
134     /** <p>Add an option that only contains a short-name</p>
135      * <p>It may be specified as requiring an argument.</p>
136      *
137      * @param opt Short single-character name of the option.
138      * @param hasArg flag signally if an argument is required after this option
139      * @param description Self-documenting description
140      * @return the resulting Options instance
141      */

142     public Options addOption(String JavaDoc opt, boolean hasArg, String JavaDoc description) {
143         addOption( opt, null, hasArg, description );
144         return this;
145     }
146     
147     /** <p>Add an option that contains a short-name and a long-name</p>
148      * <p>It may be specified as requiring an argument.</p>
149      *
150      * @param opt Short single-character name of the option.
151      * @param longOpt Long multi-character name of the option.
152      * @param hasArg flag signally if an argument is required after this option
153      * @param description Self-documenting description
154      * @return the resulting Options instance
155      */

156     public Options addOption(String JavaDoc opt, String JavaDoc longOpt, boolean hasArg, String JavaDoc description) {
157         addOption( new Option( opt, longOpt, hasArg, description ) );
158         return this;
159     }
160
161     /**
162      * <p>Adds an option instance</p>
163      *
164      * @param opt the option that is to be added
165      * @return the resulting Options instance
166      */

167     public Options addOption(Option opt) {
168         String JavaDoc shortOpt = "-" + opt.getOpt();
169         
170         // add it to the long option list
171
if ( opt.hasLongOpt() ) {
172             longOpts.put( "--" + opt.getLongOpt(), opt );
173         }
174         
175         // if the option is required add it to the required list
176
if ( opt.isRequired() ) {
177             requiredOpts.add( shortOpt );
178         }
179
180         shortOpts.put( shortOpt, opt );
181         
182         return this;
183     }
184     
185     /** <p>Retrieve a read-only list of options in this set</p>
186      *
187      * @return read-only Collection of {@link Option} objects in this descriptor
188      */

189     public Collection JavaDoc getOptions() {
190         List JavaDoc opts = new ArrayList JavaDoc( shortOpts.values() );
191
192         // now look through the long opts to see if there are any Long-opt
193
// only options
194
Iterator JavaDoc iter = longOpts.values().iterator();
195         while (iter.hasNext())
196         {
197             Object JavaDoc item = iter.next();
198             if (!opts.contains(item))
199             {
200                 opts.add(item);
201             }
202         }
203         return Collections.unmodifiableCollection( opts );
204     }
205
206     /**
207      * <p>Returns the Options for use by the HelpFormatter.</p>
208      *
209      * @return the List of Options
210      */

211     List JavaDoc helpOptions() {
212         return new ArrayList JavaDoc( shortOpts.values() );
213     }
214
215     /** <p>Returns the required options as a
216      * <code>java.util.Collection</code>.</p>
217      *
218      * @return Collection of required options
219      */

220     public List JavaDoc getRequiredOptions() {
221         return requiredOpts;
222     }
223     
224     /** <p>Retrieve the named {@link Option}</p>
225      *
226      * @param opt short or long name of the {@link Option}
227      * @return the option represented by opt
228      */

229     public Option getOption( String JavaDoc opt ) {
230
231         Option option = null;
232
233         // short option
234
if( opt.length() == 1 ) {
235             option = (Option)shortOpts.get( "-" + opt );
236         }
237         // long option
238
else if( opt.startsWith( "--" ) ) {
239             option = (Option)longOpts.get( opt );
240         }
241         // a just-in-case
242
else {
243             option = (Option)shortOpts.get( opt );
244         }
245
246         return (option == null) ? null : (Option)option.clone();
247     }
248
249     /**
250      * <p>Returns whether the named {@link Option} is a member
251      * of this {@link Options}</p>
252      *
253      * @param opt short or long name of the {@link Option}
254      * @return true if the named {@link Option} is a member
255      * of this {@link Options}
256      */

257     public boolean hasOption( String JavaDoc opt ) {
258
259         // short option
260
if( opt.length() == 1 ) {
261             return shortOpts.containsKey( "-" + opt );
262         }
263         // long option
264
else if( opt.startsWith( "--" ) ) {
265             return longOpts.containsKey( opt );
266         }
267         // a just-in-case
268
else {
269             return shortOpts.containsKey( opt );
270         }
271     }
272
273     /** <p>Returns the OptionGroup the <code>opt</code>
274      * belongs to.</p>
275      * @param opt the option whose OptionGroup is being queried.
276      *
277      * @return the OptionGroup if <code>opt</code> is part
278      * of an OptionGroup, otherwise return null
279      */

280     public OptionGroup getOptionGroup( Option opt ) {
281         return (OptionGroup)optionGroups.get( opt.getOpt() );
282     }
283     
284     /** <p>Dump state, suitable for debugging.</p>
285      *
286      * @return Stringified form of this object
287      */

288     public String JavaDoc toString() {
289         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
290         
291         buf.append("[ Options: [ short ");
292         buf.append( shortOpts.toString() );
293         buf.append( " ] [ long " );
294         buf.append( longOpts );
295         buf.append( " ]");
296         
297         return buf.toString();
298     }
299 }
300
Popular Tags