tclap  1.2.1
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
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:590
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:137
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:169
int _default
Used to support the reset() method so that ValueArg can be reset to their constructed value...
std::string longID(const std::string &val) const
Returns the longID for this Arg.
A simple switch argument.
Definition: SwitchArg.h:39
int getValue()
Returns int, the number of times the switch has been set.
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:523
MultiSwitchArg(const std::string &flag, const std::string &name, const std::string &desc, int init=0, Visitor *v=NULL)
MultiSwitchArg constructor.
A multiple switch argument.
void _checkWithVisitor() const
Performs the special handling described by the Vistitor.
Definition: Arg.h:611
A base class that defines the interface for visitors.
Definition: Visitor.h:31
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:505
The base class that manages the command line definition and passes along the parsing to the appropria...
std::string shortID(const std::string &val) const
Returns the shortID for this Arg.
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:150
int _value
The value of the switch.
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:205
void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:57
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.