tclap 1.6.0
MultiArg.h
Go to the documentation of this file.
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 <concepts>
32#include <string>
33#include <utility>
34#include <vector>
35
36namespace TCLAP {
42template <class T>
43class MultiArg : public Arg {
44public:
45 using container_type = std::vector<T>;
46 using iterator = typename container_type::iterator;
47 using const_iterator = typename container_type::const_iterator;
48
49protected:
53 std::vector<T> _values;
54
58 std::string _typeDesc;
59
64
71 void _extractValue(const std::string &val)
72 requires detail::ValidArgValueType<T> && std::default_initializable<T>;
73
79
80public:
98 MultiArg(const std::string &flag, const std::string &name,
99 const std::string &desc, bool req, std::string typeDesc,
100 Visitor *v = nullptr);
101
120 MultiArg(const std::string &flag, const std::string &name,
121 const std::string &desc, bool req, std::string typeDesc,
122 ArgContainer &parser, Visitor *v = nullptr);
123
139 MultiArg(const std::string &flag, const std::string &name,
140 const std::string &desc, bool req, const Constraint<T> *constraint,
141 Visitor *v = nullptr);
142
159 MultiArg(const std::string &flag, const std::string &name,
160 const std::string &desc, bool req, const Constraint<T> *constraint,
161 ArgContainer &parser, Visitor *v = nullptr);
162
171 bool processArg(int *i, std::vector<std::string> &args) override;
172
177 const std::vector<T> &getValue() const noexcept { return _values; }
178
183 const_iterator begin() const noexcept { return _values.begin(); }
184
189 const_iterator end() const noexcept { return _values.end(); }
190
195 [[nodiscard]] std::string shortID(
196 const std::string &val = "val") const override;
197
202 [[nodiscard]] std::string longID(
203 const std::string &val = "val") const override;
204
205 bool allowMore() override;
206
207 void reset() override;
208
212 MultiArg(const MultiArg<T> &rhs) = delete;
213 MultiArg &operator=(const MultiArg<T> &rhs) = delete;
214
215private:
216 void _extractNextValue(int *index, std::vector<std::string> &args);
217};
218
219template <class T>
220MultiArg<T>::MultiArg(const std::string &flag, const std::string &name,
221 const std::string &desc, bool req,
222 std::string typeDesc, Visitor *v)
223 : Arg(flag, name, desc, req, true, v),
224 _values(),
225 _typeDesc(std::move(typeDesc)),
226 _constraint(nullptr),
227 _allowMore(false) {
229}
230
231template <class T>
232MultiArg<T>::MultiArg(const std::string &flag, const std::string &name,
233 const std::string &desc, bool req,
234 std::string typeDesc, ArgContainer &parser,
235 Visitor *v)
236 : Arg(flag, name, desc, req, true, v),
237 _values(),
238 _typeDesc(std::move(typeDesc)),
239 _constraint(nullptr),
240 _allowMore(false) {
241 parser.add(this);
243}
244
248template <class T>
249MultiArg<T>::MultiArg(const std::string &flag, const std::string &name,
250 const std::string &desc, bool req,
251 const Constraint<T> *constraint, Visitor *v)
252 : Arg(flag, name, desc, req, true, v),
253 _values(),
254 _typeDesc(Constraint<T>::shortID(constraint)),
255 _constraint(constraint),
256 _allowMore(false) {
258}
259
260template <class T>
261MultiArg<T>::MultiArg(const std::string &flag, const std::string &name,
262 const std::string &desc, bool req,
263 const Constraint<T> *constraint, ArgContainer &parser,
264 Visitor *v)
265 : Arg(flag, name, desc, req, true, v),
266 _values(),
267 _typeDesc(Constraint<T>::shortID(constraint)),
268 _constraint(constraint),
269 _allowMore(false) {
270 parser.add(this);
272}
273
274template <class T>
275bool MultiArg<T>::processArg(int *i, std::vector<std::string> &args) {
276 if (_hasBlanks(args[*i])) return false;
277
278 std::string flag = args[*i];
279 std::string value = "";
280
281 trimFlag(flag, value);
282 const bool hasAttachedValue = flag != args[*i];
283
284 if (!argMatches(flag)) return false;
285
286 if (this->getDelimiter() != ' ' && !hasAttachedValue)
287 throw(ArgParseException("Couldn't find delimiter for this argument!",
288 toString()));
289
290 if (!hasAttachedValue) {
291 _extractNextValue(i, args);
292 } else {
293 _extractValue(value);
294 }
295
296 _alreadySet = true;
297 _setBy = flag;
298 _checkWithVisitor();
299
300 return true;
301}
302
303template <class T>
304void MultiArg<T>::_extractNextValue(int *index,
305 std::vector<std::string> &args) {
306 (*index)++;
307 if (static_cast<unsigned int>(*index) < args.size())
308 _extractValue(args[*index]);
309 else
310 throw(ArgParseException("Missing a value for this argument!",
311 toString()));
312}
313
314template <class T>
315std::string MultiArg<T>::shortID(const std::string &val) const {
316 static_cast<void>(val); // Ignore input, don't warn
317 return Arg::shortID("<" + _typeDesc + ">") + " ...";
318}
319
323template <class T>
324std::string MultiArg<T>::longID(const std::string &val) const {
325 static_cast<void>(val); // Ignore input, don't warn
326 return Arg::longID("<" + _typeDesc + ">") + " (accepted multiple times)";
327}
328
329template <class T>
330void MultiArg<T>::_extractValue(const std::string &val)
331 requires detail::ValidArgValueType<T> && std::default_initializable<T>
332{
333 try {
334 T tmp;
335 ExtractValue(tmp, val, typename ArgTraits<T>::ValueCategory());
336 _values.push_back(tmp);
337 } catch (ArgParseException &e) {
338 throw ArgParseException(e.error(), toString());
339 }
340
341 if (_constraint != nullptr && !_constraint->check(_values.back()))
343 "Value '" + val +
344 "' does not meet constraint: " + _constraint->description(),
345 toString()));
346}
347
348template <class T>
350 bool am = _allowMore;
351 _allowMore = true;
352 return am;
353}
354
355template <class T>
357 Arg::reset();
358 _values.clear();
359}
360
361} // namespace TCLAP
362
363#endif // TCLAP_MULTI_ARG_H
Interface that allows adding an Arg to a "container".
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
std::string error() const
Returns the error text.
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
typename DefaultArgTrait< T >::ValueCategory ValueCategory
Definition ArgTraits.h:113
A virtual base class that defines the essential data for all arguments.
Definition Arg.h:69
bool _acceptsMultipleValues
Definition Arg.h:177
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition Arg.h:603
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition Arg.h:823
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition Arg.h:590
Thrown from CmdLine when the arguments on the command line are not properly specified,...
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:43
const_iterator end() const noexcept
Returns the end of the values parsed from the command line.
Definition MultiArg.h:189
typename container_type::const_iterator const_iterator
Definition MultiArg.h:47
std::string _typeDesc
The description of type T to be used in the usage.
Definition MultiArg.h:58
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition MultiArg.h:330
std::string shortID(const std::string &val="val") const override
Returns the a short id string.
Definition MultiArg.h:315
std::string longID(const std::string &val="val") const override
Returns the a long id string.
Definition MultiArg.h:324
bool processArg(int *i, std::vector< std::string > &args) override
Handles the processing of the argument.
Definition MultiArg.h:275
MultiArg & operator=(const MultiArg< T > &rhs)=delete
MultiArg(const std::string &flag, const std::string &name, const std::string &desc, bool req, std::string typeDesc, Visitor *v=nullptr)
Constructor.
Definition MultiArg.h:220
const_iterator begin() const noexcept
Returns an iterator over the values parsed from the command line.
Definition MultiArg.h:183
bool allowMore() override
Used for MultiArgs to determine whether args can still be set.
Definition MultiArg.h:349
void reset() override
Clears the Arg object and allows it to be reused by new command lines.
Definition MultiArg.h:356
const std::vector< T > & getValue() const noexcept
Returns a vector of type T containing the values parsed from the command line.
Definition MultiArg.h:177
bool _allowMore
Used by MultiArg to decide whether to keep parsing for this arg.
Definition MultiArg.h:78
MultiArg(const MultiArg< T > &rhs)=delete
Prevent accidental copying.
typename container_type::iterator iterator
Definition MultiArg.h:46
std::vector< T > _values
The list of values parsed from the CmdLine.
Definition MultiArg.h:53
const Constraint< T > * _constraint
A constraint that the values of this Arg must conform to.
Definition MultiArg.h:63
std::vector< T > container_type
Definition MultiArg.h:45
A base class that defines the interface for visitors.
Definition Visitor.h:32
Definition Arg.h:54
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition Arg.h:494