tclap 1.2.6
UnlabeledValueArg.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3
4/******************************************************************************
5 *
6 * file: UnlabeledValueArg.h
7 *
8 * Copyright (c) 2003, Michael E. Smoot .
9 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
10 * Copyright (c) 2017, Google LLC
11 * All rights reserved.
12 *
13 * See the file COPYING in the top directory of this distribution for
14 * more information.
15 *
16 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 *****************************************************************************/
25
26
27#ifndef TCLAP_UNLABELED_VALUE_ARGUMENT_H
28#define TCLAP_UNLABELED_VALUE_ARGUMENT_H
29
30#include <string>
31#include <vector>
32
33#include <tclap/ValueArg.h>
35
36
37namespace TCLAP {
38
45template<class T>
46class UnlabeledValueArg : public ValueArg<T>
47{
48
49 // If compiler has two stage name lookup (as gcc >= 3.4 does)
50 // this is required to prevent undef. symbols
51 using ValueArg<T>::_ignoreable;
52 using ValueArg<T>::_hasBlanks;
54 using ValueArg<T>::_typeDesc;
55 using ValueArg<T>::_name;
57 using ValueArg<T>::_alreadySet;
58 using ValueArg<T>::toString;
59
60 public:
61 virtual bool hasLabel() const { return false; }
62
63
85 UnlabeledValueArg( const std::string& name,
86 const std::string& desc,
87 bool req,
88 T value,
89 const std::string& typeDesc,
90 bool ignoreable = false,
91 Visitor* v = NULL);
92
115 UnlabeledValueArg( const std::string& name,
116 const std::string& desc,
117 bool req,
118 T value,
119 const std::string& typeDesc,
120 CmdLineInterface& parser,
121 bool ignoreable = false,
122 Visitor* v = NULL );
123
143 UnlabeledValueArg( const std::string& name,
144 const std::string& desc,
145 bool req,
146 T value,
147 Constraint<T>* constraint,
148 bool ignoreable = false,
149 Visitor* v = NULL );
150
151
172 UnlabeledValueArg( const std::string& name,
173 const std::string& desc,
174 bool req,
175 T value,
176 Constraint<T>* constraint,
177 CmdLineInterface& parser,
178 bool ignoreable = false,
179 Visitor* v = NULL);
180
189 virtual bool processArg(int* i, std::vector<std::string>& args);
190
194 virtual std::string shortID(const std::string& val="val") const;
195
199 virtual std::string longID(const std::string& val="val") const;
200
204 virtual bool operator==(const Arg& a ) const;
205
210 virtual void addToList( std::list<Arg*>& argList ) const;
211
212};
213
217template<class T>
219 const std::string& desc,
220 bool req,
221 T val,
222 const std::string& typeDesc,
223 bool ignoreable,
224 Visitor* v)
225: ValueArg<T>("", name, desc, req, val, typeDesc, v)
226{
227 _ignoreable = ignoreable;
228
230
231}
232
233template<class T>
235 const std::string& desc,
236 bool req,
237 T val,
238 const std::string& typeDesc,
239 CmdLineInterface& parser,
240 bool ignoreable,
241 Visitor* v)
242: ValueArg<T>("", name, desc, req, val, typeDesc, v)
243{
244 _ignoreable = ignoreable;
246 parser.add( this );
247}
248
252template<class T>
254 const std::string& desc,
255 bool req,
256 T val,
257 Constraint<T>* constraint,
258 bool ignoreable,
259 Visitor* v)
260: ValueArg<T>("", name, desc, req, val, constraint, v)
261{
262 _ignoreable = ignoreable;
264}
265
266template<class T>
268 const std::string& desc,
269 bool req,
270 T val,
271 Constraint<T>* constraint,
272 CmdLineInterface& parser,
273 bool ignoreable,
274 Visitor* v)
275: ValueArg<T>("", name, desc, req, val, constraint, v)
276{
277 _ignoreable = ignoreable;
279 parser.add( this );
280}
281
285template<class T>
286bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string>& args)
287{
288
289 if ( _alreadySet )
290 return false;
291
292 if ( _hasBlanks( args[*i] ) )
293 return false;
294
295 // never ignore an unlabeled arg
296
297 _extractValue( args[*i] );
298 _alreadySet = true;
299 this->_checkWithVisitor();
300 return true;
301}
302
306template<class T>
307std::string UnlabeledValueArg<T>::shortID(const std::string& val) const
308{
309 static_cast<void>(val); // Ignore input, don't warn
310 return std::string("<") + _typeDesc + ">";
311}
312
316template<class T>
317std::string UnlabeledValueArg<T>::longID(const std::string& val) const
318{
319 static_cast<void>(val); // Ignore input, don't warn
320
321 // Ideally we would like to be able to use RTTI to return the name
322 // of the type required for this argument. However, g++ at least,
323 // doesn't appear to return terribly useful "names" of the types.
324 return std::string("<") + _typeDesc + ">";
325}
326
330template<class T>
332{
333 if ( _name == a.getName() || _description == a.getDescription() )
334 return true;
335 else
336 return false;
337}
338
339template<class T>
340void UnlabeledValueArg<T>::addToList( std::list<Arg*>& argList ) const
341{
342 argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
343}
344
345}
346#endif
A virtual base class that defines the essential data for all arguments.
Definition Arg.h:56
bool _hasBlanks(const std::string &s) const
Checks whether a given string has blank chars, indicating that it is a combined SwitchArg.
Definition Arg.h:638
bool _alreadySet
Indicates whether the argument has been set.
Definition Arg.h:128
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition Arg.h:141
std::string _description
Description of the argument.
Definition Arg.h:103
const std::string & getName() const
Returns the argument name.
Definition Arg.h:566
std::string getDescription() const
Returns the argument description.
Definition Arg.h:551
std::string _name
A single word namd identifying the argument.
Definition Arg.h:98
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition Arg.h:596
The base class that manages the command line definition and passes along the parsing to the appropria...
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
The interface that defines the interaction between the Arg and Constraint.
Definition Constraint.h:43
static void check(bool req, const std::string &argName)
The basic unlabeled argument that parses a value.
virtual std::string longID(const std::string &val="val") const
Overrides longID for specific behavior.
virtual std::string shortID(const std::string &val="val") const
Overrides shortID for specific behavior.
virtual void addToList(std::list< Arg * > &argList) const
Instead of pushing to the front of list, push to the back.
virtual bool hasLabel() const
Whether this argument has an option label rather than being an operand.
UnlabeledValueArg(const std::string &name, const std::string &desc, bool req, T value, const std::string &typeDesc, bool ignoreable=false, Visitor *v=NULL)
UnlabeledValueArg constructor.
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
virtual bool operator==(const Arg &a) const
Overrides operator== for specific behavior.
The basic labeled argument that parses a value.
Definition ValueArg.h:47
std::string _typeDesc
A human readable description of the type to be parsed.
Definition ValueArg.h:70
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition ValueArg.h:407
A base class that defines the interface for visitors.
Definition Visitor.h:35
Definition Arg.h:48