tclap 1.2.1

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 reverved.
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
32 namespace TCLAP {
33
39 class SwitchArg : public Arg
40 {
41 protected:
42
46 bool _value;
47
52 bool _default;
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
135 inline 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
145 inline 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
158 inline bool SwitchArg::getValue() { return _value; }
159
160 inline 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
169 inline 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() ) ==
178 Arg::nameStartString() )
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
204 inline void SwitchArg::commonProcessing()
205 {
206 if ( _xorSet )
207 throw(CmdLineParseException(
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
220 _checkWithVisitor();
221 }
222
223 inline 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
255 inline void SwitchArg::reset()
256 {
257 Arg::reset();
258 _value = _default;
259 }
261 //End SwitchArg.cpp
263
264 } //namespace TCLAP
265
266 #endif
TCLAP::Arg::_alreadySet
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:137
TCLAP::SwitchArg::combinedSwitchesMatch
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
TCLAP::Arg
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:64
TCLAP::CmdLineParseException
Thrown from CmdLine when the arguments on the command line are not properly specified, e.g.
Definition: ArgException.h:143
TCLAP::SwitchArg
A simple switch argument.
Definition: SwitchArg.h:39
TCLAP::Arg::_checkWithVisitor
void _checkWithVisitor() const
Performs the special handling described by the Vistitor.
Definition: Arg.h:611
TCLAP::Arg::reset
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:679
TCLAP::Arg::_xorSet
bool _xorSet
Indicates that the arg was set as part of an XOR and not on the command line.
Definition: Arg.h:156
TCLAP::Arg::delimiter
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: Arg.h:211
TCLAP::SwitchArg::getValue
bool getValue()
Returns bool, whether or not the switch has been set.
Definition: SwitchArg.h:158
TCLAP::SwitchArg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: SwitchArg.h:223
TCLAP::SwitchArg::_value
bool _value
The value of the switch.
Definition: SwitchArg.h:46
TCLAP::SwitchArg::_default
bool _default
Used to support the reset() method so that ValueArg can be reset to their constructed value...
Definition: SwitchArg.h:52
TCLAP::Arg::nameStartString
static const std::string nameStartString()
Definition: Arg.h:245
TCLAP::Visitor
A base class that defines the interface for visitors.
Definition: Visitor.h:31
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:43
TCLAP::CmdLineInterface::add
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
TCLAP::Arg::_ignoreable
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:150
Arg.h
TCLAP::Arg::ignoreRest
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:205
TCLAP::SwitchArg::reset
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: SwitchArg.h:255
TCLAP::Arg::argMatches
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:590
TCLAP::Arg::blankChar
static char blankChar()
The char used as a place holder when SwitchArgs are combined.
Definition: Arg.h:217
TCLAP::SwitchArg::SwitchArg
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
TCLAP::Arg::_flag
std::string _flag
The single char flag used to identify the argument.
Definition: Arg.h:98
TCLAP
Definition: Arg.h:57
TCLAP::Arg::toString
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:599
TCLAP::Arg::flagStartString
static const std::string flagStartString()
Definition: Arg.h:236