tclap 1.2.6
MultiSwitchArg.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3
4/******************************************************************************
5*
6* file: MultiSwitchArg.h
7*
8* Copyright (c) 2003, Michael E. Smoot .
9* Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
10* Copyright (c) 2005, Michael E. Smoot, Daniel Aarno, Erik Zeek.
11* Copyright (c) 2017, Google LLC
12* All rights reserved.
13*
14* See the file COPYING in the top directory of this distribution for
15* more information.
16*
17* THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23* DEALINGS IN THE SOFTWARE.
24*
25*****************************************************************************/
26
27
28#ifndef TCLAP_MULTI_SWITCH_ARG_H
29#define TCLAP_MULTI_SWITCH_ARG_H
30
31#include <string>
32#include <vector>
33
34#include <tclap/SwitchArg.h>
35
36namespace TCLAP {
37
43{
44 protected:
45
49 int _value;
50
56
57 public:
58
72 MultiSwitchArg(const std::string& flag,
73 const std::string& name,
74 const std::string& desc,
75 int init = 0,
76 Visitor* v = NULL);
77
78
93 MultiSwitchArg(const std::string& flag,
94 const std::string& name,
95 const std::string& desc,
96 CmdLineInterface& parser,
97 int init = 0,
98 Visitor* v = NULL);
99
100
109 virtual bool processArg(int* i, std::vector<std::string>& args);
110
114 int getValue() const { return _value; }
115
117 operator bool() const { return _value != 0; }
118
122 std::string shortID(const std::string& val) const;
123
127 std::string longID(const std::string& val) const;
128
129 void reset();
130
131};
132
134//BEGIN MultiSwitchArg.cpp
136inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
137 const std::string& name,
138 const std::string& desc,
139 int init,
140 Visitor* v )
141: SwitchArg(flag, name, desc, false, v),
142_value( init ),
143_default( init )
144{ }
145
146inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
147 const std::string& name,
148 const std::string& desc,
149 CmdLineInterface& parser,
150 int init,
151 Visitor* v )
152: SwitchArg(flag, name, desc, false, v),
153_value( init ),
154_default( init )
155{
156 parser.add( this );
157}
158
159inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string>& args)
160{
161 if ( _ignoreable && Arg::ignoreRest() )
162 return false;
163
164 if ( argMatches( args[*i] ))
165 {
166 // so the isSet() method will work
167 _alreadySet = true;
168
169 // Matched argument: increment value.
170 ++_value;
171
173
174 return true;
175 }
176 else if ( combinedSwitchesMatch( args[*i] ) )
177 {
178 // so the isSet() method will work
179 _alreadySet = true;
180
181 // Matched argument: increment value.
182 ++_value;
183
184 // Check for more in argument and increment value.
185 while ( combinedSwitchesMatch( args[*i] ) )
186 ++_value;
187
189
190 return false;
191 }
192 else
193 return false;
194}
195
196inline std::string
197MultiSwitchArg::shortID(const std::string& val) const
198{
199 return Arg::shortID(val) + " ...";
200}
201
202inline std::string
203MultiSwitchArg::longID(const std::string& val) const
204{
205 return Arg::longID(val) + " (accepted multiple times)";
206}
207
208inline void
213
215//END MultiSwitchArg.cpp
217
218} //namespace TCLAP
219
220#endif
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition Arg.h:608
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition Arg.h:520
static bool ignoreRest()
Whether to ignore the rest.
Definition Arg.h:199
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
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition Arg.h:587
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition Arg.h:502
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.
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:42
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
A base class that defines the interface for visitors.
Definition Visitor.h:35
Definition Arg.h:48