tclap  1.4.0
UnlabeledMultiArg.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 /******************************************************************************
4  *
5  * file: UnlabeledMultiArg.h
6  *
7  * Copyright (c) 2003, Michael E. Smoot.
8  * Copyright (c) 2017, Google LLC
9  * All rights reserved.
10  *
11  * See the file COPYING in the top directory of this distribution for
12  * more information.
13  *
14  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  *
22  *****************************************************************************/
23 
24 #ifndef TCLAP_UNLABELED_MULTI_ARG_H
25 #define TCLAP_UNLABELED_MULTI_ARG_H
26 
27 #include <tclap/MultiArg.h>
29 
30 #include <list>
31 #include <string>
32 #include <vector>
33 
34 namespace TCLAP {
35 
41 template <class T>
42 class UnlabeledMultiArg : public MultiArg<T> {
43  // If compiler has two stage name lookup (as gcc >= 3.4 does)
44  // this is required to prevent undef. symbols
49  using MultiArg<T>::_name;
52  using MultiArg<T>::_setBy;
54 
55 public:
73  UnlabeledMultiArg(const std::string &name, const std::string &desc,
74  bool req, const std::string &typeDesc,
75  bool ignoreable = false, Visitor *v = NULL);
94  UnlabeledMultiArg(const std::string &name, const std::string &desc,
95  bool req, const std::string &typeDesc,
96  ArgContainer &parser, bool ignoreable = false,
97  Visitor *v = NULL);
98 
114  UnlabeledMultiArg(const std::string &name, const std::string &desc,
115  bool req, Constraint<T> *constraint,
116  bool ignoreable = false, Visitor *v = NULL);
117 
134  UnlabeledMultiArg(const std::string &name, const std::string &desc,
135  bool req, Constraint<T> *constraint, ArgContainer &parser,
136  bool ignoreable = false, Visitor *v = NULL);
137 
146  virtual bool processArg(int *i, std::vector<std::string> &args);
147 
151  virtual std::string shortID(const std::string &) const {
152  return Arg::getName() + " ...";
153  }
154 
159  virtual std::string longID(const std::string &) const {
160  return Arg::getName() + " (accepted multiple times) <" + _typeDesc +
161  ">";
162  }
163 
168  virtual bool operator==(const Arg &a) const;
169 
174  virtual void addToList(std::list<Arg *> &argList) const;
175 
176  virtual bool hasLabel() const { return false; }
177 };
178 
179 template <class T>
181  const std::string &desc, bool req,
182  const std::string &typeDesc,
183  bool ignoreable, Visitor *v)
184  : MultiArg<T>("", name, desc, req, typeDesc, v) {
185  _ignoreable = ignoreable;
187 }
188 
189 template <class T>
191  const std::string &desc, bool req,
192  const std::string &typeDesc,
193  ArgContainer &parser, bool ignoreable,
194  Visitor *v)
195  : MultiArg<T>("", name, desc, req, typeDesc, v) {
196  _ignoreable = ignoreable;
198  parser.add(this);
199 }
200 
201 template <class T>
203  const std::string &desc, bool req,
204  Constraint<T> *constraint,
205  bool ignoreable, Visitor *v)
206  : MultiArg<T>("", name, desc, req, constraint, v) {
207  _ignoreable = ignoreable;
209 }
210 
211 template <class T>
213  const std::string &desc, bool req,
214  Constraint<T> *constraint,
215  ArgContainer &parser, bool ignoreable,
216  Visitor *v)
217  : MultiArg<T>("", name, desc, req, constraint, v) {
218  _ignoreable = ignoreable;
220  parser.add(this);
221 }
222 
223 template <class T>
224 bool UnlabeledMultiArg<T>::processArg(int *i, std::vector<std::string> &args) {
225  if (_hasBlanks(args[*i])) return false;
226 
227  // never ignore an unlabeled multi arg
228 
229  // always take the first value, regardless of the start string
230  _extractValue(args[(*i)]);
231 
232  /*
233  // continue taking args until we hit the end or a start string
234  while ( (unsigned int)(*i)+1 < args.size() &&
235  args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
236  args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
237  _extractValue( args[++(*i)] );
238  */
239 
240  _alreadySet = true;
241  _setBy = args[*i];
242 
243  return true;
244 }
245 
246 template <class T>
247 bool UnlabeledMultiArg<T>::operator==(const Arg &a) const {
248  if (_name == a.getName() || _description == a.getDescription())
249  return true;
250  else
251  return false;
252 }
253 
254 template <class T>
255 void UnlabeledMultiArg<T>::addToList(std::list<Arg *> &argList) const {
256  argList.push_back(const_cast<Arg *>(static_cast<const Arg *const>(this)));
257 }
258 } // namespace TCLAP
259 
260 #endif // TCLAP_UNLABELED_MULTI_ARG_H
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
static void check(bool req, const std::string &argName)
An argument that allows multiple values of type T to be specified.
Definition: MultiArg.h:41
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
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
virtual bool operator==(const Arg &a) const
Operator ==.
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
virtual void addToList(std::list< Arg *> &argList) const
Pushes this to back of list rather than front.
A base class that defines the interface for visitors.
Definition: Visitor.h:32
const std::string & getName() const
Returns the argument name.
Definition: Arg.h:525
std::string _description
Description of the argument.
Definition: Arg.h:97
Just like a MultiArg, except that the arguments are unlabeled.
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:543
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:139
std::string getDescription() const
Returns the argument description.
Definition: Arg.h:262
UnlabeledMultiArg(const std::string &name, const std::string &desc, bool req, const std::string &typeDesc, bool ignoreable=false, Visitor *v=NULL)
Constructor.
virtual std::string shortID(const std::string &) const
Returns the a short id string.
std::string _name
A single word namd identifying the argument.
Definition: Arg.h:92
virtual bool hasLabel() const
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 _setBy
Indicates the value specified to set this flag (like -a or –all).
Definition: Arg.h:126
virtual std::string longID(const std::string &) const
Returns the a long id string.