Debian Codegraph @

podofo/3rdparty/tclap/ValueArg.h

raw symbols SCIP
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3 /******************************************************************************
4 *
5 * file: ValueArg.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_VALUE_ARG_H
26 #define TCLAP_VALUE_ARG_H
27
28 #include <tclap/Arg.h>
29 #include <tclap/Constraint.h>
30
31 #include <string>
32 #include <vector>
33
34 namespace TCLAP {
35
36 /**
37 * The basic labeled argument that parses a value.
38 * This is a template class, which means the type T defines the type
39 * that a given object will attempt to parse when the flag/name is matched
40 * on the command line. While there is nothing stopping you from creating
41 * an unflagged ValueArg, it is unwise and would cause significant problems.
42 * Instead use an UnlabeledValueArg.
43 */
44 template <class T>
45 class ValueArg : public Arg {
46 protected:
47 /**
48 * The value parsed from the command line.
49 * Can be of any type, as long as the >> operator for the type
50 * is defined.
51 */
52 T _value;
53
54 /**
55 * Used to support the reset() method so that ValueArg can be
56 * reset to their constructed value.
57 */
58 T _default;
59
60 /**
61 * A human readable description of the type to be parsed.
62 * This is a hack, plain and simple. Ideally we would use RTTI to
63 * return the name of type T, but until there is some sort of
64 * consistent support for human readable names, we are left to our
65 * own devices.
66 */
67 std::string _typeDesc;
68
69 /**
70 * A Constraint this Arg must conform to.
71 */
72 Constraint<T> *_constraint;
73
74 /**
75 * Extracts the value from the string.
76 * Attempts to parse string as type T, if this fails an exception
77 * is thrown.
78 * \param val - value to be parsed.
79 */
80 void _extractValue(const std::string &val);
81
82 public:
83 /**
84 * Labeled ValueArg constructor.
85 *
86 * \param flag - The one character flag that identifies this
87 * argument on the command line.
88 * \param name - A one word name for the argument. Can be
89 * used as a long flag on the command line.
90 * \param desc - A description of what the argument is for or
91 * does.
92 * \param req - Whether the argument is required on the command
93 * line.
94 * \param value - The default value assigned to this argument if it
95 * is not present on the command line.
96 * \param typeDesc - A short, human readable description of the
97 * type that this object expects. This is used in the generation
98 * of the USAGE statement. The goal is to be helpful to the end user
99 * of the program.
100 * \param v - An optional visitor. You probably should not
101 * use this unless you have a very good reason.
102 */
103 ValueArg(const std::string &flag, const std::string &name,
104 const std::string &desc, bool req, T value,
105 const std::string &typeDesc, Visitor *v = NULL);
106
107 /**
108 * Labeled ValueArg constructor.
109 *
110 * \param flag - The one character flag that identifies this
111 * argument on the command line.
112 * \param name - A one word name for the argument. Can be
113 * used as a long flag on the command line.
114 * \param desc - A description of what the argument is for or
115 * does.
116 * \param req - Whether the argument is required on the command
117 * line.
118 * \param value - The default value assigned to this argument if it
119 * is not present on the command line.
120 * \param typeDesc - A short, human readable description of the
121 * type that this object expects. This is used in the generation
122 * of the USAGE statement. The goal is to be helpful to the end user
123 * of the program.
124 * \param parser - A CmdLine parser object to add this Arg to
125 * \param v - An optional visitor. You probably should not
126 * use this unless you have a very good reason.
127 */
128 ValueArg(const std::string &flag, const std::string &name,
129 const std::string &desc, bool req, T value,
130 const std::string &typeDesc, ArgContainer &parser,
131 Visitor *v = NULL);
132
133 /**
134 * Labeled ValueArg constructor.
135 *
136 * \param flag - The one character flag that identifies this
137 * argument on the command line.
138 * \param name - A one word name for the argument. Can be
139 * used as a long flag on the command line.
140 * \param desc - A description of what the argument is for or
141 * does.
142 * \param req - Whether the argument is required on the command
143 * line.
144 * \param value - The default value assigned to this argument if it
145 * is not present on the command line.
146 * \param constraint - A pointer to a Constraint object used
147 * to constrain this Arg.
148 * \param parser - A CmdLine parser object to add this Arg to.
149 * \param v - An optional visitor. You probably should not
150 * use this unless you have a very good reason.
151 */
152 ValueArg(const std::string &flag, const std::string &name,
153 const std::string &desc, bool req, T value,
154 Constraint<T> *constraint, ArgContainer &parser,
155 Visitor *v = NULL);
156
157 /**
158 * Labeled ValueArg constructor.
159 *
160 * \param flag - The one character flag that identifies this
161 * argument on the command line.
162 * \param name - A one word name for the argument. Can be
163 * used as a long flag on the command line.
164 * \param desc - A description of what the argument is for or
165 * does.
166 * \param req - Whether the argument is required on the command
167 * line.
168 * \param value - The default value assigned to this argument if it
169 * is not present on the command line.
170 * \param constraint - A pointer to a Constraint object used
171 * to constrain this Arg.
172 * \param v - An optional visitor. You probably should not
173 * use this unless you have a very good reason.
174 */
175 ValueArg(const std::string &flag, const std::string &name,
176 const std::string &desc, bool req, T value,
177 Constraint<T> *constraint, Visitor *v = NULL);
178
179 /**
180 * Handles the processing of the argument.
181 * This re-implements the Arg version of this method to set the
182 * _value of the argument appropriately. It knows the difference
183 * between labeled and unlabeled.
184 * \param i - Pointer the the current argument in the list.
185 * \param args - Mutable list of strings. Passed
186 * in from main().
187 */
188 virtual bool processArg(int *i, std::vector<std::string> &args);
189
190 /**
191 * Returns the value of the argument.
192 */
193 const T &getValue() const { return _value; }
194
195 /**
196 * A ValueArg can be used as as its value type (T) This is the
197 * same as calling getValue()
198 */
199 operator const T &() const { return getValue(); }
200
201 /**
202 * Specialization of shortID.
203 * \param val - value to be used.
204 */
205 virtual std::string shortID(const std::string &val = "val") const;
206
207 /**
208 * Specialization of longID.
209 * \param val - value to be used.
210 */
211 virtual std::string longID(const std::string &val = "val") const;
212
213 virtual void reset();
214
215 private:
216 /**
217 * Prevent accidental copying
218 */
219 ValueArg(const ValueArg<T>& rhs);
220 ValueArg& operator=(const ValueArg<T>& rhs);
221 };
222
223 /**
224 * Constructor implementation.
225 */
226 template <class T>
227 ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
228 const std::string &desc, bool req, T val,
229 const std::string &typeDesc, Visitor *v)
230 : Arg(flag, name, desc, req, true, v),
231 _value(val),
232 _default(val),
233 _typeDesc(typeDesc),
234 _constraint(NULL) {}
235
236 template <class T>
237 ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
238 const std::string &desc, bool req, T val,
239 const std::string &typeDesc, ArgContainer &parser,
240 Visitor *v)
241 : Arg(flag, name, desc, req, true, v),
242 _value(val),
243 _default(val),
244 _typeDesc(typeDesc),
245 _constraint(NULL) {
246 parser.add(this);
247 }
248
249 template <class T>
250 ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
251 const std::string &desc, bool req, T val,
252 Constraint<T> *constraint, Visitor *v)
253 : Arg(flag, name, desc, req, true, v),
254 _value(val),
255 _default(val),
256 _typeDesc(Constraint<T>::shortID(constraint)),
257 _constraint(constraint) {}
258
259 template <class T>
260 ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
261 const std::string &desc, bool req, T val,
262 Constraint<T> *constraint, ArgContainer &parser,
263 Visitor *v)
264 : Arg(flag, name, desc, req, true, v),
265 _value(val),
266 _default(val),
267 _typeDesc(Constraint<T>::shortID(constraint)),
268 _constraint(constraint) {
269 parser.add(this);
270 }
271
272 /**
273 * Implementation of processArg().
274 */
275 template <class T>
276 bool ValueArg<T>::processArg(int *i, std::vector<std::string> &args) {
277 if (_hasBlanks(args[*i])) return false;
278
279 std::string flag = args[*i];
280
281 std::string value = "";
282 trimFlag(flag, value);
283
284 if (argMatches(flag)) {
285 if (_alreadySet) {
286 throw(CmdLineParseException("Argument already set!", toString()));
287 }
288
289 if (Arg::delimiter() != ' ' && value == "")
290 throw(ArgParseException(
291 "Couldn't find delimiter for this argument!", toString()));
292
293 if (value == "") {
294 (*i)++;
295 if (static_cast<unsigned int>(*i) < args.size())
296 _extractValue(args[*i]);
297 else
298 throw(ArgParseException("Missing a value for this argument!",
299 toString()));
300 } else {
301 _extractValue(value);
302 }
303
304 _alreadySet = true;
305 _setBy = flag;
306 _checkWithVisitor();
307 return true;
308 } else {
309 return false;
310 }
311 }
312
313 /**
314 * Implementation of shortID.
315 */
316 template <class T>
317 std::string ValueArg<T>::shortID(const std::string &) const {
318 return Arg::shortID("<" + _typeDesc + ">");
319 }
320
321 /**
322 * Implementation of longID.
323 */
324 template <class T>
325 std::string ValueArg<T>::longID(const std::string &) const {
326 return Arg::longID("<" + _typeDesc + ">");
327 }
328
329 template <class T>
330 void ValueArg<T>::_extractValue(const std::string &val) {
331 try {
332 ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
333 } catch (ArgParseException &e) {
334 throw ArgParseException(e.error(), toString());
335 }
336
337 if (_constraint != NULL)
338 if (!_constraint->check(_value))
339 throw(CmdLineParseException("Value '" + val +
340 +"' does not meet constraint: " +
341 _constraint->description(),
342 toString()));
343 }
344
345 template <class T>
346 void ValueArg<T>::reset() {
347 Arg::reset();
348 _value = _default;
349 }
350
351 } // namespace TCLAP
352
353 #endif // TCLAP_VALUE_ARG_H