tclap 1.2.5
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
119 std::string shortID(const std::string& val) const;
120
124 std::string longID(const std::string& val) const;
125
126 void reset();
127
128};
129
131//BEGIN MultiSwitchArg.cpp
133inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
134 const std::string& name,
135 const std::string& desc,
136 int init,
137 Visitor* v )
138: SwitchArg(flag, name, desc, false, v),
139_value( init ),
140_default( init )
141{ }
142
143inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
144 const std::string& name,
145 const std::string& desc,
146 CmdLineInterface& parser,
147 int init,
148 Visitor* v )
149: SwitchArg(flag, name, desc, false, v),
150_value( init ),
151_default( init )
152{
153 parser.add( this );
154}
155
156inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string>& args)
157{
158 if ( _ignoreable && Arg::ignoreRest() )
159 return false;
160
161 if ( argMatches( args[*i] ))
162 {
163 // so the isSet() method will work
164 _alreadySet = true;
165
166 // Matched argument: increment value.
167 ++_value;
168
170
171 return true;
172 }
173 else if ( combinedSwitchesMatch( args[*i] ) )
174 {
175 // so the isSet() method will work
176 _alreadySet = true;
177
178 // Matched argument: increment value.
179 ++_value;
180
181 // Check for more in argument and increment value.
182 while ( combinedSwitchesMatch( args[*i] ) )
183 ++_value;
184
186
187 return false;
188 }
189 else
190 return false;
191}
192
193inline std::string
194MultiSwitchArg::shortID(const std::string& val) const
195{
196 return Arg::shortID(val) + " ...";
197}
198
199inline std::string
200MultiSwitchArg::longID(const std::string& val) const
201{
202 return Arg::longID(val) + " (accepted multiple times)";
203}
204
205inline void
207{
209}
210
212//END MultiSwitchArg.cpp
214
215} //namespace TCLAP
216
217#endif
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition: Arg.h:602
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:514
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:196
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:581
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:496
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