tclap: UnlabeledValueArg.h Source File

tclap 1.2.5

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>
34 #include <tclap/OptionalUnlabeledTracker.h>
35
36
37 namespace TCLAP {
38
45 template<class T>
46 class 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;
53 using ValueArg<T>::_extractValue;
54 using ValueArg<T>::_typeDesc;
55 using ValueArg<T>::_name;
56 using ValueArg<T>::_description;
57 using ValueArg<T>::_alreadySet;
58 using ValueArg<T>::toString;
59
60 public:
61
83 UnlabeledValueArg( const std::string& name,
84 const std::string& desc,
85 bool req,
86 T value,
87 const std::string& typeDesc,
88 bool ignoreable = false,
89 Visitor* v = NULL);
90
113 UnlabeledValueArg( const std::string& name,
114 const std::string& desc,
115 bool req,
116 T value,
117 const std::string& typeDesc,
118 CmdLineInterface& parser,
119 bool ignoreable = false,
120 Visitor* v = NULL );
121
141 UnlabeledValueArg( const std::string& name,
142 const std::string& desc,
143 bool req,
144 T value,
145 Constraint<T>* constraint,
146 bool ignoreable = false,
147 Visitor* v = NULL );
148
149
170 UnlabeledValueArg( const std::string& name,
171 const std::string& desc,
172 bool req,
173 T value,
174 Constraint<T>* constraint,
175 CmdLineInterface& parser,
176 bool ignoreable = false,
177 Visitor* v = NULL);
178
187 virtual bool processArg(int* i, std::vector<std::string>& args);
188
192 virtual std::string shortID(const std::string& val="val") const;
193
197 virtual std::string longID(const std::string& val="val") const;
198
202 virtual bool operator==(const Arg& a ) const;
203
208 virtual void addToList( std::list<Arg*>& argList ) const;
209
210 };
211
215 template<class T>
216 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
217 const std::string& desc,
218 bool req,
219 T val,
220 const std::string& typeDesc,
221 bool ignoreable,
222 Visitor* v)
223 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
224 {
225 _ignoreable = ignoreable;
226
227 OptionalUnlabeledTracker::check(req, toString());
228
229 }
230
231 template<class T>
232 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
233 const std::string& desc,
234 bool req,
235 T val,
236 const std::string& typeDesc,
237 CmdLineInterface& parser,
238 bool ignoreable,
239 Visitor* v)
240 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
241 {
242 _ignoreable = ignoreable;
243 OptionalUnlabeledTracker::check(req, toString());
244 parser.add( this );
245 }
246
250 template<class T>
251 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
252 const std::string& desc,
253 bool req,
254 T val,
255 Constraint<T>* constraint,
256 bool ignoreable,
257 Visitor* v)
258 : ValueArg<T>("", name, desc, req, val, constraint, v)
259 {
260 _ignoreable = ignoreable;
261 OptionalUnlabeledTracker::check(req, toString());
262 }
263
264 template<class T>
265 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
266 const std::string& desc,
267 bool req,
268 T val,
269 Constraint<T>* constraint,
270 CmdLineInterface& parser,
271 bool ignoreable,
272 Visitor* v)
273 : ValueArg<T>("", name, desc, req, val, constraint, v)
274 {
275 _ignoreable = ignoreable;
276 OptionalUnlabeledTracker::check(req, toString());
277 parser.add( this );
278 }
279
283 template<class T>
284 bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string>& args)
285 {
286
287 if ( _alreadySet )
288 return false;
289
290 if ( _hasBlanks( args[*i] ) )
291 return false;
292
293 // never ignore an unlabeled arg
294
295 _extractValue( args[*i] );
296 _alreadySet = true;
297 return true;
298 }
299
303 template<class T>
304 std::string UnlabeledValueArg<T>::shortID(const std::string& val) const
305 {
306 static_cast<void>(val); // Ignore input, don't warn
307 return std::string("<") + _typeDesc + ">";
308 }
309
313 template<class T>
314 std::string UnlabeledValueArg<T>::longID(const std::string& val) const
315 {
316 static_cast<void>(val); // Ignore input, don't warn
317
318 // Ideally we would like to be able to use RTTI to return the name
319 // of the type required for this argument. However, g++ at least,
320 // doesn't appear to return terribly useful "names" of the types.
321 return std::string("<") + _typeDesc + ">";
322 }
323
327 template<class T>
328 bool UnlabeledValueArg<T>::operator==(const Arg& a ) const
329 {
330 if ( _name == a.getName() || _description == a.getDescription() )
331 return true;
332 else
333 return false;
334 }
335
336 template<class T>
337 void UnlabeledValueArg<T>::addToList( std::list<Arg*>& argList ) const
338 {
339 argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
340 }
341
342 }
343 #endif
OptionalUnlabeledTracker.h
ValueArg.h
TCLAP::Arg
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:56
TCLAP::Arg::_ignoreable
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:141
TCLAP::Arg::getName
const std::string & getName() const
Returns the argument name.
Definition: Arg.h:560
TCLAP::Arg::getDescription
std::string getDescription() const
Returns the argument description.
Definition: Arg.h:545
TCLAP::Arg::toString
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:590
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:47
TCLAP::CmdLineInterface::add
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
TCLAP::Constraint
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:43
TCLAP::OptionalUnlabeledTracker::check
static void check(bool req, const std::string &argName)
Definition: OptionalUnlabeledTracker.h:50
TCLAP::UnlabeledValueArg
The basic unlabeled argument that parses a value.
Definition: UnlabeledValueArg.h:47
TCLAP::UnlabeledValueArg::longID
virtual std::string longID(const std::string &val="val") const
Overrides longID for specific behavior.
Definition: UnlabeledValueArg.h:314
TCLAP::UnlabeledValueArg::shortID
virtual std::string shortID(const std::string &val="val") const
Overrides shortID for specific behavior.
Definition: UnlabeledValueArg.h:304
TCLAP::UnlabeledValueArg::addToList
virtual void addToList(std::list< Arg * > &argList) const
Instead of pushing to the front of list, push to the back.
Definition: UnlabeledValueArg.h:337
TCLAP::UnlabeledValueArg::UnlabeledValueArg
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.
Definition: UnlabeledValueArg.h:216
TCLAP::UnlabeledValueArg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: UnlabeledValueArg.h:284
TCLAP::UnlabeledValueArg::operator==
virtual bool operator==(const Arg &a) const
Overrides operator== for specific behavior.
Definition: UnlabeledValueArg.h:328
TCLAP::ValueArg
The basic labeled argument that parses a value.
Definition: ValueArg.h:47
TCLAP::Visitor
A base class that defines the interface for visitors.
Definition: Visitor.h:35
TCLAP
Definition: Arg.h:48