tclap  1.2.0
SwitchArg.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * file: SwitchArg.h
5  *
6  * Copyright (c) 2003, Michael E. Smoot .
7  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
8  * All rights reverved.
9  *
10  * See the file COPYING in the top directory of this distribution for
11  * more information.
12  *
13  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19  * DEALINGS IN THE SOFTWARE.
20  *
21  *****************************************************************************/
22 
23 
24 #ifndef TCLAP_SWITCH_ARG_H
25 #define TCLAP_SWITCH_ARG_H
26 
27 #include <string>
28 #include <vector>
29 
30 #include <tclap/Arg.h>
31 
32 namespace TCLAP {
33 
39 class SwitchArg : public Arg
40 {
41  protected:
42 
46  bool _value;
47 
52  bool _default;
53 
54  public:
55 
68  SwitchArg(const std::string& flag,
69  const std::string& name,
70  const std::string& desc,
71  bool def = false,
72  Visitor* v = NULL);
73 
74 
88  SwitchArg(const std::string& flag,
89  const std::string& name,
90  const std::string& desc,
91  CmdLineInterface& parser,
92  bool def = false,
93  Visitor* v = NULL);
94 
95 
104  virtual bool processArg(int* i, std::vector<std::string>& args);
105 
110  bool combinedSwitchesMatch(std::string& combined);
111 
115  bool getValue();
116 
117  virtual void reset();
118 
119 };
120 
122 //BEGIN SwitchArg.cpp
124 inline SwitchArg::SwitchArg(const std::string& flag,
125  const std::string& name,
126  const std::string& desc,
127  bool default_val,
128  Visitor* v )
129 : Arg(flag, name, desc, false, false, v),
130  _value( default_val ),
131  _default( default_val )
132 { }
133 
134 inline SwitchArg::SwitchArg(const std::string& flag,
135  const std::string& name,
136  const std::string& desc,
137  CmdLineInterface& parser,
138  bool default_val,
139  Visitor* v )
140 : Arg(flag, name, desc, false, false, v),
141  _value( default_val ),
142  _default(default_val)
143 {
144  parser.add( this );
145 }
146 
147 inline bool SwitchArg::getValue() { return _value; }
148 
149 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
150 {
151  // make sure this is actually a combined switch
152  if ( combinedSwitches.length() > 0 &&
153  combinedSwitches[0] != Arg::flagStartString()[0] )
154  return false;
155 
156  // make sure it isn't a long name
157  if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
159  return false;
160 
161  // make sure the delimiter isn't in the string
162  if ( combinedSwitches.find_first_of( Arg::delimiter() ) != std::string::npos )
163  return false;
164 
165  // ok, we're not specifying a ValueArg, so we know that we have
166  // a combined switch list.
167  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
168  if ( _flag.length() > 0 &&
169  combinedSwitches[i] == _flag[0] &&
170  _flag[0] != Arg::flagStartString()[0] )
171  {
172  // update the combined switches so this one is no longer present
173  // this is necessary so that no unlabeled args are matched
174  // later in the processing.
175  //combinedSwitches.erase(i,1);
176  combinedSwitches[i] = Arg::blankChar();
177  return true;
178  }
179 
180  // none of the switches passed in the list match.
181  return false;
182 }
183 
184 
185 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
186 {
187  if ( _ignoreable && Arg::ignoreRest() )
188  return false;
189 
190  if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
191  {
192  // If we match on a combined switch, then we want to return false
193  // so that other switches in the combination will also have a
194  // chance to match.
195  bool ret = false;
196  if ( argMatches( args[*i] ) )
197  ret = true;
198 
199  if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) )
200  throw(CmdLineParseException("Argument already set!", toString()));
201 
202  _alreadySet = true;
203 
204  if ( _value == true )
205  _value = false;
206  else
207  _value = true;
208 
210 
211  return ret;
212  }
213  else
214  return false;
215 }
216 
217 inline void SwitchArg::reset()
218 {
219  Arg::reset();
220  _value = _default;
221 }
223 //End SwitchArg.cpp
225 
226 } //namespace TCLAP
227 
228 #endif