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 
34 namespace TCLAP {
35 
40 class MultiSwitchArg : public SwitchArg {
41 protected:
45  int _value;
46 
51  int _default;
52 
53 public:
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 
106  std::string shortID(const std::string &val) const;
107 
111  std::string longID(const std::string &val) const;
112 
113  void reset();
114 };
115 
116 inline MultiSwitchArg::MultiSwitchArg(const std::string &flag,
117  const std::string &name,
118  const std::string &desc, int init,
119  Visitor *v)
120  : SwitchArg(flag, name, desc, false, v), _value(init), _default(init) {}
121 
122 inline MultiSwitchArg::MultiSwitchArg(const std::string &flag,
123  const std::string &name,
124  const std::string &desc,
125  ArgContainer &parser, int init,
126  Visitor *v)
127  : SwitchArg(flag, name, desc, false, v), _value(init), _default(init) {
128  parser.add(this);
129 }
130 
131 inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string> &args) {
132  if (argMatches(args[*i])) {
133  // so the isSet() method will work
134  _alreadySet = true;
135  _setBy = args[*i];
136 
137  // Matched argument: increment value.
138  ++_value;
139 
141 
142  return true;
143  } else if (combinedSwitchesMatch(args[*i])) {
144  // so the isSet() method will work
145  _alreadySet = true;
146 
147  // Matched argument: increment value.
148  ++_value;
149 
150  // Check for more in argument and increment value.
151  while (combinedSwitchesMatch(args[*i])) ++_value;
152 
154 
155  return false;
156  } else {
157  return false;
158  }
159 }
160 
161 inline std::string MultiSwitchArg::shortID(const std::string &val) const {
162  return Arg::shortID(val) + " ...";
163 }
164 
165 inline std::string MultiSwitchArg::longID(const std::string &val) const {
166  return Arg::longID(val) + " (accepted multiple times)";
167 }
168 
169 inline void MultiSwitchArg::reset() {
171 }
172 
173 } // namespace TCLAP
174 
175 #endif // TCLAP_MULTI_SWITCH_ARG_H
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:535
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:122
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
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
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:40
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:498
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:553
Interface that allows adding an Arg to a "container".
Definition: ArgContainer.h:39
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:32
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:485
std::string shortID(const std::string &val) const
Returns the shortID for this Arg.
int _value
The value of the switch.
void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:46
std::string _setBy
Indicates the value specified to set this flag (like -a or –all).
Definition: Arg.h:126
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.