VR-Forces 4.3 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DtVrvCmdSwitchArg.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 
26 #pragma once
27 
28 #include <string>
29 #include <vector>
30 
31 #include <cmdLine/cmdArg.h>
32 
33 namespace makVrv {
39 
40 
41 
42  class SwitchArg : public DtCmdLine::Arg
43  {
44  protected:
45 
49  bool _value;
50 
51 
52  public:
53 
66  SwitchArg(const std::string& flag,
67  const std::string& name,
68  const std::string& desc,
69  bool def,
70  DtCmdLine::Visitor* v = NULL);
71 
72 
86  SwitchArg(const std::string& flag,
87  const std::string& name,
88  const std::string& desc,
89  bool def,
90  DtCmdLine::CmdLineInterface& parser,
91  DtCmdLine::Visitor* v = NULL);
92 
93 
102  virtual bool processArg(int* i, std::vector<std::string>& args);
103 
108  bool combinedSwitchesMatch(std::string& combined);
109 
113  bool getValue() const;
114 
117  operator bool();
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,
128  DtCmdLine::Visitor* v )
129  : DtCmdLine::Arg(flag, name, desc, false, false, v),
130  _value( _default )
131  { }
132 
133  inline SwitchArg::SwitchArg(const std::string& flag,
134  const std::string& name,
135  const std::string& desc,
136  bool _default,
137  DtCmdLine::CmdLineInterface& parser,
138  DtCmdLine::Visitor* v )
139  : Arg(flag, name, desc, false, false, v),
140  _value( _default )
141  {
142  parser.add( this );
143  }
144 
145  inline bool SwitchArg::getValue() const { return _value; }
146 
147  inline SwitchArg::operator bool() { return _value; }
148 
149  inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
150  {
152  if ( combinedSwitches[0] != Arg::flagStartString()[0] )
153  return false;
154 
156  if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
157  Arg::nameStartString() )
158  return false;
159 
162  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
163  if ( !_flag.empty() && combinedSwitches[i] == _flag[0] )
164  {
168  //combinedSwitches.erase(i,1);
169  combinedSwitches[i] = Arg::blankChar();
170  return true;
171  }
172 
174  return false;
175  }
176 
177 
178  inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
179  {
180  if ( _ignoreable && Arg::ignoreRest() )
181  return false;
182 
183  if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
184  {
188  bool ret = false;
189  if ( argMatches( args[*i] ) )
190  ret = true;
191 
192  if ( _alreadySet )
193  throw(DtCmdLine::CmdLineParseException("Argument already set!", toString()));
194 
195  _alreadySet = true;
196 
197  if ( _value == true )
198  _value = false;
199  else
200  _value = true;
201 
202  _checkWithVisitor();
203 
204  return ret;
205  }
206  else
207  return false;
208  }
209 
211  //End SwitchArg.cpp
213 }

Document ID: Generated on Wed Mar 11 21:20:57 EDT 2015 from SVN revision 150940
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)