Main MRPT website > C++ reference for MRPT 1.4.0
MultiSwitchArg.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-2016, 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: MultiSwitchArg.h
13 *
14 * Copyright (c) 2003, Michael E. Smoot .
15 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
16 * Copyright (c) 2005, Michael E. Smoot, Daniel Aarno, Erik Zeek.
17 * All rights reverved.
18 *
19 * See the file COPYING in the top directory of this distribution for
20 * more information.
21 *
22 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 *
30 *****************************************************************************/
31 
32 
33 #ifndef TCLAP_MULTI_SWITCH_ARG_H
34 #define TCLAP_MULTI_SWITCH_ARG_H
35 
36 #include <string>
37 #include <vector>
38 
40 
41 namespace TCLAP {
42 
43 /**
44 * A multiple switch argument. If the switch is set on the command line, then
45 * the getValue method will return the number of times the switch appears.
46 */
47 template <class DUMMY = int>
48 class MultiSwitchArg : public SwitchArg
49 {
50  protected:
51 
52  /**
53  * The value of the switch.
54  */
55  int _value;
56 
57 
58  public:
59 
60  /**
61  * MultiSwitchArg constructor.
62  * \param flag - The one character flag that identifies this
63  * argument on the command line.
64  * \param name - A one word name for the argument. Can be
65  * used as a long flag on the command line.
66  * \param desc - A description of what the argument is for or
67  * does.
68  * \param init - Optional. The initial/default value of this Arg.
69  * Defaults to 0.
70  * \param v - An optional visitor. You probably should not
71  * use this unless you have a very good reason.
72  */
73  MultiSwitchArg(const std::string& flag,
74  const std::string& name,
75  const std::string& desc,
76  int init = 0,
77  Visitor* v = NULL);
78 
79 
80  /**
81  * MultiSwitchArg constructor.
82  * \param flag - The one character flag that identifies this
83  * argument on the command line.
84  * \param name - A one word name for the argument. Can be
85  * used as a long flag on the command line.
86  * \param desc - A description of what the argument is for or
87  * does.
88  * \param parser - A CmdLine parser object to add this Arg to
89  * \param init - Optional. The initial/default value of this Arg.
90  * Defaults to 0.
91  * \param v - An optional visitor. You probably should not
92  * use this unless you have a very good reason.
93  */
94  MultiSwitchArg(const std::string& flag,
95  const std::string& name,
96  const std::string& desc,
97  CmdLineInterface& parser,
98  int init = 0,
99  Visitor* v = NULL);
100 
101 
102  /**
103  * Handles the processing of the argument.
104  * This re-implements the SwitchArg version of this method to set the
105  * _value of the argument appropriately.
106  * \param i - Pointer the the current argument in the list.
107  * \param args - Mutable list of strings. Passed
108  * in from main().
109  */
110  virtual bool processArg(int* i, std::vector<std::string>& args);
111 
112  /**
113  * Returns int, the number of times the switch has been set.
114  */
115  int getValue();
116 
117  /**
118  * Returns the shortID for this Arg.
119  */
120  std::string shortID(const std::string& val) const;
121 
122  /**
123  * Returns the longID for this Arg.
124  */
125  std::string longID(const std::string& val) const;
126 };
127 
128 //////////////////////////////////////////////////////////////////////
129 //BEGIN MultiSwitchArg.cpp
130 //////////////////////////////////////////////////////////////////////
131 template <class DUMMY>
132 inline MultiSwitchArg<DUMMY>::MultiSwitchArg(const std::string& flag,
133  const std::string& name,
134  const std::string& desc,
135  int init,
136  Visitor* v )
137 : SwitchArg(flag, name, desc, false, v),
138 _value( init )
139 { }
140 
141 template <class DUMMY>
142 inline MultiSwitchArg<DUMMY>::MultiSwitchArg(const std::string& flag,
143  const std::string& name,
144  const std::string& desc,
145  CmdLineInterface& parser,
146  int init,
147  Visitor* v )
148 : SwitchArg(flag, name, desc, false, v),
149 _value( init )
150 {
151  parser.add( this );
152 }
153 
154 template <class DUMMY>
155 inline int MultiSwitchArg<DUMMY>::getValue() { return _value; }
156 
157 template <class DUMMY>
158 inline bool MultiSwitchArg<DUMMY>::processArg(int *i, std::vector<std::string>& args)
159 {
160  if ( _ignoreable && Arg::ignoreRest() )
161  return false;
162 
163  if ( argMatches( args[*i] ))
164  {
165  // so the isSet() method will work
166  _alreadySet = true;
167 
168  // Matched argument: increment value.
169  ++_value;
170 
172 
173  return true;
174  }
175  else if ( combinedSwitchesMatch( args[*i] ) )
176  {
177  // so the isSet() method will work
178  _alreadySet = true;
179 
180  // Matched argument: increment value.
181  ++_value;
182 
183  // Check for more in argument and increment value.
184  while ( combinedSwitchesMatch( args[*i] ) )
185  ++_value;
186 
188 
189  return false;
190  }
191  else
192  return false;
193 }
194 
195 template <class DUMMY>
196 std::string MultiSwitchArg<DUMMY>::shortID(const std::string& val) const
197 {
198  std::string id = Arg::shortID() + " ... ";
199 
200  return id;
201 }
202 
203 template <class DUMMY>
204 std::string MultiSwitchArg<DUMMY>::longID(const std::string& val) const
205 {
206  std::string id = Arg::longID() + " (accepted multiple times)";
207 
208  return id;
209 }
210 
211 //////////////////////////////////////////////////////////////////////
212 //END MultiSwitchArg.cpp
213 //////////////////////////////////////////////////////////////////////
214 
215 } //namespace TCLAP
216 
217 #endif
int getValue()
Returns int, the number of times the switch has been set.
Definition: Arg.h:44
int _value
The value of the switch.
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
A simple switch argument.
Definition: SwitchArg.h:47
A multiple switch argument.
void _checkWithVisitor() const
Performs the special handling described by the Vistitor.
Definition: Arg.h:519
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:410
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:115
MultiSwitchArg(const std::string &flag, const std::string &name, const std::string &desc, int init=0, Visitor *v=NULL)
MultiSwitchArg constructor.
A base class that defines the interface for visitors.
Definition: Visitor.h:39
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:431
std::string shortID(const std::string &val) const
Returns the shortID for this Arg.
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
bool combinedSwitchesMatch(std::string &combined)
Checks a string to see if any of the chars in the string match the flag for this Switch.
Definition: SwitchArg.h:147
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:128
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:498
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:183
std::string longID(const std::string &val) const
Returns the longID for this Arg.
The base class that manages the command line definition and passes along the parsing to the appropria...



Page generated by Doxygen 1.8.11 for MRPT 1.4.0 SVN: at Mon Aug 15 11:50:21 UTC 2016