KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > command > lib > OptionMultipleArguments


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@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): Christophe Demarey.
23 Contributor(s): Philippe Merle.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.command.lib;
28
29 // Package dependencies.
30
import org.objectweb.util.cmdline.lib.DefaultOptionArgument;
31
32 /**
33  * This is a default implementation for options that can be specified more
34  * that once. Moreover this class can consume one or two arguments of the
35  * command line.
36  * ex: -UNAME => Remove any definition for NAME
37  *
38  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
39  *
40  * @version 0.2
41  */

42
43 public class OptionMultipleArguments
44      extends DefaultOptionArgument
45 {
46     // ==================================================================
47
//
48
// Internal state.
49
//
50
// ==================================================================
51

52     /** The option identifier. */
53     protected String JavaDoc id_;
54
55     /** Consume another argument? */
56     protected boolean consume_;
57
58     /** Can one have twice the same option? */
59     protected boolean multiple_;
60
61     /** List of arguments declared with this option. */
62     protected java.util.List JavaDoc arguments_;
63
64     // ==================================================================
65
//
66
// Constructor.
67
//
68
// ==================================================================
69

70     /**
71      * The constructor with the initial values for the Labels,
72      * Arguments, and Description usage properties.
73      *
74      * @param id - The option identifier.
75      * @param labels - The usage labels.
76      * @param arguments - The usage arguments.
77      * @param description - The usage description.
78      * @param argument - The initial argument value.
79      * @param multiple - Can one have twice the same option?
80      */

81     public
82     OptionMultipleArguments(String JavaDoc id,
83                             String JavaDoc[] labels,
84                             String JavaDoc arguments,
85                             String JavaDoc[] description,
86                             String JavaDoc argument,
87                             boolean multiple )
88     {
89         super(labels, arguments, description, argument);
90         id_ = id;
91         multiple_ = multiple;
92         consume_ = true;
93         arguments_ = new java.util.ArrayList JavaDoc();
94     }
95
96     /**
97      * The constructor with the initial values for the Labels,
98      * Arguments, and Description usage properties.
99      *
100      * @param id - The option identifier.
101      * @param labels - The usage labels.
102      * @param arguments - The usage arguments.
103      * @param description - The usage description.
104      * @param argument - The initial argument value.
105      * @param multiple - Can one have twice the same option?
106      * @param consume - Consume one more argument from the command line?
107      */

108     public
109     OptionMultipleArguments(String JavaDoc id,
110                             String JavaDoc[] labels,
111                             String JavaDoc arguments,
112                             String JavaDoc[] description,
113                             String JavaDoc argument,
114                             boolean multiple,
115                             boolean consume )
116     {
117         super(labels, arguments, description, argument);
118         id_ = id;
119         multiple_ = multiple;
120         consume_ = consume;
121         arguments_ = new java.util.ArrayList JavaDoc();
122     }
123
124     // ==================================================================
125
//
126
// Internal methods.
127
//
128
// ==================================================================
129

130     // ==================================================================
131
//
132
// Internal methods for class org.objectweb.util.lib.DefaultPrintableBase
133
//
134
// ==================================================================
135

136     // ==================================================================
137
//
138
// Public methods for interface org.objectweb.util.cmdline.api.Option
139
//
140
// ==================================================================
141

142     /**
143      * Checks the current command line argument.
144      *
145      * @param current The current command line argument.
146      *
147      * @return true if the option accepts this argument.
148      */

149     public boolean
150     check(String JavaDoc current)
151     {
152         if ( current.equals(id_) )
153             return true;
154
155         if ( (current.length()>id_.length()) && (current.startsWith(id_)) && (!consume_))
156         {
157             setArgument(current.substring(2));
158             arguments_.add( getArgument() );
159             return true;
160         }
161         return false;
162     }
163
164     /**
165      * Consumes command line arguments from an iterator.
166      *
167      * @param iterator The command line argument iterator.
168      */

169     public void
170     consume(org.objectweb.util.cmdline.api.Iterator iterator)
171     {
172         if (!multiple_)
173             checkAlreadySet(iterator);
174         if (consume_)
175             arguments_.add(consumeArgument(iterator));
176     }
177
178     // ==================================================================
179
//
180
// Other public methods.
181
//
182
// ==================================================================
183

184     /**
185      * Get all arguments declared with this option.
186      *
187      * @return An array of arguments.
188      */

189     public String JavaDoc[]
190     getOptionArguments()
191     {
192         return (String JavaDoc[])arguments_.toArray(new String JavaDoc[0]);
193     }
194
195 }
196
Popular Tags