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