tclap 1.4.0
UnlabeledValueArg.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: UnlabeledValueArg.h
6 *
7 * Copyright (c) 2003, Michael E. Smoot .
8 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
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#ifndef TCLAP_UNLABELED_VALUE_ARG_H
26#define TCLAP_UNLABELED_VALUE_ARG_H
27
29#include <tclap/ValueArg.h>
30
31#include <list>
32#include <string>
33#include <vector>
34
35namespace TCLAP {
36
43template <class T>
44class UnlabeledValueArg : public ValueArg<T> {
45 // If compiler has two stage name lookup (as gcc >= 3.4 does)
46 // this is required to prevent undef. symbols
47 using ValueArg<T>::_ignoreable;
48 using ValueArg<T>::_hasBlanks;
50 using ValueArg<T>::_typeDesc;
51 using ValueArg<T>::_name;
53 using ValueArg<T>::_alreadySet;
54 using ValueArg<T>::_setBy;
55 using ValueArg<T>::toString;
56
57public:
80 UnlabeledValueArg(const std::string &name, const std::string &desc,
81 bool req, T value, const std::string &typeDesc,
82 bool ignoreable = false, Visitor *v = NULL);
83
107 UnlabeledValueArg(const std::string &name, const std::string &desc,
108 bool req, T value, const std::string &typeDesc,
109 CmdLineInterface &parser, bool ignoreable = false,
110 Visitor *v = NULL);
111
132 UnlabeledValueArg(const std::string &name, const std::string &desc,
133 bool req, T value, const Constraint<T> *constraint,
134 bool ignoreable = false, Visitor *v = NULL);
135
157 UnlabeledValueArg(const std::string &name, const std::string &desc,
158 bool req, T value, const Constraint<T> *constraint,
159 CmdLineInterface &parser, bool ignoreable = false,
160 Visitor *v = NULL);
161
170 virtual bool processArg(int *i, std::vector<std::string> &args);
171
175 virtual std::string shortID(const std::string &) const {
176 return Arg::getName();
177 }
178
182 virtual std::string longID(const std::string &) const {
183 return Arg::getName() + " <" + _typeDesc + ">";
184 }
185
189 virtual bool operator==(const Arg &a) const;
190
195 virtual void addToList(std::list<Arg *> &argList) const;
196
197 virtual bool hasLabel() const { return false; }
198};
199
203template <class T>
205 const std::string &desc, bool req,
206 T val, const std::string &typeDesc,
207 bool ignoreable, Visitor *v)
208 : ValueArg<T>("", name, desc, req, val, typeDesc, v) {
209 _ignoreable = ignoreable;
210
211}
212
213template <class T>
215 const std::string &desc, bool req,
216 T val, const std::string &typeDesc,
217 CmdLineInterface &parser,
218 bool ignoreable, Visitor *v)
219 : ValueArg<T>("", name, desc, req, val, typeDesc, v) {
220 _ignoreable = ignoreable;
221 parser.add(this);
222}
223
227template <class T>
229 const std::string &desc, bool req,
230 T val, const Constraint<T> *constraint,
231 bool ignoreable, Visitor *v)
232 : ValueArg<T>("", name, desc, req, val, constraint, v) {
233 _ignoreable = ignoreable;
234}
235
236template <class T>
238 const std::string &desc, bool req,
239 T val, const Constraint<T> *constraint,
240 CmdLineInterface &parser,
241 bool ignoreable, Visitor *v)
242 : ValueArg<T>("", name, desc, req, val, constraint, v) {
243 _ignoreable = ignoreable;
244 parser.add(this);
245}
246
250template <class T>
251bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string> &args) {
252 if (_alreadySet) return false;
253
254 if (_hasBlanks(args[*i])) return false;
255
256 _extractValue(args[*i]);
257 _alreadySet = true;
258 _setBy = args[*i];
259 this->_checkWithVisitor();
260 return true;
261}
262
266template <class T>
268 return _name == a.getName() ||
269 this->getDescription() == a.getDescription();
270}
271
272template <class T>
273void UnlabeledValueArg<T>::addToList(std::list<Arg *> &argList) const {
274 argList.push_back(const_cast<Arg *>(static_cast<const Arg *const>(this)));
275}
276} // namespace TCLAP
277
278#endif // TCLAP_UNLABELED_VALUE_ARG_H
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 base class that manages the command line definition and passes along the parsing to the appropria...
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
The interface that defines the interaction between the Arg and Constraint.
Definition Constraint.h:41
The basic unlabeled argument that parses a value.
virtual std::string longID(const std::string &) const
Overrides longID for specific behavior.
virtual std::string shortID(const std::string &) 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
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:45
std::string _typeDesc
A human readable description of the type to be parsed.
Definition ValueArg.h:67
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition ValueArg.h:337
A base class that defines the interface for visitors.
Definition Visitor.h:32
Definition Arg.h:52