Main MRPT website > C++ reference for MRPT 1.5.3
UnlabeledMultiArg.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 /******************************************************************************
11  *
12  * file: UnlabeledMultiArg.h
13  *
14  * Copyright (c) 2003, Michael E. Smoot.
15  * All rights reverved.
16  *
17  * See the file COPYING in the top directory of this distribution for
18  * more information.
19  *
20  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  *
28  *****************************************************************************/
29 
30 
31 #ifndef TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H
32 #define TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H
33 
34 #include <string>
35 #include <vector>
36 
39 
40 namespace TCLAP {
41 
42 /**
43  * Just like a MultiArg, except that the arguments are unlabeled. Basically,
44  * this Arg will slurp up everything that hasn't been matched to another
45  * Arg.
46  */
47 template<class T>
48 class UnlabeledMultiArg : public MultiArg<T>
49 {
50 
51  // If compiler has two stage name lookup (as gcc >= 3.4 does)
52  // this is requried to prevent undef. symbols
57  using MultiArg<T>::_name;
61 
62  public:
63 
64  /**
65  * Constructor.
66  * \param name - The name of the Arg. Note that this is used for
67  * identification, not as a long flag.
68  * \param desc - A description of what the argument is for or
69  * does.
70  * \param req - Whether the argument is required on the command
71  * line.
72  * \param typeDesc - A short, human readable description of the
73  * type that this object expects. This is used in the generation
74  * of the USAGE statement. The goal is to be helpful to the end user
75  * of the program.
76  * \param ignoreable - Whether or not this argument can be ignored
77  * using the "--" flag.
78  * \param v - An optional visitor. You probably should not
79  * use this unless you have a very good reason.
80  */
81  UnlabeledMultiArg( const std::string& name,
82  const std::string& desc,
83  bool req,
84  const std::string& typeDesc,
85  bool ignoreable = false,
86  Visitor* v = NULL );
87  /**
88  * Constructor.
89  * \param name - The name of the Arg. Note that this is used for
90  * identification, not as a long flag.
91  * \param desc - A description of what the argument is for or
92  * does.
93  * \param req - Whether the argument is required on the command
94  * line.
95  * \param typeDesc - A short, human readable description of the
96  * type that this object expects. This is used in the generation
97  * of the USAGE statement. The goal is to be helpful to the end user
98  * of the program.
99  * \param parser - A CmdLine parser object to add this Arg to
100  * \param ignoreable - Whether or not this argument can be ignored
101  * using the "--" flag.
102  * \param v - An optional visitor. You probably should not
103  * use this unless you have a very good reason.
104  */
105  UnlabeledMultiArg( const std::string& name,
106  const std::string& desc,
107  bool req,
108  const std::string& typeDesc,
109  CmdLineInterface& parser,
110  bool ignoreable = false,
111  Visitor* v = NULL );
112 
113  /**
114  * Constructor.
115  * \param name - The name of the Arg. Note that this is used for
116  * identification, not as a long flag.
117  * \param desc - A description of what the argument is for or
118  * does.
119  * \param req - Whether the argument is required on the command
120  * line.
121  * \param constraint - A pointer to a Constraint object used
122  * to constrain this Arg.
123  * \param ignoreable - Whether or not this argument can be ignored
124  * using the "--" flag.
125  * \param v - An optional visitor. You probably should not
126  * use this unless you have a very good reason.
127  */
128  UnlabeledMultiArg( const std::string& name,
129  const std::string& desc,
130  bool req,
131  Constraint<T>* constraint,
132  bool ignoreable = false,
133  Visitor* v = NULL );
134 
135  /**
136  * Constructor.
137  * \param name - The name of the Arg. Note that this is used for
138  * identification, not as a long flag.
139  * \param desc - A description of what the argument is for or
140  * does.
141  * \param req - Whether the argument is required on the command
142  * line.
143  * \param constraint - A pointer to a Constraint object used
144  * to constrain this Arg.
145  * \param parser - A CmdLine parser object to add this Arg to
146  * \param ignoreable - Whether or not this argument can be ignored
147  * using the "--" flag.
148  * \param v - An optional visitor. You probably should not
149  * use this unless you have a very good reason.
150  */
151  UnlabeledMultiArg( const std::string& name,
152  const std::string& desc,
153  bool req,
154  Constraint<T>* constraint,
155  CmdLineInterface& parser,
156  bool ignoreable = false,
157  Visitor* v = NULL );
158 
159  /**
160  * Handles the processing of the argument.
161  * This re-implements the Arg version of this method to set the
162  * _value of the argument appropriately. It knows the difference
163  * between labeled and unlabeled.
164  * \param i - Pointer the the current argument in the list.
165  * \param args - Mutable list of strings. Passed from main().
166  */
167  virtual bool processArg(int* i, std::vector<std::string>& args);
168 
169  /**
170  * Returns the a short id string. Used in the usage.
171  * \param val - value to be used.
172  */
173  virtual std::string shortID(const std::string& val="val") const;
174 
175  /**
176  * Returns the a long id string. Used in the usage.
177  * \param val - value to be used.
178  */
179  virtual std::string longID(const std::string& val="val") const;
180 
181  /**
182  * Opertor ==.
183  * \param a - The Arg to be compared to this.
184  */
185  virtual bool operator==(const Arg& a) const;
186 
187  /**
188  * Pushes this to back of list rather than front.
189  * \param argList - The list this should be added to.
190  */
191  virtual void addToList( std::list<Arg*>& argList ) const;
192 };
193 
194 template<class T>
196  const std::string& desc,
197  bool req,
198  const std::string& typeDesc,
199  bool ignoreable,
200  Visitor* v)
201 : MultiArg<T>("", name, desc, req, typeDesc, v)
202 {
203  _ignoreable = ignoreable;
205 }
206 
207 template<class T>
209  const std::string& desc,
210  bool req,
211  const std::string& typeDesc,
212  CmdLineInterface& parser,
213  bool ignoreable,
214  Visitor* v)
215 : MultiArg<T>("", name, desc, req, typeDesc, v)
216 {
217  _ignoreable = ignoreable;
219  parser.add( this );
220 }
221 
222 
223 template<class T>
225  const std::string& desc,
226  bool req,
227  Constraint<T>* constraint,
228  bool ignoreable,
229  Visitor* v)
230 : MultiArg<T>("", name, desc, req, constraint, v)
231 {
232  _ignoreable = ignoreable;
234 }
235 
236 template<class T>
238  const std::string& desc,
239  bool req,
240  Constraint<T>* constraint,
241  CmdLineInterface& parser,
242  bool ignoreable,
243  Visitor* v)
244 : MultiArg<T>("", name, desc, req, constraint, v)
245 {
246  _ignoreable = ignoreable;
248  parser.add( this );
249 }
250 
251 
252 template<class T>
253 bool UnlabeledMultiArg<T>::processArg(int *i, std::vector<std::string>& args)
254 {
255 
256  if ( _hasBlanks( args[*i] ) )
257  return false;
258 
259  // never ignore an unlabeled multi arg
260 
261 
262  // always take the first value, regardless of the start string
263  _extractValue( args[(*i)] );
264 
265  /*
266  // continue taking args until we hit the end or a start string
267  while ( (unsigned int)(*i)+1 < args.size() &&
268  args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
269  args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
270  _extractValue( args[++(*i)] );
271  */
272 
273  _alreadySet = true;
274 
275  return true;
276 }
277 
278 template<class T>
279 std::string UnlabeledMultiArg<T>::shortID(const std::string& val) const
280 {
281  std::string id = "<" + _typeDesc + "> ...";
282 
283  return id;
284 }
285 
286 template<class T>
287 std::string UnlabeledMultiArg<T>::longID(const std::string& val) const
288 {
289  std::string id = "<" + _typeDesc + "> (accepted multiple times)";
290 
291  return id;
292 }
293 
294 template<class T>
296 {
297  if ( _name == a.getName() || _description == a.getDescription() )
298  return true;
299  else
300  return false;
301 }
302 
303 template<class T>
304 void UnlabeledMultiArg<T>::addToList( std::list<Arg*>& argList ) const
305 {
306  argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
307 }
308 
309 }
310 
311 #endif
std::string getDescription() const
Returns the argument description.
Definition: Arg.h:462
UnlabeledMultiArg(const std::string &name, const std::string &desc, bool req, const std::string &typeDesc, bool ignoreable=false, Visitor *v=NULL)
Constructor.
An argument that allows multiple values of type T to be specified.
Definition: MultiArg.h:37
Definition: Arg.h:44
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:51
virtual void addToList(std::list< Arg *> &argList) const
Pushes this to back of list rather than front.
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
std::string _description
Description of the argument.
Definition: Arg.h:90
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:115
const std::string & getName() const
Returns the argument name.
Definition: Arg.h:477
virtual std::string longID(const std::string &val="val") const
Returns the a long id string.
virtual bool operator==(const Arg &a) const
Opertor ==.
A base class that defines the interface for visitors.
Definition: Visitor.h:39
std::string _typeDesc
The description of type T to be used in the usage.
Definition: MultiArg.h:168
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:128
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:507
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: MultiArg.h:484
Just like a MultiArg, except that the arguments are unlabeled.
std::string _name
A single work namd indentifying the argument.
Definition: Arg.h:85
virtual std::string shortID(const std::string &val="val") const
Returns the a short id string.
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:46
static void check(bool req, const std::string &argName)
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:549
The base class that manages the command line definition and passes along the parsing to the appropria...



Page generated by Doxygen 1.8.13 for MRPT 1.5.3 at Tue Oct 31 07:27:35 UTC 2017