KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > cmdline > lib > DefaultOptionBase


1 /*====================================================================
2
3 ObjectWeb Util CommandLine Package.
4 Copyright (C) 2003 INRIA & USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.cmdline.lib;
28
29 import org.objectweb.util.cmdline.api.Iterator;
30 import org.objectweb.util.cmdline.api.Option;
31
32 /**
33  * This is a default abstract implementation of the
34  * org.objectweb.util.cmdline.api.Option interface.
35  *
36  * Subclasses must implement the consume method.
37  *
38  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
39  *
40  * @version 0.1
41  */

42
43 public abstract class DefaultOptionBase
44         extends DefaultUsage
45      implements Option
46 {
47     // ==================================================================
48
//
49
// Internal state.
50
//
51
// ==================================================================
52

53     /** The mandatory flag. */
54     protected boolean mandatory_;
55
56     /** The set flag. */
57     protected boolean set_;
58
59     // ==================================================================
60
//
61
// Constructors.
62
//
63
// ==================================================================
64

65     /**
66      * The default constructor.
67      *
68      * It inits the Labels, Arguments, and Description usage properties
69      * with an empty string.
70      */

71     public
72     DefaultOptionBase()
73     {
74         this("", new String JavaDoc[0], null, false);
75     }
76
77     /**
78      * The constructor with the initial values for the Labels,
79      * Arguments, and Description usage properties.
80      *
81      * @param label The usage label.
82      * @param argument The usage argument.
83      * @param description The usage description.
84      * @param is_mandatory True if a mandatory option.
85      */

86     public
87     DefaultOptionBase(String JavaDoc label,
88                       String JavaDoc argument,
89                       String JavaDoc[] description)
90     {
91         this(label, new String JavaDoc[] { argument }, description, false);
92     }
93
94     /**
95      * The constructor with the initial values for the Labels,
96      * Arguments, and Description usage properties.
97      *
98      * @param label The usage label.
99      * @param arguments The usage arguments.
100      * @param description The usage description.
101      * @param is_mandatory True if a mandatory option.
102      */

103     public
104     DefaultOptionBase(String JavaDoc label,
105                       String JavaDoc[] arguments,
106                       String JavaDoc[] description)
107     {
108         this(label, arguments, description, false);
109     }
110
111     /**
112      * The constructor with the initial values for the Labels,
113      * Arguments, and Description usage properties.
114      *
115      * @param labels The usage labels.
116      * @param argument The usage argument.
117      * @param description The usage description.
118      * @param is_mandatory True if a mandatory option.
119      */

120     public
121     DefaultOptionBase(String JavaDoc[] labels,
122                       String JavaDoc argument,
123                       String JavaDoc[] description)
124     {
125         this(labels, new String JavaDoc[] { argument }, description, false);
126     }
127
128     /**
129      * The constructor with the initial values for the Labels,
130      * Arguments, and Description usage properties.
131      *
132      * @param labels The usage labels.
133      * @param arguments The usage arguments.
134      * @param description The usage description.
135      * @param is_mandatory True if a mandatory option.
136      */

137     public
138     DefaultOptionBase(String JavaDoc[] labels,
139                       String JavaDoc[] arguments,
140                       String JavaDoc[] description)
141     {
142         this(labels, arguments, description, false);
143     }
144
145     /**
146      * The constructor with the initial values for the Labels,
147      * Arguments, and Description usage properties.
148      *
149      * @param labels The usage labels.
150      * @param argument The usage argument.
151      * @param description The usage description.
152      * @param is_mandatory True if a mandatory option.
153      */

154     public
155     DefaultOptionBase(String JavaDoc[] labels,
156                       String JavaDoc argument,
157                       String JavaDoc description)
158     {
159         this(labels, new String JavaDoc[] { argument },
160              new String JavaDoc[] { description }, false);
161     }
162
163     /**
164      * The constructor with the initial values for the Labels,
165      * Arguments, and Description usage properties.
166      *
167      * @param labels The usage labels.
168      * @param arguments The usage arguments.
169      * @param description The usage description.
170      * @param is_mandatory True if a mandatory option.
171      */

172     public
173     DefaultOptionBase(String JavaDoc[] labels,
174                       String JavaDoc[] arguments,
175                       String JavaDoc description)
176     {
177         this(labels, arguments, new String JavaDoc[] { description }, false);
178     }
179
180     /**
181      * The constructor with the initial values for the Labels,
182      * Arguments, and Description usage properties.
183      *
184      * @param label The usage label.
185      * @param argument The usage argument.
186      * @param description The usage description.
187      * @param mandatory True if a mandatory option.
188      */

189     public
190     DefaultOptionBase(String JavaDoc label,
191                       String JavaDoc argument,
192                       String JavaDoc[] description,
193                       boolean mandatory)
194     {
195         this(new String JavaDoc[] { label }, new String JavaDoc[] { argument },
196              description, mandatory);
197     }
198
199     /**
200      * The constructor with the initial values for the Labels,
201      * Arguments, and Description usage properties.
202      *
203      * @param label The usage label.
204      * @param arguments The usage arguments.
205      * @param description The usage description.
206      * @param mandatory True if a mandatory option.
207      */

208     public
209     DefaultOptionBase(String JavaDoc label,
210                       String JavaDoc[] arguments,
211                       String JavaDoc[] description,
212                       boolean mandatory)
213     {
214         this(new String JavaDoc[] { label }, arguments, description, mandatory);
215     }
216
217     /**
218      * The constructor with the initial values for the Labels,
219      * Arguments, and Description usage properties.
220      *
221      * @param labels The usage labels.
222      * @param argument The usage argument.
223      * @param description The usage description.
224      * @param mandatory True if a mandatory option.
225      */

226     public
227     DefaultOptionBase(String JavaDoc[] labels,
228                       String JavaDoc argument,
229                       String JavaDoc[] description,
230                       boolean mandatory)
231     {
232         this(labels, new String JavaDoc[] { argument }, description, mandatory);
233     }
234
235     /**
236      * The constructor with the initial values for the Labels,
237      * Arguments, and Description usage properties.
238      *
239      * @param labels The usage labels.
240      * @param arguments The usage arguments.
241      * @param description The usage description.
242      * @param mandatory True if a mandatory option.
243      */

244     public
245     DefaultOptionBase(String JavaDoc[] labels,
246                       String JavaDoc[] arguments,
247                       String JavaDoc[] description,
248                       boolean mandatory)
249     {
250         super(labels, arguments, description);
251         setMandatory(mandatory);
252         set_ = false;
253     }
254
255     // ==================================================================
256
//
257
// Internal methods.
258
//
259
// ==================================================================
260

261     /**
262      * Consumes a command line argument from an iterator.
263      *
264      * @param iterator The command line argument iterator.
265      *
266      * @return The consumed argument.
267      */

268     protected String JavaDoc
269     consumeArgument(Iterator iterator)
270     {
271       try {
272           String JavaDoc argument = iterator.next();
273           iterator.remove();
274           return argument;
275       } catch(java.util.NoSuchElementException JavaDoc exc) {
276           throw new Error JavaDoc("Missed argument");
277       }
278     }
279
280     /**
281      * Checks if the option was already set.
282      *
283      * @param iterator The command line argument iterator.
284      */

285     protected void
286     checkAlreadySet(org.objectweb.util.cmdline.api.Iterator iterator)
287     {
288       if(set_)
289           throw new Error JavaDoc("Twice uses");
290       set_ = true;
291     }
292
293     // ==================================================================
294
//
295
// Public methods for interface org.objectweb.util.cmdline.api.Option
296
//
297
// ==================================================================
298

299     /**
300      * Is it a mandatory option?
301      *
302      * @return true is it is a mandatory option.
303      */

304     public boolean
305     isMandatory()
306     {
307        return mandatory_;
308     }
309
310     /**
311      * Sets the mandatory option status.
312      *
313      * @param mandatory true if it is a mandatory option.
314      */

315     public void
316     setMandatory(boolean mandatory)
317     {
318        mandatory_ = mandatory;
319     }
320
321     /**
322      * Obtains the Set status of the option.
323      *
324      * @return true the option was set during
325      * the command line argument parsing.
326      */

327     public boolean
328     isSet()
329     {
330        return set_;
331     }
332
333     /**
334      * Checks the current command line argument.
335      *
336      * @param current The current command line argument.
337      *
338      * @return true if the option accepts this argument.
339      */

340     public boolean
341     check(String JavaDoc current)
342     {
343         String JavaDoc[] labels = getLabels();
344         for(int i=0; i<labels.length; i++)
345         {
346             if(current.equals(labels[i]))
347                 return true;
348         }
349
350         return false;
351     }
352
353     /**
354      * Consumes command line arguments from an iterator.
355      *
356      * @param iterator The command line argument iterator.
357      *
358      * @return true if the option has consumed arguments.
359      */

360     abstract public void
361     consume(org.objectweb.util.cmdline.api.Iterator iterator);
362
363     // ==================================================================
364
//
365
// Other public methods.
366
//
367
// ==================================================================
368
}
369
Popular Tags