tclap  1.2.0
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 
31 #include <tclap/SwitchArg.h>
32 
33 namespace TCLAP {
34 
39 class MultiSwitchArg : public SwitchArg
40 {
41  protected:
42 
46  int _value;
47 
52  int _default;
53 
54  public:
55 
69  MultiSwitchArg(const std::string& flag,
70  const std::string& name,
71  const std::string& desc,
72  int init = 0,
73  Visitor* v = NULL);
74 
75 
90  MultiSwitchArg(const std::string& flag,
91  const std::string& name,
92  const std::string& desc,
93  CmdLineInterface& parser,
94  int init = 0,
95  Visitor* v = NULL);
96 
97 
106  virtual bool processArg(int* i, std::vector<std::string>& args);
107 
111  int getValue();
112 
116  std::string shortID(const std::string& val) const;
117 
121  std::string longID(const std::string& val) const;
122 
123  void reset();
124 
125 };
126 
128 //BEGIN MultiSwitchArg.cpp
130 inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
131  const std::string& name,
132  const std::string& desc,
133  int init,
134  Visitor* v )
135 : SwitchArg(flag, name, desc, false, v),
136 _value( init ),
137 _default( init )
138 { }
139 
140 inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
141  const std::string& name,
142  const std::string& desc,
143  CmdLineInterface& parser,
144  int init,
145  Visitor* v )
146 : SwitchArg(flag, name, desc, false, v),
147 _value( init ),
148 _default( init )
149 {
150  parser.add( this );
151 }
152 
153 inline int MultiSwitchArg::getValue() { return _value; }
154 
155 inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string>& args)
156 {
157  if ( _ignoreable && Arg::ignoreRest() )
158  return false;
159 
160  if ( argMatches( args[*i] ))
161  {
162  // so the isSet() method will work
163  _alreadySet = true;
164 
165  // Matched argument: increment value.
166  ++_value;
167 
169 
170  return true;
171  }
172  else if ( combinedSwitchesMatch( args[*i] ) )
173  {
174  // so the isSet() method will work
175  _alreadySet = true;
176 
177  // Matched argument: increment value.
178  ++_value;
179 
180  // Check for more in argument and increment value.
181  while ( combinedSwitchesMatch( args[*i] ) )
182  ++_value;
183 
185 
186  return false;
187  }
188  else
189  return false;
190 }
191 
192 inline std::string
193 MultiSwitchArg::shortID(const std::string& val) const
194 {
195  return Arg::shortID(val) + " ... ";
196 }
197 
198 inline std::string
199 MultiSwitchArg::longID(const std::string& val) const
200 {
201  return Arg::longID(val) + " (accepted multiple times)";
202 }
203 
204 inline void
206 {
208 }
209 
211 //END MultiSwitchArg.cpp
213 
214 } //namespace TCLAP
215 
216 #endif