tclap 1.2.2
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 reserved.
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
33namespace TCLAP {
34
40{
41 protected:
42
46 int _value;
47
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
130inline 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
140inline 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
153inline int MultiSwitchArg::getValue() { return _value; }
154
155inline 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
192inline std::string
193MultiSwitchArg::shortID(const std::string& val) const
194{
195 return Arg::shortID(val) + " ... ";
196}
197
198inline std::string
199MultiSwitchArg::longID(const std::string& val) const
200{
201 return Arg::longID(val) + " (accepted multiple times)";
202}
203
204inline void
206{
208}
209
211//END MultiSwitchArg.cpp
213
214} //namespace TCLAP
215
216#endif
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition: Arg.h:612
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:524
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:206
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:138
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:151
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:591
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:506
The base class that manages the command line definition and passes along the parsing to the appropria...
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
A multiple switch argument.
int getValue()
Returns int, the number of times the switch has been set.
MultiSwitchArg(const std::string &flag, const std::string &name, const std::string &desc, int init=0, Visitor *v=NULL)
MultiSwitchArg constructor.
std::string shortID(const std::string &val) const
Returns the shortID for this Arg.
std::string longID(const std::string &val) const
Returns the longID for this Arg.
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
int _value
The value of the switch.
void reset()
Clears the Arg object and allows it to be reused by new command lines.
int _default
Used to support the reset() method so that ValueArg can be reset to their constructed value.
A simple switch argument.
Definition: SwitchArg.h:40
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
A base class that defines the interface for visitors.
Definition: Visitor.h:32
Definition: Arg.h:58