tclap 1.2.6
UnlabeledMultiArg.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3
4/******************************************************************************
5 *
6 * file: UnlabeledMultiArg.h
7 *
8 * Copyright (c) 2003, Michael E. Smoot.
9 * Copyright (c) 2017, Google LLC
10 * All rights reserved.
11 *
12 * See the file COPYING in the top directory of this distribution for
13 * more information.
14 *
15 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 *****************************************************************************/
24
25
26#ifndef TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H
27#define TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H
28
29#include <string>
30#include <vector>
31
32#include <tclap/MultiArg.h>
34
35namespace TCLAP {
36
42template<class T>
43class UnlabeledMultiArg : public MultiArg<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 MultiArg<T>::_ignoreable;
49 using MultiArg<T>::_hasBlanks;
51 using MultiArg<T>::_typeDesc;
52 using MultiArg<T>::_name;
54 using MultiArg<T>::_alreadySet;
55 using MultiArg<T>::toString;
56
57 public:
58 virtual bool hasLabel() const { return false; }
59
60
78 UnlabeledMultiArg( const std::string& name,
79 const std::string& desc,
80 bool req,
81 const std::string& typeDesc,
82 bool ignoreable = false,
83 Visitor* v = NULL );
102 UnlabeledMultiArg( const std::string& name,
103 const std::string& desc,
104 bool req,
105 const std::string& typeDesc,
106 CmdLineInterface& parser,
107 bool ignoreable = false,
108 Visitor* v = NULL );
109
125 UnlabeledMultiArg( const std::string& name,
126 const std::string& desc,
127 bool req,
128 Constraint<T>* constraint,
129 bool ignoreable = false,
130 Visitor* v = NULL );
131
148 UnlabeledMultiArg( const std::string& name,
149 const std::string& desc,
150 bool req,
151 Constraint<T>* constraint,
152 CmdLineInterface& parser,
153 bool ignoreable = false,
154 Visitor* v = NULL );
155
164 virtual bool processArg(int* i, std::vector<std::string>& args);
165
170 virtual std::string shortID(const std::string& val="val") const;
171
176 virtual std::string longID(const std::string& val="val") const;
177
182 virtual bool operator==(const Arg& a) const;
183
188 virtual void addToList( std::list<Arg*>& argList ) const;
189};
190
191template<class T>
193 const std::string& desc,
194 bool req,
195 const std::string& typeDesc,
196 bool ignoreable,
197 Visitor* v)
198: MultiArg<T>("", name, desc, req, typeDesc, v)
199{
200 _ignoreable = ignoreable;
202}
203
204template<class T>
206 const std::string& desc,
207 bool req,
208 const std::string& typeDesc,
209 CmdLineInterface& parser,
210 bool ignoreable,
211 Visitor* v)
212: MultiArg<T>("", name, desc, req, typeDesc, v)
213{
214 _ignoreable = ignoreable;
216 parser.add( this );
217}
218
219
220template<class T>
222 const std::string& desc,
223 bool req,
224 Constraint<T>* constraint,
225 bool ignoreable,
226 Visitor* v)
227: MultiArg<T>("", name, desc, req, constraint, v)
228{
229 _ignoreable = ignoreable;
231}
232
233template<class T>
235 const std::string& desc,
236 bool req,
237 Constraint<T>* constraint,
238 CmdLineInterface& parser,
239 bool ignoreable,
240 Visitor* v)
241: MultiArg<T>("", name, desc, req, constraint, v)
242{
243 _ignoreable = ignoreable;
245 parser.add( this );
246}
247
248
249template<class T>
250bool UnlabeledMultiArg<T>::processArg(int *i, std::vector<std::string>& args)
251{
252
253 if ( _hasBlanks( args[*i] ) )
254 return false;
255
256 // never ignore an unlabeled multi arg
257
258
259 // always take the first value, regardless of the start string
260 _extractValue( args[(*i)] );
261
262 /*
263 // continue taking args until we hit the end or a start string
264 while ( (unsigned int)(*i)+1 < args.size() &&
265 args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
266 args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
267 _extractValue( args[++(*i)] );
268 */
269
270 _alreadySet = true;
271 this->_checkWithVisitor();
272
273 return true;
274}
275
276template<class T>
277std::string UnlabeledMultiArg<T>::shortID(const std::string& val) const
278{
279 static_cast<void>(val); // Ignore input, don't warn
280 return std::string("<") + _typeDesc + "> ...";
281}
282
283template<class T>
284std::string UnlabeledMultiArg<T>::longID(const std::string& val) const
285{
286 static_cast<void>(val); // Ignore input, don't warn
287 return std::string("<") + _typeDesc + "> (accepted multiple times)";
288}
289
290template<class T>
292{
293 if ( _name == a.getName() || _description == a.getDescription() )
294 return true;
295 else
296 return false;
297}
298
299template<class T>
300void UnlabeledMultiArg<T>::addToList( std::list<Arg*>& argList ) const
301{
302 argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
303}
304
305}
306
307#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
An argument that allows multiple values of type T to be specified.
Definition MultiArg.h:43
std::string _typeDesc
The description of type T to be used in the usage.
Definition MultiArg.h:59
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition MultiArg.h:400
static void check(bool req, const std::string &argName)
Just like a MultiArg, except that the arguments are unlabeled.
virtual void addToList(std::list< Arg * > &argList) const
Pushes this to back of list rather than front.
virtual std::string shortID(const std::string &val="val") const
Returns the a short id string.
UnlabeledMultiArg(const std::string &name, const std::string &desc, bool req, const std::string &typeDesc, bool ignoreable=false, Visitor *v=NULL)
Constructor.
virtual std::string longID(const std::string &val="val") const
Returns the a long id string.
virtual bool hasLabel() const
Whether this argument has an option label rather than being an operand.
virtual bool operator==(const Arg &a) const
Operator ==.
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
A base class that defines the interface for visitors.
Definition Visitor.h:35
Definition Arg.h:48