tclap 1.6.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 = nullptr);
68
82 SwitchArg(const std::string &flag, const std::string &name,
83 const std::string &desc, ArgContainer &parser, bool def = false,
84 Visitor *v = nullptr);
85
94 bool processArg(int *i, std::vector<std::string> &args) override;
95
100 bool combinedSwitchesMatch(std::string &combined);
101
106 [[nodiscard]] bool getValue() const noexcept { return _value; }
107
112 operator bool() const noexcept { return _value; }
113
114 void reset() override;
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 return combinedSwitches.find_first_not_of(Arg::blankChar(), 1) ==
152 std::string::npos;
153}
154
155inline bool SwitchArg::combinedSwitchesMatch(std::string &combinedSwitches) {
156 if (!isCombinedSwitchToken(combinedSwitches)) return false;
157
158 for (unsigned int i = 1; i < combinedSwitches.length(); i++) {
159 if (combinedSwitches[i] != Arg::flagStartString()[0] &&
160 matchesShortFlag(std::string(1, combinedSwitches[i]))) {
161 // update the combined switches so this one is no longer
162 // present this is necessary so that no unlabeled args are
163 // matched later in the processing.
164 _setBy = Arg::flagStartString() + combinedSwitches[i];
165 combinedSwitches[i] = Arg::blankChar();
166 return true;
167 }
168 }
169
170 // none of the switches passed in the list match.
171 return false;
172}
173
174inline bool SwitchArg::isCombinedSwitchToken(const std::string &token) const {
175 if (!token.empty() && token[0] != Arg::flagStartString()[0]) return false;
176
177 if (token.substr(0, Arg::nameStartString().length()) ==
179 return false;
180
181 if (token.find_first_of(this->getDelimiter()) != std::string::npos)
182 return false;
183
184 return true;
185}
186
187inline void SwitchArg::commonProcessing() {
188 if (_alreadySet)
189 throw(CmdLineParseException("Argument already set!", toString()));
190
191 _alreadySet = true;
192 _value = !_value;
193
195}
196
197inline bool SwitchArg::processArg(int *i, std::vector<std::string> &args) {
198 if (argMatches(args[*i])) {
199 // The whole string matches the flag or name string
200 _setBy = args[*i];
201 commonProcessing();
202
203 return true;
204 }
205
206 if (combinedSwitchesMatch(args[*i])) {
207 // A substring matches the flag as part of a combination
208 // check again to ensure we don't misinterpret
209 // this as a MultiSwitchArg
210 if (combinedSwitchesMatch(args[*i]))
211 throw(CmdLineParseException("Argument already set!", toString()));
212
213 commonProcessing();
214
215 // We only want to return true if we've found the last combined
216 // match in the string, otherwise return false so that other
217 // switches in the combination will have a chance to match.
218 return lastCombined(args[*i]);
219 }
220
221 return false;
222}
223
224inline void SwitchArg::reset() {
225 Arg::reset();
227}
228
229} // namespace TCLAP
230
231#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:69
static std::string flagStartString()
Definition Arg.h:266
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition Arg.h:789
std::string _setBy
Indicates the value specified to set this flag (like -a or –all).
Definition Arg.h:162
static constexpr char blankChar() noexcept
The char used as a place holder when SwitchArgs are combined.
Definition Arg.h:247
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:823
bool matchesShortFlag(const std::string &flag) const
Definition Arg.h:674
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition Arg.h:769
static std::string nameStartString()
Definition Arg.h:275
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition Arg.h:779
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
void reset() override
Clears the Arg object and allows it to be reused by new command lines.
Definition SwitchArg.h:224
bool processArg(int *i, std::vector< std::string > &args) override
Handles the processing of the argument.
Definition SwitchArg.h:197
bool getValue() const noexcept
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:155
SwitchArg(const std::string &flag, const std::string &name, const std::string &desc, bool def=false, Visitor *v=nullptr)
SwitchArg constructor.
Definition SwitchArg.h:134
A base class that defines the interface for visitors.
Definition Visitor.h:32
Definition Arg.h:54