![]() |
VR-Forces 4.0.4 Class Documentation
|
00001 00002 /****************************************************************************** 00003 * 00004 * file: SwitchArg.h 00005 * 00006 * Copyright (c) 2003, Michael E. Smoot . 00007 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno. 00008 * All rights reverved. 00009 * 00010 * See the file COPYING in the top directory of this distribution for 00011 * more information. 00012 * 00013 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 00014 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00015 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00016 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00017 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00018 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00019 * DEALINGS IN THE SOFTWARE. 00020 * 00021 *****************************************************************************/ 00022 00026 #pragma once 00027 00028 #include <string> 00029 #include <vector> 00030 00031 #include <cmdLine/cmdArg.h> 00032 00033 namespace makVrv { 00039 00040 00041 00042 class SwitchArg : public DtCmdLine::Arg 00043 { 00044 protected: 00045 00049 bool _value; 00050 00051 00052 public: 00053 00066 SwitchArg(const std::string& flag, 00067 const std::string& name, 00068 const std::string& desc, 00069 bool def, 00070 DtCmdLine::Visitor* v = NULL); 00071 00072 00086 SwitchArg(const std::string& flag, 00087 const std::string& name, 00088 const std::string& desc, 00089 bool def, 00090 DtCmdLine::CmdLineInterface& parser, 00091 DtCmdLine::Visitor* v = NULL); 00092 00093 00102 virtual bool processArg(int* i, std::vector<std::string>& args); 00103 00108 bool combinedSwitchesMatch(std::string& combined); 00109 00113 bool getValue() const; 00114 00117 operator bool(); 00118 00119 }; 00120 00122 //BEGIN SwitchArg.cpp 00124 inline SwitchArg::SwitchArg(const std::string& flag, 00125 const std::string& name, 00126 const std::string& desc, 00127 bool _default, 00128 DtCmdLine::Visitor* v ) 00129 : DtCmdLine::Arg(flag, name, desc, false, false, v), 00130 _value( _default ) 00131 { } 00132 00133 inline SwitchArg::SwitchArg(const std::string& flag, 00134 const std::string& name, 00135 const std::string& desc, 00136 bool _default, 00137 DtCmdLine::CmdLineInterface& parser, 00138 DtCmdLine::Visitor* v ) 00139 : Arg(flag, name, desc, false, false, v), 00140 _value( _default ) 00141 { 00142 parser.add( this ); 00143 } 00144 00145 inline bool SwitchArg::getValue() const { return _value; } 00146 00147 inline SwitchArg::operator bool() { return _value; } 00148 00149 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches ) 00150 { 00152 if ( combinedSwitches[0] != Arg::flagStartString()[0] ) 00153 return false; 00154 00156 if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) == 00157 Arg::nameStartString() ) 00158 return false; 00159 00162 for ( unsigned int i = 1; i < combinedSwitches.length(); i++ ) 00163 if ( !_flag.empty() && combinedSwitches[i] == _flag[0] ) 00164 { 00168 //combinedSwitches.erase(i,1); 00169 combinedSwitches[i] = Arg::blankChar(); 00170 return true; 00171 } 00172 00174 return false; 00175 } 00176 00177 00178 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args) 00179 { 00180 if ( _ignoreable && Arg::ignoreRest() ) 00181 return false; 00182 00183 if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) ) 00184 { 00188 bool ret = false; 00189 if ( argMatches( args[*i] ) ) 00190 ret = true; 00191 00192 if ( _alreadySet ) 00193 throw(DtCmdLine::CmdLineParseException("Argument already set!", toString())); 00194 00195 _alreadySet = true; 00196 00197 if ( _value == true ) 00198 _value = false; 00199 else 00200 _value = true; 00201 00202 _checkWithVisitor(); 00203 00204 return ret; 00205 } 00206 else 00207 return false; 00208 } 00209 00211 //End SwitchArg.cpp 00213 }