tclap 1.4.0
UnlabeledMultiArg.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: UnlabeledMultiArg.h
6 *
7 * Copyright (c) 2003, Michael E. Smoot.
8 * Copyright (c) 2017, Google LLC
9 * All rights reserved.
10 *
11 * See the file COPYING in the top directory of this distribution for
12 * more information.
13 *
14 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *
22 *****************************************************************************/
23
24#ifndef TCLAP_UNLABELED_MULTI_ARG_H
25#define TCLAP_UNLABELED_MULTI_ARG_H
26
27#include <tclap/MultiArg.h>
29
30#include <list>
31#include <string>
32#include <vector>
33
34namespace TCLAP {
35
41template <class T>
42class UnlabeledMultiArg : public MultiArg<T> {
43 // If compiler has two stage name lookup (as gcc >= 3.4 does)
44 // this is required to prevent undef. symbols
45 using MultiArg<T>::_ignoreable;
46 using MultiArg<T>::_hasBlanks;
48 using MultiArg<T>::_typeDesc;
49 using MultiArg<T>::_name;
51 using MultiArg<T>::_alreadySet;
52 using MultiArg<T>::_setBy;
53 using MultiArg<T>::toString;
54
55public:
73 UnlabeledMultiArg(const std::string &name, const std::string &desc,
74 bool req, const std::string &typeDesc,
75 bool ignoreable = false, Visitor *v = NULL);
94 UnlabeledMultiArg(const std::string &name, const std::string &desc,
95 bool req, const std::string &typeDesc,
96 ArgContainer &parser, bool ignoreable = false,
97 Visitor *v = NULL);
98
114 UnlabeledMultiArg(const std::string &name, const std::string &desc,
115 bool req, const Constraint<T> *constraint,
116 bool ignoreable = false, Visitor *v = NULL);
117
134 UnlabeledMultiArg(const std::string &name, const std::string &desc,
135 bool req, const Constraint<T> *constraint,
136 ArgContainer &parser, bool ignoreable = false,
137 Visitor *v = NULL);
138
147 virtual bool processArg(int *i, std::vector<std::string> &args);
148
152 virtual std::string shortID(const std::string &) const {
153 return Arg::getName() + " ...";
154 }
155
160 virtual std::string longID(const std::string &) const {
161 return Arg::getName() + " (accepted multiple times) <" + _typeDesc +
162 ">";
163 }
164
169 virtual bool operator==(const Arg &a) const;
170
175 virtual void addToList(std::list<Arg *> &argList) const;
176
177 virtual bool hasLabel() const { return false; }
178};
179
180template <class T>
182 const std::string &desc, bool req,
183 const std::string &typeDesc,
184 bool ignoreable, Visitor *v)
185 : MultiArg<T>("", name, desc, req, typeDesc, v) {
186 _ignoreable = ignoreable;
187}
188
189template <class T>
191 const std::string &desc, bool req,
192 const std::string &typeDesc,
193 ArgContainer &parser, bool ignoreable,
194 Visitor *v)
195 : MultiArg<T>("", name, desc, req, typeDesc, v) {
196 _ignoreable = ignoreable;
197 parser.add(this);
198}
199
200template <class T>
202 const std::string &desc, bool req,
203 const Constraint<T> *constraint,
204 bool ignoreable, Visitor *v)
205 : MultiArg<T>("", name, desc, req, constraint, v) {
206 _ignoreable = ignoreable;
207}
208
209template <class T>
211 const std::string &desc, bool req,
212 const Constraint<T> *constraint,
213 ArgContainer &parser, bool ignoreable,
214 Visitor *v)
215 : MultiArg<T>("", name, desc, req, constraint, v) {
216 _ignoreable = ignoreable;
217 parser.add(this);
218}
219
220template <class T>
221bool UnlabeledMultiArg<T>::processArg(int *i, std::vector<std::string> &args) {
222 if (_hasBlanks(args[*i])) return false;
223
224 // always take the first value, regardless of the start string
225 _extractValue(args[(*i)]);
226
227 _alreadySet = true;
228 _setBy = args[*i];
229 this->_checkWithVisitor();
230 return true;
231}
232
233template <class T>
235 return _name == a.getName() ||
236 this->getDescription() == a.getDescription();
237}
238
239template <class T>
240void UnlabeledMultiArg<T>::addToList(std::list<Arg *> &argList) const {
241 argList.push_back(const_cast<Arg *>(static_cast<const Arg *const>(this)));
242}
243} // namespace TCLAP
244
245#endif // TCLAP_UNLABELED_MULTI_ARG_H
Interface that allows adding an Arg to a "container".
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
A virtual base class that defines the essential data for all arguments.
Definition Arg.h:66
std::string _setBy
Indicates the value specified to set this flag (like -a or –all).
Definition Arg.h:162
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:799
bool _alreadySet
Indicates whether the argument has been set.
Definition Arg.h:158
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition Arg.h:175
std::string _description
Description of the argument.
Definition Arg.h:133
const std::string & getName() const
Returns the argument name.
Definition Arg.h:745
std::string getDescription() const
Returns the argument description.
Definition Arg.h:328
std::string _name
A single word namd identifying the argument.
Definition Arg.h:128
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition Arg.h:765
The interface that defines the interaction between the Arg and Constraint.
Definition Constraint.h:41
An argument that allows multiple values of type T to be specified.
Definition MultiArg.h:41
std::string _typeDesc
The description of type T to be used in the usage.
Definition MultiArg.h:56
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition MultiArg.h:328
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 longID(const std::string &) const
Returns the a long 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 bool hasLabel() const
virtual bool operator==(const Arg &a) const
Operator ==.
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
virtual std::string shortID(const std::string &) const
Returns the a short id string.
A base class that defines the interface for visitors.
Definition Visitor.h:32
Definition Arg.h:52