tclap  1.2.2
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 
36 namespace TCLAP {
37 
42 class MultiSwitchArg : public SwitchArg
43 {
44  protected:
45 
49  int _value;
50 
55  int _default;
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
133 inline 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 
143 inline 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 
156 inline 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 
193 inline std::string
194 MultiSwitchArg::shortID(const std::string& val) const
195 {
196  return Arg::shortID(val) + " ...";
197 }
198 
199 inline std::string
200 MultiSwitchArg::longID(const std::string& val) const
201 {
202  return Arg::longID(val) + " (accepted multiple times)";
203 }
204 
205 inline void
207 {
209 }
210 
212 //END MultiSwitchArg.cpp
214 
215 } //namespace TCLAP
216 
217 #endif
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:581
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:128
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
int _default
Used to support the reset() method so that ValueArg can be reset to their constructed value...
std::string longID(const std::string &val) const
Returns the longID for this Arg.
A simple switch argument.
Definition: SwitchArg.h:41
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:514
MultiSwitchArg(const std::string &flag, const std::string &name, const std::string &desc, int init=0, Visitor *v=NULL)
MultiSwitchArg constructor.
A multiple switch argument.
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition: Arg.h:602
int getValue() const
Returns int, the number of times the switch has been set.
A base class that defines the interface for visitors.
Definition: Visitor.h:34
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...
std::string shortID(const std::string &val) const
Returns the shortID for this Arg.
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:141
int _value
The value of the switch.
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:196
void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:48
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.