tclap 1.4.0
MultiSwitchArg.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: MultiSwitchArg.h
6 *
7 * Copyright (c) 2003, Michael E. Smoot .
8 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
9 * Copyright (c) 2005, Michael E. Smoot, Daniel Aarno, Erik Zeek.
10 * Copyright (c) 2017, Google LLC
11 * All rights reserved.
12 *
13 * See the file COPYING in the top directory of this distribution for
14 * more information.
15 *
16 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 *****************************************************************************/
25
26#ifndef TCLAP_MULTI_SWITCH_ARG_H
27#define TCLAP_MULTI_SWITCH_ARG_H
28
29#include <tclap/SwitchArg.h>
30
31#include <string>
32#include <vector>
33
34namespace TCLAP {
35
40class MultiSwitchArg : public SwitchArg {
41protected:
45 int _value;
46
52
53public:
67 MultiSwitchArg(const std::string &flag, const std::string &name,
68 const std::string &desc, int init = 0, Visitor *v = NULL);
69
84 MultiSwitchArg(const std::string &flag, const std::string &name,
85 const std::string &desc, ArgContainer &parser, int init = 0,
86 Visitor *v = NULL);
87
96 virtual bool processArg(int *i, std::vector<std::string> &args);
97
101 int getValue() const { return _value; }
102
104 operator bool() const { return _value != 0; }
105
109 std::string shortID(const std::string &val) const;
110
114 std::string longID(const std::string &val) const;
115
116 void reset();
117};
118
119inline MultiSwitchArg::MultiSwitchArg(const std::string &flag,
120 const std::string &name,
121 const std::string &desc, int init,
122 Visitor *v)
123 : SwitchArg(flag, name, desc, false, v), _value(init), _default(init) {}
124
125inline MultiSwitchArg::MultiSwitchArg(const std::string &flag,
126 const std::string &name,
127 const std::string &desc,
128 ArgContainer &parser, int init,
129 Visitor *v)
130 : SwitchArg(flag, name, desc, false, v), _value(init), _default(init) {
131 parser.add(this);
132}
133
134inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string> &args) {
135 if (argMatches(args[*i])) {
136 // so the isSet() method will work
137 _alreadySet = true;
138 _setBy = args[*i];
139
140 // Matched argument: increment value.
141 ++_value;
142
144
145 return true;
146 } else if (combinedSwitchesMatch(args[*i])) {
147 // so the isSet() method will work
148 _alreadySet = true;
149
150 // Matched argument: increment value.
151 ++_value;
152
153 // Check for more in argument and increment value.
154 while (combinedSwitchesMatch(args[*i])) ++_value;
155
157
158 return false;
159 } else {
160 return false;
161 }
162}
163
164inline std::string MultiSwitchArg::shortID(const std::string &val) const {
165 return Arg::shortID(val) + " ...";
166}
167
168inline std::string MultiSwitchArg::longID(const std::string &val) const {
169 return Arg::longID(val) + " (accepted multiple times)";
170}
171
176
177} // namespace TCLAP
178
179#endif // TCLAP_MULTI_SWITCH_ARG_H
Interface that allows adding an Arg to a "container".
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition Arg.h:775
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition Arg.h:581
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
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 shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition Arg.h:568
A multiple switch argument.
MultiSwitchArg(const std::string &flag, const std::string &name, const std::string &desc, int init=0, Visitor *v=NULL)
MultiSwitchArg constructor.
int getValue() const
Returns int, the number of times the switch has been set.
std::string shortID(const std::string &val) const
Returns the shortID for this Arg.
std::string longID(const std::string &val) const
Returns the longID for this Arg.
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
int _value
The value of the switch.
void reset()
Clears the Arg object and allows it to be reused by new command lines.
int _default
Used to support the reset() method so that ValueArg can be reset to their constructed value.
A simple switch argument.
Definition SwitchArg.h:40
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
A base class that defines the interface for visitors.
Definition Visitor.h:32
Definition Arg.h:52