tclap  1.4.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 <string>
32 #include <vector>
33 
34 namespace TCLAP {
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:
51  std::vector<T> _values;
52 
56  std::string _typeDesc;
57 
62 
69  void _extractValue(const std::string &val);
70 
75  bool _allowMore;
76 
77 public:
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 
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 
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 
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 
168  virtual bool processArg(int *i, std::vector<std::string> &args);
169 
174  const std::vector<T> &getValue() const { return _values; }
175 
180  const_iterator begin() const { return _values.begin(); }
181 
186  const_iterator end() const { return _values.end(); }
187 
192  virtual std::string shortID(const std::string &val = "val") const;
193 
198  virtual std::string longID(const std::string &val = "val") const;
199 
200  virtual bool allowMore();
201 
202  virtual void reset();
203 
204 private:
208  MultiArg<T>(const MultiArg<T> &rhs);
209  MultiArg<T> &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 
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;
296 
297  return true;
298  } else {
299  return false;
300  }
301 }
302 
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 
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>
341  bool am = _allowMore;
342  _allowMore = true;
343  return am;
344 }
345 
346 template <class T>
348  Arg::reset();
349  _values.clear();
350 }
351 
352 } // namespace TCLAP
353 
354 #endif // TCLAP_MULTI_ARG_H
virtual std::string longID(const std::string &val="val") const
Returns the a long id string.
Definition: MultiArg.h:316
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:535
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:122
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:53
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: MultiArg.h:268
Thrown from CmdLine when the arguments on the command line are not properly specified, e.g.
Definition: ArgException.h:129
A value like argument value type is a value that can be set using operator>>.
Definition: ArgTraits.h:39
const_iterator end() const
Returns the end of the values parsed from the command line.
Definition: MultiArg.h:186
An argument that allows multiple values of type T to be specified.
Definition: MultiArg.h:41
std::vector< T > _values
The list of values parsed from the CmdLine.
Definition: MultiArg.h:51
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:595
virtual std::string shortID(const std::string &val="val") const
Returns the a short id string.
Definition: MultiArg.h:307
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:498
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: MultiArg.h:347
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: Arg.h:186
const_iterator begin() const
Returns an iterator over the values parsed from the command line.
Definition: MultiArg.h:180
std::string _typeDesc
The description of type T to be used in the usage.
Definition: MultiArg.h:56
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:41
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition: Arg.h:553
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: MultiArg.h:322
Interface that allows adding an Arg to a "container".
Definition: ArgContainer.h:39
Constraint< T > * _constraint
A list of constraint on this Arg.
Definition: MultiArg.h:61
virtual bool allowMore()
Used for MultiArgs to determine whether args can still be set.
Definition: MultiArg.h:340
A base class that defines the interface for visitors.
Definition: Visitor.h:32
std::vector< T > container_type
Definition: MultiArg.h:43
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:485
const std::vector< T > & getValue() const
Returns a vector of type T containing the values parsed from the command line.
Definition: MultiArg.h:174
bool _allowMore
Used by MultiArg to decide whether to keep parsing for this arg.
Definition: MultiArg.h:75
container_type::const_iterator const_iterator
Definition: MultiArg.h:45
MultiArg(const std::string &flag, const std::string &name, const std::string &desc, bool req, const std::string &typeDesc, Visitor *v=NULL)
Constructor.
Definition: MultiArg.h:213
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:543
container_type::iterator iterator
Definition: MultiArg.h:44
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition: Arg.h:399
bool _acceptsMultipleValues
Definition: Arg.h:141
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
Definition: ArgException.h:110
Definition: Arg.h:46
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:577
std::string error() const
Returns the error text.
Definition: ArgException.h:61
std::string _setBy
Indicates the value specified to set this flag (like -a or –all).
Definition: Arg.h:126
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
Definition: Arg.h:560