UniversalIndentGUI 1.2.0
|
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 00023 00024 #ifndef TCLAP_SWITCH_ARG_H 00025 #define TCLAP_SWITCH_ARG_H 00026 00027 #include <string> 00028 #include <vector> 00029 00030 #include <tclap/Arg.h> 00031 00032 namespace TCLAP { 00033 00039 class SwitchArg : public Arg 00040 { 00041 protected: 00042 00046 bool _value; 00047 00052 bool _default; 00053 00054 public: 00055 00068 SwitchArg(const std::string& flag, 00069 const std::string& name, 00070 const std::string& desc, 00071 bool def = false, 00072 Visitor* v = NULL); 00073 00074 00088 SwitchArg(const std::string& flag, 00089 const std::string& name, 00090 const std::string& desc, 00091 CmdLineInterface& parser, 00092 bool def = false, 00093 Visitor* v = NULL); 00094 00095 00104 virtual bool processArg(int* i, std::vector<std::string>& args); 00105 00110 bool combinedSwitchesMatch(std::string& combined); 00111 00115 bool getValue(); 00116 00117 virtual void reset(); 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_val, 00128 Visitor* v ) 00129 : Arg(flag, name, desc, false, false, v), 00130 _value( default_val ), 00131 _default( default_val ) 00132 { } 00133 00134 inline SwitchArg::SwitchArg(const std::string& flag, 00135 const std::string& name, 00136 const std::string& desc, 00137 CmdLineInterface& parser, 00138 bool default_val, 00139 Visitor* v ) 00140 : Arg(flag, name, desc, false, false, v), 00141 _value( default_val ), 00142 _default(default_val) 00143 { 00144 parser.add( this ); 00145 } 00146 00147 inline bool SwitchArg::getValue() { return _value; } 00148 00149 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches ) 00150 { 00151 // make sure this is actually a combined switch 00152 if ( combinedSwitches.length() > 0 && 00153 combinedSwitches[0] != Arg::flagStartString()[0] ) 00154 return false; 00155 00156 // make sure it isn't a long name 00157 if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) == 00158 Arg::nameStartString() ) 00159 return false; 00160 00161 // make sure the delimiter isn't in the string 00162 if ( combinedSwitches.find_first_of( Arg::delimiter() ) != std::string::npos ) 00163 return false; 00164 00165 // ok, we're not specifying a ValueArg, so we know that we have 00166 // a combined switch list. 00167 for ( unsigned int i = 1; i < combinedSwitches.length(); i++ ) 00168 if ( _flag.length() > 0 && 00169 combinedSwitches[i] == _flag[0] && 00170 _flag[0] != Arg::flagStartString()[0] ) 00171 { 00172 // update the combined switches so this one is no longer present 00173 // this is necessary so that no unlabeled args are matched 00174 // later in the processing. 00175 //combinedSwitches.erase(i,1); 00176 combinedSwitches[i] = Arg::blankChar(); 00177 return true; 00178 } 00179 00180 // none of the switches passed in the list match. 00181 return false; 00182 } 00183 00184 00185 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args) 00186 { 00187 if ( _ignoreable && Arg::ignoreRest() ) 00188 return false; 00189 00190 if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) ) 00191 { 00192 // If we match on a combined switch, then we want to return false 00193 // so that other switches in the combination will also have a 00194 // chance to match. 00195 bool ret = false; 00196 if ( argMatches( args[*i] ) ) 00197 ret = true; 00198 00199 if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) ) 00200 throw(CmdLineParseException("Argument already set!", toString())); 00201 00202 _alreadySet = true; 00203 00204 if ( _value == true ) 00205 _value = false; 00206 else 00207 _value = true; 00208 00209 _checkWithVisitor(); 00210 00211 return ret; 00212 } 00213 else 00214 return false; 00215 } 00216 00217 inline void SwitchArg::reset() 00218 { 00219 Arg::reset(); 00220 _value = _default; 00221 } 00223 //End SwitchArg.cpp 00225 00226 } //namespace TCLAP 00227 00228 #endif