tclap  1.2.5
SwitchArg.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 /******************************************************************************
4  *
5  * file: SwitchArg.h
6  *
7  * Copyright (c) 2003, Michael E. Smoot .
8  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
9  * Copyright (c) 2017, Google LLC
10  * All rights reserved.
11  *
12  * See the file COPYING in the top directory of this distribution for
13  * more information.
14  *
15  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  *****************************************************************************/
24 
25 
26 #ifndef TCLAP_SWITCH_ARG_H
27 #define TCLAP_SWITCH_ARG_H
28 
29 #include <string>
30 #include <vector>
31 
32 #include <tclap/Arg.h>
33 
34 namespace TCLAP {
35 
41 class SwitchArg : public Arg
42 {
43 protected:
44 
48  bool _value;
49 
54  bool _default;
55 
56 public:
57 
70  SwitchArg(const std::string& flag,
71  const std::string& name,
72  const std::string& desc,
73  bool def = false,
74  Visitor* v = NULL);
75 
76 
90  SwitchArg(const std::string& flag,
91  const std::string& name,
92  const std::string& desc,
93  CmdLineInterface& parser,
94  bool def = false,
95  Visitor* v = NULL);
96 
97 
106  virtual bool processArg(int* i, std::vector<std::string>& args);
107 
112  bool combinedSwitchesMatch(std::string& combined);
113 
117  bool getValue() const { return _value; }
118 
124  operator bool() const { return _value; }
125 
126  virtual void reset();
127 
128 private:
133  bool lastCombined(std::string& combined);
134 
138  void commonProcessing();
139 };
140 
142 //BEGIN SwitchArg.cpp
144 inline SwitchArg::SwitchArg(const std::string& flag,
145  const std::string& name,
146  const std::string& desc,
147  bool default_val,
148  Visitor* v )
149  : Arg(flag, name, desc, false, false, v),
150  _value( default_val ),
151  _default( default_val )
152 { }
153 
154 inline SwitchArg::SwitchArg(const std::string& flag,
155  const std::string& name,
156  const std::string& desc,
157  CmdLineInterface& parser,
158  bool default_val,
159  Visitor* v )
160  : Arg(flag, name, desc, false, false, v),
161  _value( default_val ),
162  _default(default_val)
163 {
164  parser.add( this );
165 }
166 
167 inline bool SwitchArg::lastCombined(std::string& combinedSwitches )
168 {
169  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
170  if ( combinedSwitches[i] != Arg::blankChar() )
171  return false;
172 
173  return true;
174 }
175 
176 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
177 {
178  // make sure this is actually a combined switch
179  if ( combinedSwitches.length() > 0 &&
180  combinedSwitches[0] != Arg::flagStartString()[0] )
181  return false;
182 
183  // make sure it isn't a long name
184  if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
186  return false;
187 
188  // make sure the delimiter isn't in the string
189  if ( combinedSwitches.find_first_of(Arg::delimiter()) != std::string::npos)
190  return false;
191 
192  // ok, we're not specifying a ValueArg, so we know that we have
193  // a combined switch list.
194  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
195  if ( _flag.length() > 0 &&
196  combinedSwitches[i] == _flag[0] &&
197  _flag[0] != Arg::flagStartString()[0] )
198  {
199  // update the combined switches so this one is no longer present
200  // this is necessary so that no unlabeled args are matched
201  // later in the processing.
202  //combinedSwitches.erase(i,1);
203  combinedSwitches[i] = Arg::blankChar();
204  return true;
205  }
206 
207  // none of the switches passed in the list match.
208  return false;
209 }
210 
211 inline void SwitchArg::commonProcessing()
212 {
213  if ( _xorSet )
214  throw(CmdLineParseException(
215  "Mutually exclusive argument already set!", toString()));
216 
217  if ( _alreadySet )
218  throw(CmdLineParseException("Argument already set!", toString()));
219 
220  _alreadySet = true;
221 
222  if ( _value == true )
223  _value = false;
224  else
225  _value = true;
226 
228 }
229 
230 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
231 {
232  if ( _ignoreable && Arg::ignoreRest() )
233  return false;
234