Main MRPT website > C++ reference
MRPT logo
MultiSwitchArg.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3 *
4 * file: MultiSwitchArg.h
5 *
6 * Copyright (c) 2003, Michael E. Smoot .
7 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
8 * Copyright (c) 2005, Michael E. Smoot, Daniel Aarno, Erik Zeek.
9 * All rights reverved.
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 
25 #ifndef TCLAP_MULTI_SWITCH_ARG_H
26 #define TCLAP_MULTI_SWITCH_ARG_H
27 
28 #include <string>
29 #include <vector>
30 
32 
33 namespace TCLAP {
34 
35 /**
36 * A multiple switch argument. If the switch is set on the command line, then
37 * the getValue method will return the number of times the switch appears.
38 */
39 class MultiSwitchArg : public SwitchArg
40 {
41  protected:
42 
43  /**
44  * The value of the switch.
45  */
46  int _value;
47 
48 
49  public:
50 
51  /**
52  * MultiSwitchArg constructor.
53  * \param flag - The one character flag that identifies this
54  * argument on the command line.
55  * \param name - A one word name for the argument. Can be
56  * used as a long flag on the command line.
57  * \param desc - A description of what the argument is for or
58  * does.
59  * \param init - Optional. The initial/default value of this Arg.
60  * Defaults to 0.
61  * \param v - An optional visitor. You probably should not
62  * use this unless you have a very good reason.
63  */
64  MultiSwitchArg(const std::string& flag,
65  const std::string& name,
66  const std::string& desc,
67  int init = 0,
68  Visitor* v = NULL);
69 
70 
71  /**
72  * MultiSwitchArg constructor.
73  * \param flag - The one character flag that identifies this
74  * argument on the command line.
75  * \param name - A one word name for the argument. Can be
76  * used as a long flag on the command line.
77  * \param desc - A description of what the argument is for or
78  * does.
79  * \param parser - A CmdLine parser object to add this Arg to
80  * \param init - Optional. The initial/default value of this Arg.
81  * Defaults to 0.
82  * \param v - An optional visitor. You probably should not
83  * use this unless you have a very good reason.
84  */
85  MultiSwitchArg(const std::string& flag,
86  const std::string& name,
87  const std::string& desc,
88  CmdLineInterface& parser,
89  int init = 0,
90  Visitor* v = NULL);
91 
92 
93  /**
94  * Handles the processing of the argument.
95  * This re-implements the SwitchArg version of this method to set the
96  * _value of the argument appropriately.
97  * \param i - Pointer the the current argument in the list.
98  * \param args - Mutable list of strings. Passed
99  * in from main().
100  */
101  virtual bool processArg(int* i, std::vector<std::string>& args);
102 
103  /**
104  * Returns int, the number of times the switch has been set.
105  */
106  int getValue();
107 
108  /**
109  * Returns the shortID for this Arg.
110  */
111  std::string shortID(const std::string& val) const;
112 
113  /**
114  * Returns the longID for this Arg.
115  */
116  std::string longID(const std::string& val) const;
117 };
118 
119 //////////////////////////////////////////////////////////////////////
120 //BEGIN MultiSwitchArg.cpp
121 //////////////////////////////////////////////////////////////////////
122 inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
123  const std::string& name,
124  const std::string& desc,
125  int init,
126  Visitor* v )
127 : SwitchArg(flag, name, desc, false, v),
128 _value( init )
129 { }
130 
131 inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
132  const std::string& name,
133  const std::string& desc,
134  CmdLineInterface& parser,
135  int init,
136  Visitor* v )
137 : SwitchArg(flag, name, desc, false, v),
138 _value( init )
139 {
140  parser.add( this );
141 }
142 
143 inline int MultiSwitchArg::getValue() { return _value; }
144 
145 inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string>& args)
146 {
147  if ( _ignoreable && Arg::ignoreRest() )
148  return false;
149 
150  if ( argMatches( args[*i] ))
151  {
152  // so the isSet() method will work
153  _alreadySet = true;
154 
155  // Matched argument: increment value.
156  ++_value;
157 
159 
160  return true;
161  }
162  else if ( combinedSwitchesMatch( args[*i] ) )
163  {
164  // so the isSet() method will work
165  _alreadySet = true;
166 
167  // Matched argument: increment value.
168  ++_value;
169 
170  // Check for more in argument and increment value.
171  while ( combinedSwitchesMatch( args[*i] ) )
172  ++_value;
173 
175 
176  return false;
177  }
178  else
179  return false;
180 }
181 
182 std::string MultiSwitchArg::shortID(const std::string& val) const
183 {
184  std::string id = Arg::shortID() + " ... ";
185 
186  return id;
187 }
188 
189 std::string MultiSwitchArg::longID(const std::string& val) const
190 {
191  std::string id = Arg::longID() + " (accepted multiple times)";
192 
193  return id;
194 }
195 
196 //////////////////////////////////////////////////////////////////////
197 //END MultiSwitchArg.cpp
198 //////////////////////////////////////////////////////////////////////
199 
200 } //namespace TCLAP
201 
202 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013