KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.commons.cli.avalon;
18 //Renamed from org.apache.avalon.excalibur.cli
19

20 /**
21  * Basic class describing an type of option.
22  * Typically, one creates a static array of <code>CLOptionDescriptor</code>s,
23  * and passes it to {@link CLArgsParser#CLArgsParser(String[], CLOptionDescriptor[])}.
24  *
25  * @version $Revision: 1.1.2.2 $ $Date: 2005/02/20 15:56:12 $
26  * @see CLArgsParser
27  * @see CLUtil
28  */

29 public final class CLOptionDescriptor
30 {
31     /** Flag to say that one argument is required */
32     public static final int ARGUMENT_REQUIRED = 1 << 1;
33     /** Flag to say that the argument is optional */
34     public static final int ARGUMENT_OPTIONAL = 1 << 2;
35     /** Flag to say this option does not take arguments */
36     public static final int ARGUMENT_DISALLOWED = 1 << 3;
37     /** Flag to say this option requires 2 arguments */
38     public static final int ARGUMENTS_REQUIRED_2 = 1 << 4;
39     /** Flag to say this option may be repeated on the command line */
40     public static final int DUPLICATES_ALLOWED = 1 << 5;
41
42     private final int m_id;
43     private final int m_flags;
44     private final String JavaDoc m_name;
45     private final String JavaDoc m_description;
46     private final int[] m_incompatible;
47
48     /**
49      * Constructor.
50      *
51      * @param name the name/long option
52      * @param flags the flags
53      * @param id the id/character option
54      * @param description description of option usage
55      */

56     public CLOptionDescriptor( final String JavaDoc name,
57             final int flags,
58             final int id,
59             final String JavaDoc description )
60     {
61         this( name, flags, id, description,
62                 ((flags & CLOptionDescriptor.DUPLICATES_ALLOWED) > 0)
63                 ? new int[0] : new int[]{id} );
64     }
65
66     /**
67      * Constructor.
68      *
69      * @param name the name/long option
70      * @param flags the flags
71      * @param id the id/character option
72      * @param description description of option usage
73      * @param incompatible an array listing the ids of all incompatible options
74      * @deprecated use the version with the array of CLOptionDescriptor's
75      */

76     public CLOptionDescriptor( final String JavaDoc name,
77             final int flags,
78             final int id,
79             final String JavaDoc description,
80             final int[] incompatible )
81     {
82         m_id = id;
83         m_name = name;
84         m_flags = flags;
85         m_description = description;
86         m_incompatible = incompatible;
87
88         int modeCount = 0;
89         if( (ARGUMENT_REQUIRED & flags) == ARGUMENT_REQUIRED )
90         {
91             modeCount++;
92         }
93         if( (ARGUMENT_OPTIONAL & flags) == ARGUMENT_OPTIONAL )
94         {
95             modeCount++;
96         }
97         if( (ARGUMENT_DISALLOWED & flags) == ARGUMENT_DISALLOWED )
98         {
99             modeCount++;
100         }
101         if( (ARGUMENTS_REQUIRED_2 & flags) == ARGUMENTS_REQUIRED_2 )
102         {
103             modeCount++;
104         }
105
106         if( 0 == modeCount )
107         {
108             final String JavaDoc message = "No mode specified for option " + this;
109             throw new IllegalStateException JavaDoc( message );
110         }
111         else if( 1 != modeCount )
112         {
113             final String JavaDoc message = "Multiple modes specified for option " + this;
114             throw new IllegalStateException JavaDoc( message );
115         }
116     }
117
118     /**
119      * Constructor.
120      *
121      * @param name the name/long option
122      * @param flags the flags
123      * @param id the id/character option
124      * @param description description of option usage
125      */

126     public CLOptionDescriptor( final String JavaDoc name,
127             final int flags,
128             final int id,
129             final String JavaDoc description,
130             final CLOptionDescriptor[] incompatible )
131     {
132         m_id = id;
133         m_name = name;
134         m_flags = flags;
135         m_description = description;
136
137         m_incompatible = new int[incompatible.length];
138         for( int i = 0; i < incompatible.length; i++ )
139         {
140             m_incompatible[i] = incompatible[i].getId();
141         }
142     }
143
144     /**
145      * @deprecated Use the correctly spelled {@link #getIncompatible} instead.
146      * @return the array of incompatible option ids
147      */

148     protected final int[] getIncompatble()
149     {
150         return getIncompatible();
151     }
152
153     /**
154      * Get the array of incompatible option ids.
155      *
156      * @return the array of incompatible option ids
157      */

158     protected final int[] getIncompatible()
159     {
160         return m_incompatible;
161     }
162
163     /**
164      * Retrieve textual description.
165      *
166      * @return the description
167      */

168     public final String JavaDoc getDescription()
169     {
170         return m_description;
171     }
172
173     /**
174      * Retrieve flags about option.
175      * Flags include details such as whether it allows parameters etc.
176      *
177      * @return the flags
178      */

179     public final int getFlags()
180     {
181         return m_flags;
182     }
183
184     /**
185      * Retrieve the id for option.
186      * The id is also the character if using single character options.
187      *
188      * @return the id
189      */

190     public final int getId()
191     {
192         return m_id;
193     }
194
195     /**
196      * Retrieve name of option which is also text for long option.
197      *
198      * @return name/long option
199      */

200     public final String JavaDoc getName()
201     {
202         return m_name;
203     }
204
205     /**
206      * Convert to String.
207      *
208      * @return the converted value to string.
209      */

210     public final String JavaDoc toString()
211     {
212         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
213         sb.append( "[OptionDescriptor " );
214         sb.append( m_name );
215         sb.append( "[OptionDescriptor " );
216         sb.append( m_name );
217         sb.append( ", " );
218         sb.append( m_id );
219         sb.append( ", " );
220         sb.append( m_flags );
221         sb.append( ", " );
222         sb.append( m_description );
223         sb.append( " ]" );
224         return sb.toString();
225     }
226 }
227
Popular Tags