tclap 1.4.0
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#ifndef TCLAP_SWITCH_ARG_H
26#define TCLAP_SWITCH_ARG_H
27
28#include <tclap/Arg.h>
29
30#include <string>
31#include <vector>
32
33namespace TCLAP {
34
40class SwitchArg : public Arg {
41protected:
45 bool _value;
46
52
53public:
66 SwitchArg(const std::string &flag, const std::string &name,
67 const std::string &desc, bool def = false, Visitor *v = NULL);
68
82 SwitchArg(const std::string &flag, const std::string &name,
83 const std::string &desc, ArgContainer &parser, bool def = false,
84 Visitor *v = NULL);
85
94 virtual bool processArg(int *i, std::vector<std::string> &args);
95
100 bool combinedSwitchesMatch(std::string &combined);
101
106 bool getValue() const { return _value; }
107
112 operator bool() const { return _value; }
113
114 virtual void reset();
115
116private:
117 bool isCombinedSwitchToken(const std::string &token) const;
118
123 bool lastCombined(std::string &combined);
124
128 void commonProcessing();
129};
130
132// BEGIN SwitchArg.cpp
134inline SwitchArg::SwitchArg(const std::string &flag, const std::string &name,
135 const std::string &desc, bool default_val,
136 Visitor *v)
137 : Arg(flag, name, desc, false, false, v),
138 _value(default_val),
139 _default(default_val) {}
140
141inline SwitchArg::SwitchArg(const std::string &flag, const std::string &name,
142 const std::string &desc, ArgContainer &parser,
143 bool default_val, Visitor *v)
144 : Arg(flag, name, desc, false, false, v),
145 _value(default_val),
146 _default(default_val) {
147 parser.add(this);
148}
149
150inline bool SwitchArg::lastCombined(std::string &combinedSwitches) {
151 for (unsigned int i = 1; i < combinedSwitches.length(); i++)
152 if (combinedSwitches[i] != Arg::blankChar()) return false;
153
154 return true;
155}
156
157inline bool SwitchArg::combinedSwitchesMatch(std::string &combinedSwitches) {
158 if (!isCombinedSwitchToken(combinedSwitches)) return false;
159
160 for (unsigned int i = 1; i < combinedSwitches.length(); i++) {
161 if (combinedSwitches[i] != Arg::flagStartString()[0] &&
162 matchesShortFlag(std::string(1, combinedSwitches[i]))) {
163 // update the combined switches so this one is no longer
164 // present this is necessary so that no unlabeled args are
165 // matched later in the processing.
166 _setBy = Arg::flagStartString() + combinedSwitches[i];
167 combinedSwitches[i] = Arg::blankChar();
168 return true;
169 }
170 }
171
172 // none of the switches passed in the list match.
173 return false;
174}
175
176inline bool SwitchArg::isCombinedSwitchToken(const std::string &token) const {
177 if (token.length() > 0 &&
178 token[0] != Arg::flagStartString()[0])
179 return false;
180
181 if (token.substr(0, Arg::nameStartString().length()) ==
183 return false;
184
185 if (token.find_first_of(this->getDelimiter()) != std::string::npos)
186 return false;
187
188 return true;
189}
190
191inline void SwitchArg::commonProcessing() {
192 if (_alreadySet)
193 throw(CmdLineParseException("Argument already set!", toString()));
194
195 _alreadySet = true;
196
197 _value = !_value;
198
200}
201
202inline bool SwitchArg::processArg(int *i, std::vector<std::string> &args) {
203 if (argMatches(args[*i])) {
204 // The whole string matches the flag or name string
205 _setBy = args[*i];
206 commonProcessing();
207
208 return true;
209 }
210
211 if (combinedSwitchesMatch(args[*i])) {
212 // A substring matches the flag as part of a combination
213 // check again to ensure we don't misinterpret
214 // this as a MultiSwitchArg
215 if (combinedSwitchesMatch(args[*i]))
216 throw(CmdLineParseException("Argument already set!", toString()));
217
218 commonProcessing();
219
220 // We only want to return true if we've found the last combined
221 // match in the string, otherwise return false so that other
222 // switches in the combination will have a chance to match.
223 return lastCombined(args[*i]);
224 }
225
226 return false;
227}
228
229inline void SwitchArg::reset() {
230 Arg::reset();
232}
233
234} // namespace TCLAP
235
236#endif // TCLAP_SWITCH_ARG_H
Interface that allows adding an Arg to a "container".
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
A virtual base class that defines the essential data for all arguments.
Definition Arg.h:66
static char blankChar()
The char used as a place holder when SwitchArgs are combined.
Definition Arg.h:247
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition Arg.h:775
static const std::string nameStartString()
Definition Arg.h:275
std::string _setBy
Indicates the value specified to set this flag (like -a or –all).
Definition Arg.h:162
bool _alreadySet
Indicates whether the argument has been set.
Definition Arg.h:158
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition Arg.h:817
bool matchesShortFlag(const std::string &flag) const
Definition Arg.h:681
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition Arg.h:755
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition Arg.h:765
static const std::string flagStartString()
Definition Arg.h:266
Thrown from CmdLine when the arguments on the command line are not properly specified,...
A simple switch argument.
Definition SwitchArg.h:40
bool _value
The value of the switch.
Definition SwitchArg.h:45
SwitchArg(const std::string &flag, const std::string &name, const std::string &desc, bool def=false, Visitor *v=NULL)
SwitchArg constructor.
Definition SwitchArg.h:134
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition SwitchArg.h:202
bool getValue() const
Returns the default value, toggled when the switch occurs.
Definition SwitchArg.h:106
bool _default
Used to support the reset() method so that ValueArg can be reset to their constructed value.
Definition SwitchArg.h:51
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:157
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition SwitchArg.h:229
A base class that defines the interface for visitors.
Definition Visitor.h:32
Definition Arg.h:52