Main MRPT website > C++ reference for MRPT 1.5.3
UnlabeledValueArg.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: UnlabeledValueArg.h
13  *
14  * Copyright (c) 2003, Michael E. Smoot .
15  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
16  * All rights reverved.
17  *
18  * See the file COPYING in the top directory of this distribution for
19  * more information.
20  *
21  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  *
29  *****************************************************************************/
30 
31 
32 #ifndef TCLAP_UNLABELED_VALUE_ARGUMENT_H
33 #define TCLAP_UNLABELED_VALUE_ARGUMENT_H
34 
35 #include <string>
36 #include <vector>
37 
40 
41 namespace TCLAP {
42 
43 /**
44  * The basic unlabeled argument that parses a value.
45  * This is a template class, which means the type T defines the type
46  * that a given object will attempt to parse when an UnlabeledValueArg
47  * is reached in the list of args that the CmdLine iterates over.
48  */
49 template<class T>
50 class UnlabeledValueArg : public ValueArg<T>
51 {
52 
53  // If compiler has two stage name lookup (as gcc >= 3.4 does)
54  // this is requried to prevent undef. symbols
59  using ValueArg<T>::_name;
63 
64  public:
65 
66  /**
67  * UnlabeledValueArg constructor.
68  * \param name - A one word name for the argument. Can be
69  * used as a long flag on the command line.
70  * \param desc - A description of what the argument is for or
71  * does.
72  * \param req - Whether the argument is required on the command
73  * line.
74  * \param value - The default value assigned to this argument if it
75  * is not present on the command line.
76  * \param typeDesc - A short, human readable description of the
77  * type that this object expects. This is used in the generation
78  * of the USAGE statement. The goal is to be helpful to the end user
79  * of the program.
80  * \param ignoreable - Allows you to specify that this argument can be
81  * ignored if the '--' flag is set. This defaults to false (cannot
82  * be ignored) and should generally stay that way unless you have
83  * some special need for certain arguments to be ignored.
84  * \param v - Optional Vistor. You should leave this blank unless
85  * you have a very good reason.
86  */
87  UnlabeledValueArg( const std::string& name,
88  const std::string& desc,
89  bool req,
90  T value,
91  const std::string& typeDesc,
92  bool ignoreable = false,
93  Visitor* v = NULL);
94 
95  /**
96  * UnlabeledValueArg constructor.
97  * \param name - A one word name for the argument. Can be
98  * used as a long flag on the command line.
99  * \param desc - A description of what the argument is for or
100  * does.
101  * \param req - Whether the argument is required on the command
102  * line.
103  * \param value - The default value assigned to this argument if it
104  * is not present on the command line.
105  * \param typeDesc - A short, human readable description of the
106  * type that this object expects. This is used in the generation
107  * of the USAGE statement. The goal is to be helpful to the end user
108  * of the program.
109  * \param parser - A CmdLine parser object to add this Arg to
110  * \param ignoreable - Allows you to specify that this argument can be
111  * ignored if the '--' flag is set. This defaults to false (cannot
112  * be ignored) and should generally stay that way unless you have
113  * some special need for certain arguments to be ignored.
114  * \param v - Optional Vistor. You should leave this blank unless
115  * you have a very good reason.
116  */
117  UnlabeledValueArg( const std::string& name,
118  const std::string& desc,
119  bool req,
120  T value,
121  const std::string& typeDesc,
122  CmdLineInterface& parser,
123  bool ignoreable = false,
124  Visitor* v = NULL );
125 
126  /**
127  * UnlabeledValueArg constructor.
128  * \param name - A one word name for the argument. Can be
129  * used as a long flag on the command line.
130  * \param desc - A description of what the argument is for or
131  * does.
132  * \param req - Whether the argument is required on the command
133  * line.
134  * \param value - The default value assigned to this argument if it
135  * is not present on the command line.
136  * \param constraint - A pointer to a Constraint object used
137  * to constrain this Arg.
138  * \param ignoreable - Allows you to specify that this argument can be
139  * ignored if the '--' flag is set. This defaults to false (cannot
140  * be ignored) and should generally stay that way unless you have
141  * some special need for certain arguments to be ignored.
142  * \param v - Optional Vistor. You should leave this blank unless
143  * you have a very good reason.
144  */
145  UnlabeledValueArg( const std::string& name,
146  const std::string& desc,
147  bool req,
148  T value,
149  Constraint<T>* constraint,
150  bool ignoreable = false,
151  Visitor* v = NULL );
152 
153 
154  /**
155  * UnlabeledValueArg constructor.
156  * \param name - A one word name for the argument. Can be
157  * used as a long flag on the command line.
158  * \param desc - A description of what the argument is for or
159  * does.
160  * \param req - Whether the argument is required on the command
161  * line.
162  * \param value - The default value assigned to this argument if it
163  * is not present on the command line.
164  * \param constraint - A pointer to a Constraint object used
165  * to constrain this Arg.
166  * \param parser - A CmdLine parser object to add this Arg to
167  * \param ignoreable - Allows you to specify that this argument can be
168  * ignored if the '--' flag is set. This defaults to false (cannot
169  * be ignored) and should generally stay that way unless you have
170  * some special need for certain arguments to be ignored.
171  * \param v - Optional Vistor. You should leave this blank unless
172  * you have a very good reason.
173  */
174  UnlabeledValueArg( const std::string& name,
175  const std::string& desc,
176  bool req,
177  T value,
178  Constraint<T>* constraint,
179  CmdLineInterface& parser,
180  bool ignoreable = false,
181  Visitor* v = NULL);
182 
183  /**
184  * Handles the processing of the argument.
185  * This re-implements the Arg version of this method to set the
186  * _value of the argument appropriately. Handling specific to
187  * unlabled arguments.
188  * \param i - Pointer the the current argument in the list.
189  * \param args - Mutable list of strings.
190  */
191  virtual bool processArg(int* i, std::vector<std::string>& args);
192 
193  /**
194  * Overrides shortID for specific behavior.
195  */
196  virtual std::string shortID(const std::string& val="val") const;
197 
198  /**
199  * Overrides longID for specific behavior.
200  */
201  virtual std::string longID(const std::string& val="val") const;
202 
203  /**
204  * Overrides operator== for specific behavior.
205  */
206  virtual bool operator==(const Arg& a ) const;
207 
208  /**
209  * Instead of pushing to the front of list, push to the back.
210  * \param argList - The list to add this to.
211  */
212  virtual void addToList( std::list<Arg*>& argList ) const;
213 
214 };
215 
216 /**
217  * Constructor implemenation.
218  */
219 template<class T>
221  const std::string& desc,
222  bool req,
223  T val,
224  const std::string& typeDesc,
225  bool ignoreable,
226  Visitor* v)
227 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
228 {
229  _ignoreable = ignoreable;
230 
232 
233 }
234 
235 template<class T>
237  const std::string& desc,
238  bool req,
239  T val,
240  const std::string& typeDesc,
241  CmdLineInterface& parser,
242  bool ignoreable,
243  Visitor* v)
244 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
245 {
246  _ignoreable = ignoreable;
248  parser.add( this );
249 }
250 
251 /**
252  * Constructor implemenation.
253  */
254 template<class T>
256  const std::string& desc,
257  bool req,
258  T val,
259  Constraint<T>* constraint,
260  bool ignoreable,
261  Visitor* v)
262 : ValueArg<T>("", name, desc, req, val, constraint, v)
263 {
264  _ignoreable = ignoreable;
266 }
267 
268 template<class T>
270  const std::string& desc,
271  bool req,
272  T val,
273  Constraint<T>* constraint,
274  CmdLineInterface& parser,
275  bool ignoreable,
276  Visitor* v)
277 : ValueArg<T>("", name, desc, req, val, constraint, v)
278 {
279  _ignoreable = ignoreable;
281  parser.add( this );
282 }
283 
284 /**
285  * Implementation of processArg().
286  */
287 template<class T>
288 bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string>& args)
289 {
290 
291  if ( _alreadySet )
292  return false;
293 
294  if ( _hasBlanks( args[*i] ) )
295  return false;
296 
297  // never ignore an unlabeled arg
298 
299  _extractValue( args[*i] );
300  _alreadySet = true;
301  return true;
302 }
303 
304 /**
305  * Overriding shortID for specific output.
306  */
307 template<class T>
308 std::string UnlabeledValueArg<T>::shortID(const std::string& val) const
309 {
310  std::string id = "<" + _typeDesc + ">";
311 
312  return id;
313 }
314 
315 /**
316  * Overriding longID for specific output.
317  */
318 template<class T>
319 std::string UnlabeledValueArg<T>::longID(const std::string& val) const
320 {
321  // Ideally we would like to be able to use RTTI to return the name
322  // of the type required for this argument. However, g++ at least,
323  // doesn't appear to return terribly useful "names" of the types.
324  std::string id = "<" + _typeDesc + ">";
325 
326  return id;
327 }
328 
329 /**
330  * Overriding operator== for specific behavior.
331  */
332 template<class T>
334 {
335  if ( _name == a.getName() || _description == a.getDescription() )
336  return true;
337  else
338  return false;
339 }
340 
341 template<class T>
342 void UnlabeledValueArg<T>::addToList( std::list<Arg*>& argList ) const
343 {
344  argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
345 }
346 
347 }
348 #endif
virtual std::string longID(const std::string &val="val") const
Overrides longID for specific behavior.
std::string getDescription() const
Returns the argument description.
Definition: Arg.h:462
The basic unlabeled argument that parses a value.
std::string _typeDesc
A human readable description of the type to be parsed.
Definition: ValueArg.h:177
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12104
Definition: Arg.h:44
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:51
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
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 shortID(const std::string &val="val") const
Overrides shortID for specific behavior.
A base class that defines the interface for visitors.
Definition: Visitor.h:39
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: ValueArg.h:488
The basic labeled argument that parses a value.
Definition: ValueArg.h:37
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
virtual bool operator==(const Arg &a) const
Overrides operator== for specific behavior.
UnlabeledValueArg(const std::string &name, const std::string &desc, bool req, T value, const std::string &typeDesc, bool ignoreable=false, Visitor *v=NULL)
UnlabeledValueArg constructor.
std::string _name
A single work namd indentifying the argument.
Definition: Arg.h:85
virtual void addToList(std::list< Arg *> &argList) const
Instead of pushing to the front of list, push to the back.
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:46
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
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 Sun Nov 26 00:44:48 UTC 2017