tclap 1.2.2
SwitchArg.h
Go to the documentation of this file.
1
2/******************************************************************************
3 *
4 * file: SwitchArg.h
5 *
6 * Copyright (c) 2003, Michael E. Smoot .
7 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
8 * All rights reserved.
9 *
10 * See the file COPYING in the top directory of this distribution for
11 * more information.
12 *
13 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *****************************************************************************/
22
23
24#ifndef TCLAP_SWITCH_ARG_H
25#define TCLAP_SWITCH_ARG_H
26
27#include <string>
28#include <vector>
29
30#include <tclap/Arg.h>
31
32namespace TCLAP {
33
39class SwitchArg : public Arg
40{
41 protected:
42
46 bool _value;
47
53
54 public:
55
68 SwitchArg(const std::string& flag,
69 const std::string& name,
70 const std::string& desc,
71 bool def = false,
72 Visitor* v = NULL);
73
74
88 SwitchArg(const std::string& flag,
89 const std::string& name,
90 const std::string& desc,
91 CmdLineInterface& parser,
92 bool def = false,
93 Visitor* v = NULL);
94
95
104 virtual bool processArg(int* i, std::vector<std::string>& args);
105
110 bool combinedSwitchesMatch(std::string& combined);
111
115 bool getValue();
116
117 virtual void reset();
118
119 private:
124 bool lastCombined(std::string& combined);
125
129 void commonProcessing();
130};
131
133//BEGIN SwitchArg.cpp
135inline SwitchArg::SwitchArg(const std::string& flag,
136 const std::string& name,
137 const std::string& desc,
138 bool default_val,
139 Visitor* v )
140: Arg(flag, name, desc, false, false, v),
141 _value( default_val ),
142 _default( default_val )
143{ }
144
145inline SwitchArg::SwitchArg(const std::string& flag,
146 const std::string& name,
147 const std::string& desc,
148 CmdLineInterface& parser,
149 bool default_val,
150 Visitor* v )
151: Arg(flag, name, desc, false, false, v),
152 _value( default_val ),
153 _default(default_val)
154{
155 parser.add( this );
156}
157
158inline bool SwitchArg::getValue() { return _value; }
159
160inline bool SwitchArg::lastCombined(std::string& combinedSwitches )
161{
162 for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
163 if ( combinedSwitches[i] != Arg::blankChar() )
164 return false;
165
166 return true;
167}
168
169inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
170{
171 // make sure this is actually a combined switch
172 if ( combinedSwitches.length() > 0 &&
173 combinedSwitches[0] != Arg::flagStartString()[0] )
174 return false;
175
176 // make sure it isn't a long name
177 if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
179 return false;
180
181 // make sure the delimiter isn't in the string
182 if ( combinedSwitches.find_first_of( Arg::delimiter() ) != std::string::npos )
183 return false;
184
185 // ok, we're not specifying a ValueArg, so we know that we have
186 // a combined switch list.
187 for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
188 if ( _flag.length() > 0 &&
189 combinedSwitches[i] == _flag[0] &&
190 _flag[0] != Arg::flagStartString()[0] )
191 {
192 // update the combined switches so this one is no longer present
193 // this is necessary so that no unlabeled args are matched
194 // later in the processing.
195 //combinedSwitches.erase(i,1);
196 combinedSwitches[i] = Arg::blankChar();
197 return true;
198 }
199
200 // none of the switches passed in the list match.
201 return false;
202}
203
204inline void SwitchArg::commonProcessing()
205{
206 if ( _xorSet )
208 "Mutually exclusive argument already set!", toString()));
209
210 if ( _alreadySet )
211 throw(CmdLineParseException("Argument already set!", toString()));
212
213 _alreadySet = true;
214
215 if ( _value == true )
216 _value = false;
217 else
218 _value = true;
219
221}
222
223inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
224{
225 if ( _ignoreable && Arg::ignoreRest() )
226 return false;
227
228 // if the whole string matches the flag or name string
229 if ( argMatches( args[*i] ) )
230 {
231 commonProcessing();
232
233 return true;
234 }
235 // if a substring matches the flag as part of a combination
236 else if ( combinedSwitchesMatch( args[*i] ) )
237 {
238 // check again to ensure we don't misinterpret
239 // this as a MultiSwitchArg
240 if ( combinedSwitchesMatch( args[*i] ) )
241 throw(CmdLineParseException("Argument already set!",
242 toString()));
243
244 commonProcessing();
245
246 // We only want to return true if we've found the last combined
247 // match in the string, otherwise we return true so that other
248 // switches in the combination will have a chance to match.
249 return lastCombined( args[*i] );
250 }
251 else
252 return false;
253}
254
255inline void SwitchArg::reset()
256{
257 Arg::reset();
258 _value = _default;
259}
260
261//End SwitchArg.cpp
263
264} //namespace TCLAP
265
266#endif
static char blankChar()
The char used as a place holder when SwitchArgs are combined.
Definition Arg.h:218
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition Arg.h:612
static const std::string nameStartString()
Definition Arg.h:246
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
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition Arg.h:212
bool _xorSet
Indicates that the arg was set as part of an XOR and not on the command line.
Definition Arg.h:157
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition Arg.h:680
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 toString() const
Returns a simple string representation of the argument.
Definition Arg.h:600
std::string _flag
The single char flag used to identify the argument.
Definition Arg.h:99
static const std::string flagStartString()
Definition Arg.h:237
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.
Thrown from CmdLine when the arguments on the command line are not properly specified,...
bool _value
The value of the switch.
Definition SwitchArg.h:46
SwitchArg(const std::string &flag, const std::string &name, const std::string &desc, bool def=false, Visitor *v=NULL)
SwitchArg constructor.
Definition SwitchArg.h:135
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition SwitchArg.h:223
bool _default
Used to support the reset() method so that ValueArg can be reset to their constructed value.
Definition SwitchArg.h:52
bool getValue()
Returns bool, whether or not the switch has been set.
Definition SwitchArg.h:158
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
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition SwitchArg.h:255
A base class that defines the interface for visitors.
Definition Visitor.h:32
Definition Arg.h:58