KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > cli > CLOptionDescriptor


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

50 package org.apache.avalon.excalibur.cli;
51
52 /**
53  * Basic class describing an type of option.
54  * Typically, one creates a static array of <code>CLOptionDescriptor</code>s,
55  * and passes it to {@link CLArgsParser#CLArgsParser(String[], CLOptionDescriptor[])}.
56  *
57  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
58  * @version $Revision: 1.5 $ $Date: 2003/04/11 10:25:52 $
59  * @since 4.0
60  * @see CLArgsParser
61  * @see CLUtil
62  * @deprecated Toolkit deprecated and replaced by http://spice.sourceforge.net/cli/
63  */

64 public final class CLOptionDescriptor
65 {
66     /** Flag to say that one argument is required */
67     public static final int ARGUMENT_REQUIRED = 1 << 1;
68     /** Flag to say that the argument is optional */
69     public static final int ARGUMENT_OPTIONAL = 1 << 2;
70     /** Flag to say this option does not take arguments */
71     public static final int ARGUMENT_DISALLOWED = 1 << 3;
72     /** Flag to say this option requires 2 arguments */
73     public static final int ARGUMENTS_REQUIRED_2 = 1 << 4;
74     /** Flag to say this option may be repeated on the command line */
75     public static final int DUPLICATES_ALLOWED = 1 << 5;
76
77     private final int m_id;
78     private final int m_flags;
79     private final String JavaDoc m_name;
80     private final String JavaDoc m_description;
81     private final int[] m_incompatible;
82
83     /**
84      * Constructor.
85      *
86      * @param name the name/long option
87      * @param flags the flags
88      * @param id the id/character option
89      * @param description description of option usage
90      */

91     public CLOptionDescriptor( final String JavaDoc name,
92                                final int flags,
93                                final int id,
94                                final String JavaDoc description )
95     {
96         this( name, flags, id, description,
97               ( ( flags & CLOptionDescriptor.DUPLICATES_ALLOWED ) > 0 )
98               ? new int[ 0 ] : new int[]{id} );
99     }
100
101     /**
102      * Constructor.
103      *
104      * @param name the name/long option
105      * @param flags the flags
106      * @param id the id/character option
107      * @param description description of option usage
108      * @param incompatible an array listing the ids of all incompatible options
109      * @deprecated use the version with the array of CLOptionDescriptor's
110      */

111     public CLOptionDescriptor( final String JavaDoc name,
112                                final int flags,
113                                final int id,
114                                final String JavaDoc description,
115                                final int[] incompatible )
116     {
117         m_id = id;
118         m_name = name;
119         m_flags = flags;
120         m_description = description;
121         m_incompatible = incompatible;
122
123         int modeCount = 0;
124         if( ( ARGUMENT_REQUIRED & flags ) == ARGUMENT_REQUIRED )
125         {
126             modeCount++;
127         }
128         if( ( ARGUMENT_OPTIONAL & flags ) == ARGUMENT_OPTIONAL )
129         {
130             modeCount++;
131         }
132         if( ( ARGUMENT_DISALLOWED & flags ) == ARGUMENT_DISALLOWED )
133         {
134             modeCount++;
135         }
136         if( ( ARGUMENTS_REQUIRED_2 & flags ) == ARGUMENTS_REQUIRED_2 )
137         {
138             modeCount++;
139         }
140
141         if( 0 == modeCount )
142         {
143             final String JavaDoc message = "No mode specified for option " + this;
144             throw new IllegalStateException JavaDoc( message );
145         }
146         else if( 1 != modeCount )
147         {
148             final String JavaDoc message = "Multiple modes specified for option " + this;
149             throw new IllegalStateException JavaDoc( message );
150         }
151     }
152
153     /**
154      * Constructor.
155      *
156      * @param name the name/long option
157      * @param flags the flags
158      * @param id the id/character option
159      * @param description description of option usage
160      */

161     public CLOptionDescriptor( final String JavaDoc name,
162                                final int flags,
163                                final int id,
164                                final String JavaDoc description,
165                                final CLOptionDescriptor[] incompatible )
166     {
167         m_id = id;
168         m_name = name;
169         m_flags = flags;
170         m_description = description;
171
172         m_incompatible = new int[ incompatible.length ];
173         for( int i = 0; i < incompatible.length; i++ )
174             m_incompatible[ i ] = incompatible[ i ].getId();
175     }
176
177     /**
178      * @deprecated Use the correctly spelled {@link #getIncompatible} instead.
179      * @return the array of incompatible option ids
180      */

181     protected final int[] getIncompatble()
182     {
183         return getIncompatible();
184     }
185
186     /**
187      * Get the array of incompatible option ids.
188      *
189      * @return the array of incompatible option ids
190      */

191     protected final int[] getIncompatible()
192     {
193         return m_incompatible;
194     }
195
196     /**
197      * Retrieve textual description.
198      *
199      * @return the description
200      */

201     public final String JavaDoc getDescription()
202     {
203         return m_description;
204     }
205
206     /**
207      * Retrieve flags about option.
208      * Flags include details such as whether it allows parameters etc.
209      *
210      * @return the flags
211      */

212     public final int getFlags()
213     {
214         return m_flags;
215     }
216
217     /**
218      * Retrieve the id for option.
219      * The id is also the character if using single character options.
220      *
221      * @return the id
222      */

223     public final int getId()
224     {
225         return m_id;
226     }
227
228     /**
229      * Retrieve name of option which is also text for long option.
230      *
231      * @return name/long option
232      */

233     public final String JavaDoc getName()
234     {
235         return m_name;
236     }
237
238     /**
239      * Convert to String.
240      *
241      * @return the converted value to string.
242      */

243     public final String JavaDoc toString()
244     {
245         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
246         sb.append( "[OptionDescriptor " );
247         sb.append( m_name );
248         sb.append( "[OptionDescriptor " );
249         sb.append( m_name );
250         sb.append( ", " );
251         sb.append( m_id );
252         sb.append( ", " );
253         sb.append( m_flags );
254         sb.append( ", " );
255         sb.append( m_description );
256         sb.append( " ]" );
257         return sb.toString();
258     }
259 }
260
Popular Tags