tclap  1.2.2
UnlabeledMultiArg.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 
4 /******************************************************************************
5  *
6  * file: UnlabeledMultiArg.h
7  *
8  * Copyright (c) 2003, Michael E. Smoot.
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 
26 #ifndef TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H
27 #define TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H
28 
29 #include <string>
30 #include <vector>
31 
32 #include <tclap/MultiArg.h>
34 
35 namespace TCLAP {
36 
42 template<class T>
43 class UnlabeledMultiArg : public MultiArg<T>
44 {
45 
46  // If compiler has two stage name lookup (as gcc >= 3.4 does)
47  // this is required to prevent undef. symbols
52  using MultiArg<T>::_name;
56 
57  public:
58 
76  UnlabeledMultiArg( const std::string& name,
77  const std::string& desc,
78  bool req,
79  const std::string& typeDesc,
80  bool ignoreable = false,
81  Visitor* v = NULL );
100  UnlabeledMultiArg( const std::string& name,
101  const std::string& desc,
102  bool req,
103  const std::string& typeDesc,
104  CmdLineInterface& parser,
105  bool ignoreable = false,
106  Visitor* v = NULL );
107 
123  UnlabeledMultiArg( const std::string& name,
124  const std::string& desc,
125  bool req,
126  Constraint<T>* constraint,
127  bool ignoreable = false,
128  Visitor* v = NULL );
129 
146  UnlabeledMultiArg( const std::string& name,
147  const std::string& desc,
148  bool req,
149  Constraint<T>* constraint,
150  CmdLineInterface& parser,
151  bool ignoreable = false,
152  Visitor* v = NULL );
153 
162  virtual bool processArg(int* i, std::vector<std::string>& args);
163 
168  virtual std::string shortID(const std::string& val="val") const;
169 
174  virtual std::string longID(const std::string& val="val") const;
175 
180  virtual bool operator==(const Arg& a) const;
181 
186  virtual void addToList( std::list<Arg*>& argList ) const;
187 };
188 
189 template<class T>
191  const std::string& desc,
192  bool req,
193  const std::string& typeDesc,
194  bool ignoreable,
195  Visitor* v)
196 : MultiArg<T>("", name, desc, req, typeDesc, v)
197 {
198  _ignoreable = ignoreable;
200 }
201 
202 template<class T>
204  const std::string& desc,
205  bool req,
206  const std::string& typeDesc,
207  CmdLineInterface& parser,
208  bool ignoreable,
209  Visitor* v)
210 : MultiArg<T>("", name, desc, req, typeDesc, v)
211 {
212  _ignoreable = ignoreable;
214  parser.add( this );
215 }
216 
217 
218 template<class T>
220  const std::string& desc,
221  bool req,
222  Constraint<T>* constraint,
223  bool ignoreable,
224  Visitor* v)
225 : MultiArg<T>("", name, desc, req, constraint, v)
226 {
227  _ignoreable = ignoreable;
229 }
230 
231 template<class T>
233  const std::string& desc,
234  bool req,
235  Constraint<T>* constraint,
236  CmdLineInterface& parser,
237  bool ignoreable,
238  Visitor* v)
239 : MultiArg<T>("", name, desc, req, constraint, v)
240 {
241  _ignoreable = ignoreable;
243  parser.add( this );
244 }
245 
246 
247 template<class T>
248 bool UnlabeledMultiArg<T>::processArg(int *i, std::vector<std::string>& args)
249 {
250 
251  if ( _hasBlanks( args[*i] ) )
252  return false;
253 
254  // never ignore an unlabeled multi arg
255 
256 
257  // always take the first value, regardless of the start string
258  _extractValue( args[(*i)] );
259 
260  /*
261  // continue taking args until we hit the end or a start string
262  while ( (unsigned int)(*i)+1 < args.size() &&
263  args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
264  args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
265  _extractValue( args[++(*i)] );
266  */
267 
268  _alreadySet = true;
269 
270  return true;
271 }
272 
273 template<class T>
274 std::string UnlabeledMultiArg<T>::shortID(const std::string& val) const
275 {
276  static_cast<void>(val); // Ignore input, don't warn
277  return std::string("<") + _typeDesc + "> ...";
278 }
279 
280 template<class T>
281 std::string UnlabeledMultiArg<T>::longID(const std::string& val) const
282 {
283  static_cast<void>(val); // Ignore input, don't warn
284  return std::string("<") + _typeDesc + "> (accepted multiple times)";
285 }
286 
287 template<class T>
289 {
290  if ( _name == a.getName() || _description == a.getDescription() )
291  return true;
292  else
293  return false;
294 }
295 
296 template<class T>
297 void UnlabeledMultiArg<T>::addToList( std::list<Arg*>& argList ) const
298 {
299  argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
300 }
301 
302 }
303 
304 #endif
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:128
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:55
static void check(bool req, const std::string &argName)
An argument that allows multiple values of type T to be specified.
Definition: MultiArg.h:42
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:59
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:42
virtual bool operator==(const Arg &a) const
Operator ==.
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: MultiArg.h:398
virtual std::string shortID(const std::string &val="val") const
Returns the a short id string.
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:34
const std::string & getName() const
Returns the argument name.
Definition: Arg.h:560
std::string _description
Description of the argument.
Definition: Arg.h:103
Just like a MultiArg, except that the arguments are unlabeled.
The base class that manages the command line definition and passes along the parsing to the appropria...
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:590
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:141
std::string getDescription() const
Returns the argument description.
Definition: Arg.h:545
UnlabeledMultiArg(const std::string &name, const std::string &desc, bool req, const std::string &typeDesc, bool ignoreable=false, Visitor *v=NULL)
Constructor.
std::string _name
A single word namd identifying the argument.
Definition: Arg.h:98
Definition: Arg.h:48
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:632
virtual std::string longID(const std::string &val="val") const
Returns the a long id string.