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 <mrpt/otherlibs/tclap/Arg.h> 00031 00032 namespace TCLAP { 00033 00034 /** 00035 * A simple switch argument. If the switch is set on the command line, then 00036 * the getValue method will return the opposite of the default value for the 00037 * switch. 00038 */ 00039 class SwitchArg : public Arg 00040 { 00041 protected: 00042 00043 /** 00044 * The value of the switch. 00045 */ 00046 bool _value; 00047 00048 public: 00049 00050 /** 00051 * SwitchArg constructor. 00052 * \param flag - The one character flag that identifies this 00053 * argument on the command line. 00054 * \param name - A one word name for the argument. Can be 00055 * used as a long flag on the command line. 00056 * \param desc - A description of what the argument is for or 00057 * does. 00058 * \param def - The default value for this Switch. 00059 * \param v - An optional visitor. You probably should not 00060 * use this unless you have a very good reason. 00061 */ 00062 SwitchArg(const std::string& flag, 00063 const std::string& name, 00064 const std::string& desc, 00065 bool def = false, 00066 Visitor* v = NULL); 00067 00068 00069 /** 00070 * SwitchArg constructor. 00071 * \param flag - The one character flag that identifies this 00072 * argument on the command line. 00073 * \param name - A one word name for the argument. Can be 00074 * used as a long flag on the command line. 00075 * \param desc - A description of what the argument is for or 00076 * does. 00077 * \param parser - A CmdLine parser object to add this Arg to 00078 * \param def - The default value for this Switch. 00079 * \param v - An optional visitor. You probably should not 00080 * use this unless you have a very good reason. 00081 */ 00082 SwitchArg(const std::string& flag, 00083 const std::string& name, 00084 const std::string& desc, 00085 CmdLineInterface& parser, 00086 bool def = false, 00087 Visitor* v = NULL); 00088 00089 00090 /** 00091 * Handles the processing of the argument. 00092 * This re-implements the Arg version of this method to set the 00093 * _value of the argument appropriately. 00094 * \param i - Pointer the the current argument in the list. 00095 * \param args - Mutable list of strings. Passed 00096 * in from main(). 00097 */ 00098 virtual bool processArg(int* i, std::vector<std::string>& args); 00099 00100 /** 00101 * Checks a string to see if any of the chars in the string 00102 * match the flag for this Switch. 00103 */ 00104 bool combinedSwitchesMatch(std::string& combined); 00105 00106 /** 00107 * Returns bool, whether or not the switch has been set. 00108 */ 00109 bool getValue(); 00110 00111 }; 00112 00113 ////////////////////////////////////////////////////////////////////// 00114 //BEGIN SwitchArg.cpp 00115 ////////////////////////////////////////////////////////////////////// 00116 inline SwitchArg::SwitchArg(const std::string& flag, 00117 const std::string& name, 00118 const std::string& desc, 00119 bool _default, 00120 Visitor* v ) 00121 : Arg(flag, name, desc, false, false, v), 00122 _value( _default ) 00123 { } 00124 00125 inline SwitchArg::SwitchArg(const std::string& flag, 00126 const std::string& name, 00127 const std::string& desc, 00128 CmdLineInterface& parser, 00129 bool _default, 00130 Visitor* v ) 00131 : Arg(flag, name, desc, false, false, v), 00132 _value( _default ) 00133 { 00134 parser.add( this ); 00135 } 00136 00137 inline bool SwitchArg::getValue() { return _value; } 00138 00139 inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches ) 00140 { 00141 // make sure this is actually a combined switch 00142 if ( combinedSwitches[0] != Arg::flagStartString()[0] ) 00143 return false; 00144 00145 // make sure it isn't a long name 00146 if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) == 00147 Arg::nameStartString() ) 00148 return false; 00149 00150 // ok, we're not specifying a ValueArg, so we know that we have 00151 // a combined switch list. 00152 for ( unsigned int i = 1; i < combinedSwitches.length(); i++ ) 00153 if ( combinedSwitches[i] == _flag[0] ) 00154 { 00155 // update the combined switches so this one is no longer present 00156 // this is necessary so that no unlabeled args are matched 00157 // later in the processing. 00158 //combinedSwitches.erase(i,1); 00159 combinedSwitches[i] = Arg::blankChar(); 00160 return true; 00161 } 00162 00163 // none of the switches passed in the list match. 00164 return false; 00165 } 00166 00167 00168 inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args) 00169 { 00170 if ( _ignoreable && Arg::ignoreRest() ) 00171 return false; 00172 00173 if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) ) 00174 { 00175 // If we match on a combined switch, then we want to return false 00176 // so that other switches in the combination will also have a 00177 // chance to match. 00178 bool ret = false; 00179 if ( argMatches( args[*i] ) ) 00180 ret = true; 00181 00182 if ( _alreadySet || ( !ret && combinedSwitchesMatch( args[*i] ) ) ) 00183 throw(CmdLineParseException("Argument already set!", toString())); 00184 00185 _alreadySet = true; 00186 00187 if ( _value == true ) 00188 _value = false; 00189 else 00190 _value = true; 00191 00192 _checkWithVisitor(); 00193 00194 return ret; 00195 } 00196 else 00197 return false; 00198 } 00199 00200 ////////////////////////////////////////////////////////////////////// 00201 //End SwitchArg.cpp 00202 ////////////////////////////////////////////////////////////////////// 00203 00204 } //namespace TCLAP 00205 00206 #endif
Page generated by Doxygen 1.7.2 for MRPT 0.9.4 SVN: at Mon Jan 10 22:46:17 UTC 2011 |