]> Creatis software - cpPlugins.git/blob - lib/tclap/UnlabeledValueArg.h
...
[cpPlugins.git] / lib / tclap / UnlabeledValueArg.h
1
2 /****************************************************************************** 
3  * 
4  *  file:  UnlabeledValueArg.h
5  * 
6  *  Copyright (c) 2003, Michael E. Smoot .
7  *  Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
8  *  All rights reserved.
9  * 
10  *  See the file COPYING in the top directory of this distribution for
11  *  more information.
12  *  
13  *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 
14  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
15  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
16  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
17  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
18  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
19  *  DEALINGS IN THE SOFTWARE.  
20  *  
21  *****************************************************************************/ 
22
23
24 #ifndef TCLAP_UNLABELED_VALUE_ARGUMENT_H
25 #define TCLAP_UNLABELED_VALUE_ARGUMENT_H
26
27 #include <string>
28 #include <vector>
29
30 #include <tclap/ValueArg.h>
31 #include <tclap/OptionalUnlabeledTracker.h>
32
33
34 namespace TCLAP {
35
36 /**
37  * The basic unlabeled argument that parses a value.
38  * This is a template class, which means the type T defines the type
39  * that a given object will attempt to parse when an UnlabeledValueArg
40  * is reached in the list of args that the CmdLine iterates over.
41  */
42 template<class T>
43 class UnlabeledValueArg : public ValueArg<T>
44 {
45
46         // If compiler has two stage name lookup (as gcc >= 3.4 does)
47         // this is required to prevent undef. symbols
48         using ValueArg<T>::_ignoreable;
49         using ValueArg<T>::_hasBlanks;
50         using ValueArg<T>::_extractValue;
51         using ValueArg<T>::_typeDesc;
52         using ValueArg<T>::_name;
53         using ValueArg<T>::_description;
54         using ValueArg<T>::_alreadySet;
55         using ValueArg<T>::toString;
56
57         public:
58
59                 /**
60                  * UnlabeledValueArg constructor.
61                  * \param name - A one word name for the argument.  Note that this is used for
62                  * identification, not as a long flag.
63                  * \param desc - A description of what the argument is for or
64                  * does.
65                  * \param req - Whether the argument is required on the command
66                  * line.
67                  * \param value - The default value assigned to this argument if it
68                  * is not present on the command line.
69                  * \param typeDesc - A short, human readable description of the
70                  * type that this object expects.  This is used in the generation
71                  * of the USAGE statement.  The goal is to be helpful to the end user
72                  * of the program.
73                  * \param ignoreable - Allows you to specify that this argument can be
74                  * ignored if the '--' flag is set.  This defaults to false (cannot
75                  * be ignored) and should  generally stay that way unless you have 
76                  * some special need for certain arguments to be ignored.
77                  * \param v - Optional Visitor.  You should leave this blank unless
78                  * you have a very good reason.
79                  */
80                 UnlabeledValueArg( const std::string& name, 
81                                        const std::string& desc, 
82                                                    bool req,
83                                            T value,
84                                            const std::string& typeDesc,
85                                                    bool ignoreable = false,
86                                            Visitor* v = NULL); 
87
88                 /**
89                  * UnlabeledValueArg constructor.
90                  * \param name - A one word name for the argument.  Note that this is used for
91                  * identification, not as a long flag.
92                  * \param desc - A description of what the argument is for or
93                  * does.
94                  * \param req - Whether the argument is required on the command
95                  * line.
96                  * \param value - The default value assigned to this argument if it
97                  * is not present on the command line.
98                  * \param typeDesc - A short, human readable description of the
99                  * type that this object expects.  This is used in the generation
100                  * of the USAGE statement.  The goal is to be helpful to the end user
101                  * of the program.
102                  * \param parser - A CmdLine parser object to add this Arg to
103                  * \param ignoreable - Allows you to specify that this argument can be
104                  * ignored if the '--' flag is set.  This defaults to false (cannot
105                  * be ignored) and should  generally stay that way unless you have 
106                  * some special need for certain arguments to be ignored.
107                  * \param v - Optional Visitor.  You should leave this blank unless
108                  * you have a very good reason.
109                  */
110                 UnlabeledValueArg( const std::string& name, 
111                                        const std::string& desc, 
112                                                    bool req,
113                                            T value,
114                                            const std::string& typeDesc,
115                                                    CmdLineInterface& parser,
116                                                    bool ignoreable = false,
117                                            Visitor* v = NULL );                                         
118                                                 
119                 /**
120                  * UnlabeledValueArg constructor.
121                  * \param name - A one word name for the argument.  Note that this is used for
122                  * identification, not as a long flag.
123                  * \param desc - A description of what the argument is for or
124                  * does.
125                  * \param req - Whether the argument is required on the command
126                  * line.
127                  * \param value - The default value assigned to this argument if it
128                  * is not present on the command line.
129                  * \param constraint - A pointer to a Constraint object used
130                  * to constrain this Arg.
131                  * \param ignoreable - Allows you to specify that this argument can be
132                  * ignored if the '--' flag is set.  This defaults to false (cannot
133                  * be ignored) and should  generally stay that way unless you have 
134                  * some special need for certain arguments to be ignored.
135                  * \param v - Optional Visitor.  You should leave this blank unless
136                  * you have a very good reason.
137                  */
138                 UnlabeledValueArg( const std::string& name, 
139                                        const std::string& desc, 
140                                                    bool req,
141                                            T value,
142                                            Constraint<T>* constraint,
143                                                    bool ignoreable = false,
144                                            Visitor* v = NULL ); 
145
146                 
147                 /**
148                  * UnlabeledValueArg constructor.
149                  * \param name - A one word name for the argument.  Note that this is used for
150                  * identification, not as a long flag.
151                  * \param desc - A description of what the argument is for or
152                  * does.
153                  * \param req - Whether the argument is required on the command
154                  * line.
155                  * \param value - The default value assigned to this argument if it
156                  * is not present on the command line.
157                  * \param constraint - A pointer to a Constraint object used
158                  * to constrain this Arg.
159                  * \param parser - A CmdLine parser object to add this Arg to
160                  * \param ignoreable - Allows you to specify that this argument can be
161                  * ignored if the '--' flag is set.  This defaults to false (cannot
162                  * be ignored) and should  generally stay that way unless you have 
163                  * some special need for certain arguments to be ignored.
164                  * \param v - Optional Visitor.  You should leave this blank unless
165                  * you have a very good reason.
166                  */
167                 UnlabeledValueArg( const std::string& name, 
168                                        const std::string& desc, 
169                                                    bool req,
170                                            T value,
171                                            Constraint<T>* constraint,
172                                                    CmdLineInterface& parser,
173                                                    bool ignoreable = false,
174                                            Visitor* v = NULL);
175                                                 
176                 /**
177                  * Handles the processing of the argument.
178                  * This re-implements the Arg version of this method to set the
179                  * _value of the argument appropriately.  Handling specific to
180                  * unlabeled arguments.
181                  * \param i - Pointer the the current argument in the list.
182                  * \param args - Mutable list of strings. 
183                  */
184                 virtual bool processArg(int* i, std::vector<std::string>& args); 
185
186                 /**
187                  * Overrides shortID for specific behavior.
188                  */
189                 virtual std::string shortID(const std::string& val="val") const;
190
191                 /**
192                  * Overrides longID for specific behavior.
193                  */
194                 virtual std::string longID(const std::string& val="val") const;
195
196                 /**
197                  * Overrides operator== for specific behavior.
198                  */
199                 virtual bool operator==(const Arg& a ) const;
200
201                 /**
202                  * Instead of pushing to the front of list, push to the back.
203                  * \param argList - The list to add this to.
204                  */
205                 virtual void addToList( std::list<Arg*>& argList ) const;
206
207 };
208
209 /**
210  * Constructor implementation.
211  */
212 template<class T>
213 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
214                                                             const std::string& desc, 
215                                                                                 bool req,
216                                                             T val,
217                                                             const std::string& typeDesc,
218                                                             bool ignoreable,
219                                                             Visitor* v)
220 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
221
222         _ignoreable = ignoreable;
223
224         OptionalUnlabeledTracker::check(req, toString());
225
226 }
227
228 template<class T>
229 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
230                                                             const std::string& desc, 
231                                                                                 bool req,
232                                                             T val,
233                                                             const std::string& typeDesc,
234                                                             CmdLineInterface& parser,
235                                                             bool ignoreable,
236                                                             Visitor* v)
237 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
238
239         _ignoreable = ignoreable;
240         OptionalUnlabeledTracker::check(req, toString());
241         parser.add( this );
242 }
243
244 /**
245  * Constructor implementation.
246  */
247 template<class T>
248 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
249                                         const std::string& desc, 
250                                                                                 bool req,
251                                         T val,
252                                         Constraint<T>* constraint,
253                                         bool ignoreable,
254                                         Visitor* v)
255 : ValueArg<T>("", name, desc, req, val, constraint, v)
256
257         _ignoreable = ignoreable;
258         OptionalUnlabeledTracker::check(req, toString());
259 }
260
261 template<class T>
262 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
263                                                             const std::string& desc, 
264                                                                                 bool req,
265                                                             T val,
266                                                             Constraint<T>* constraint,
267                                                             CmdLineInterface& parser,
268                                                             bool ignoreable,
269                                                             Visitor* v)
270 : ValueArg<T>("", name, desc, req, val, constraint,  v)
271
272         _ignoreable = ignoreable;
273         OptionalUnlabeledTracker::check(req, toString());
274         parser.add( this );
275 }
276
277 /**
278  * Implementation of processArg().
279  */
280 template<class T>
281 bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string>& args) 
282 {
283         
284         if ( _alreadySet )
285                 return false;
286         
287         if ( _hasBlanks( args[*i] ) )
288                 return false;
289
290         // never ignore an unlabeled arg
291         
292         _extractValue( args[*i] );
293         _alreadySet = true;
294         return true;
295 }
296
297 /**
298  * Overriding shortID for specific output.
299  */
300 template<class T>
301 std::string UnlabeledValueArg<T>::shortID(const std::string& val) const
302 {
303         static_cast<void>(val); // Ignore input, don't warn
304         return std::string("<") + _typeDesc + ">";
305 }
306
307 /**
308  * Overriding longID for specific output.
309  */
310 template<class T>
311 std::string UnlabeledValueArg<T>::longID(const std::string& val) const
312 {
313         static_cast<void>(val); // Ignore input, don't warn
314
315         // Ideally we would like to be able to use RTTI to return the name
316         // of the type required for this argument.  However, g++ at least, 
317         // doesn't appear to return terribly useful "names" of the types.  
318         return std::string("<") + _typeDesc + ">";
319 }
320
321 /**
322  * Overriding operator== for specific behavior.
323  */
324 template<class T>
325 bool UnlabeledValueArg<T>::operator==(const Arg& a ) const
326 {
327         if ( _name == a.getName() || _description == a.getDescription() )
328                 return true;
329         else
330                 return false;
331 }
332
333 template<class T>
334 void UnlabeledValueArg<T>::addToList( std::list<Arg*>& argList ) const
335 {
336         argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
337 }
338
339 }
340 #endif