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,
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  private:
124  bool lastCombined(std::string& combined);
125 
129  void commonProcessing();
130 };
131 
133 //BEGIN SwitchArg.cpp
135 inline SwitchArg::SwitchArg(const std::string& flag,
136  const std::string& name,
137  const std::string& desc,
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 
145 inline SwitchArg::SwitchArg(const std::string& flag,
146  const std::string& name,
147  const std::string& desc,
149  bool default_val,
150  Visitor* v )
151 : Arg(flag, name, desc, false, false, v),
152  _value( default_val ),
153  _default(default_val)
154 {
155  parser.add( this );
156 }
157 
158 inline bool SwitchArg::getValue() { return _value; }
159 
160 inline bool SwitchArg::lastCombined(std::string& combinedSwitches )
161 {
162  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
163  if ( combinedSwitches[i] != Arg::blankChar() )
164  return false;
165 
166  return true;
167 }
168 
169 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
170 {
171  // make sure this is actually a combined switch
172  if ( combinedSwitches.length() > 0 &&
173  combinedSwitches[0] != Arg::flagStartString()[0] )
174  return false;
175 
176  // make sure it isn't a long name
177  if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
179  return false;
180 
181  // make sure the delimiter isn't in the string
182  if ( combinedSwitches.find_first_of( Arg::delimiter() ) != std::string::npos )
183  return false;
184 
185  // ok, we're not specifying a ValueArg, so we know that we have
186  // a combined switch list.
187  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
188  if ( _flag.length() > 0 &&
189  combinedSwitches[i] == _flag[0] &&
190  _flag[0] != Arg::flagStartString()[0] )
191  {
192  // update the combined switches so this one is no longer present
193  // this is necessary so that no unlabeled args are matched
194  // later in the processing.
195  //combinedSwitches.erase(i,1);
196  combinedSwitches[i] = Arg::blankChar();
197  return true;
198  }
199 
200  // none of the switches passed in the list match.
201  return false;
202 }
203 
205 {
206  if ( _xorSet )
207  throw(CmdLineParseException(
208  "Mutually exclusive argument already set!", toString()));
209 
210  if ( _alreadySet )
211  throw(CmdLineParseException("Argument already set!", toString()));
212 
213  _alreadySet = true;
214 
215  if ( _value == true )
216  _value = false;
217  else
218  _value = true;
219 
221 }
222 
223 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
224 {
225  if ( _ignoreable && Arg::ignoreRest() )
226  return false;
227 
228  // if the whole string matches the flag or name string
229  if ( argMatches( args[*i] ) )
230  {
232 
233  return true;
234  }
235  // if a substring matches the flag as part of a combination
236  else if ( combinedSwitchesMatch( args[*i] ) )
237  {
238  // check again to ensure we don't misinterpret
239  // this as a MultiSwitchArg
240  if ( combinedSwitchesMatch( args[*i] ) )
241  throw(CmdLineParseException("Argument already set!",
242  toString()));
243 
245 
246  // We only want to return true if we've found the last combined
247  // match in the string, otherwise we return true so that other
248  // switches in the combination will have a chance to match.
249  return lastCombined( args[*i] );
250  }
251  else
252  return false;
253 }
254 
255 inline void SwitchArg::reset()
256 {
257  Arg::reset();
258  _value = _default;
259 }
261 //End SwitchArg.cpp
263 
264 } //namespace TCLAP
265 
266 #endif
bool _alreadySet
Definition: Arg.h:137
bool combinedSwitchesMatch(std::string &combined)
Definition: SwitchArg.h:169
Definition: Arg.h:64
void commonProcessing()
Definition: SwitchArg.h:204
GLuint const GLchar * name
parser
Definition: enums.py:61
GLsizei const GLchar *const * string
bool lastCombined(std::string &combined)
Definition: SwitchArg.h:160
void _checkWithVisitor() const
Definition: Arg.h:611
virtual void reset()
Definition: Arg.h:679
bool _xorSet
Definition: Arg.h:156
static char delimiter()
Definition: Arg.h:211
virtual bool processArg(int *i, std::vector< std::string > &args)
Definition: SwitchArg.h:223
static const std::string nameStartString()
Definition: Arg.h:245
virtual void add(Arg &a)=0
bool _ignoreable
Definition: Arg.h:150
static bool ignoreRest()
Definition: Arg.h:205
virtual void reset()
Definition: SwitchArg.h:255
virtual bool argMatches(const std::string &s) const
Definition: Arg.h:590
static char blankChar()
Definition: Arg.h:217
#define NULL
Definition: tinycthread.c:47
int i
SwitchArg(const std::string &flag, const std::string &name, const std::string &desc, bool def=false, Visitor *v=NULL)
Definition: SwitchArg.h:135
std::string _flag
Definition: Arg.h:98
Definition: Arg.h:57
virtual std::string toString() const
Definition: Arg.h:599
GLdouble v
static const std::string flagStartString()
Definition: Arg.h:236


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11