Debian Codegraph @

podofo/3rdparty/tclap/MultiArg.h

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