Main MRPT website > C++ reference
MRPT logo

MultiSwitchArg.h

Go to the documentation of this file.
00001 
00002 /****************************************************************************** 
00003 *
00004 *  file:  MultiSwitchArg.h
00005 *
00006 *  Copyright (c) 2003, Michael E. Smoot .
00007 *  Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
00008 *  Copyright (c) 2005, Michael E. Smoot, Daniel Aarno, Erik Zeek.
00009 *  All rights reverved.
00010 *
00011 *  See the file COPYING in the top directory of this distribution for
00012 *  more information.
00013 *
00014 *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00017 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00019 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00020 *  DEALINGS IN THE SOFTWARE.
00021 *
00022 *****************************************************************************/
00023 
00024 
00025 #ifndef TCLAP_MULTI_SWITCH_ARG_H
00026 #define TCLAP_MULTI_SWITCH_ARG_H
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 #include <mrpt/otherlibs/tclap/SwitchArg.h>
00032 
00033 namespace TCLAP {
00034 
00035 /**
00036 * A multiple switch argument.  If the switch is set on the command line, then
00037 * the getValue method will return the number of times the switch appears.
00038 */
00039 class MultiSwitchArg : public SwitchArg
00040 {
00041         protected:
00042 
00043                 /**
00044                  * The value of the switch.
00045                  */
00046                 int _value;
00047 
00048 
00049         public:
00050 
00051                 /**
00052                  * MultiSwitchArg constructor.
00053                  * \param flag - The one character flag that identifies this
00054                  * argument on the command line.
00055                  * \param name - A one word name for the argument.  Can be
00056                  * used as a long flag on the command line.
00057                  * \param desc - A description of what the argument is for or
00058                  * does.
00059                  * \param init - Optional. The initial/default value of this Arg. 
00060                  * Defaults to 0.
00061                  * \param v - An optional visitor.  You probably should not
00062                  * use this unless you have a very good reason.
00063                  */
00064                 MultiSwitchArg(const std::string& flag, 
00065                                 const std::string& name,
00066                                 const std::string& desc,
00067                                 int init = 0,
00068                                 Visitor* v = NULL);
00069 
00070 
00071                 /**
00072                  * MultiSwitchArg constructor.
00073                  * \param flag - The one character flag that identifies this
00074                  * argument on the command line.
00075                  * \param name - A one word name for the argument.  Can be
00076                  * used as a long flag on the command line.
00077                  * \param desc - A description of what the argument is for or
00078                  * does.
00079                  * \param parser - A CmdLine parser object to add this Arg to
00080                  * \param init - Optional. The initial/default value of this Arg. 
00081                  * Defaults to 0.
00082                  * \param v - An optional visitor.  You probably should not
00083                  * use this unless you have a very good reason.
00084                  */
00085                 MultiSwitchArg(const std::string& flag, 
00086                                 const std::string& name,
00087                                 const std::string& desc,
00088                                 CmdLineInterface& parser,
00089                                 int init = 0,
00090                                 Visitor* v = NULL);
00091 
00092 
00093                 /**
00094                  * Handles the processing of the argument.
00095                  * This re-implements the SwitchArg version of this method to set the
00096                  * _value of the argument appropriately.
00097                  * \param i - Pointer the the current argument in the list.
00098                  * \param args - Mutable list of strings. Passed
00099                  * in from main().
00100                  */
00101                 virtual bool processArg(int* i, std::vector<std::string>& args); 
00102 
00103                 /**
00104                  * Returns int, the number of times the switch has been set.
00105                  */
00106                 int getValue();
00107 
00108                 /**
00109                  * Returns the shortID for this Arg.
00110                  */
00111                 std::string shortID(const std::string& val) const;
00112 
00113                 /**
00114                  * Returns the longID for this Arg.
00115                  */
00116                 std::string longID(const std::string& val) const;
00117 };
00118 
00119 //////////////////////////////////////////////////////////////////////
00120 //BEGIN MultiSwitchArg.cpp
00121 //////////////////////////////////////////////////////////////////////
00122 inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
00123                                         const std::string& name,
00124                                         const std::string& desc,
00125                                         int init,
00126                                         Visitor* v )
00127 : SwitchArg(flag, name, desc, false, v),
00128 _value( init )
00129 { }
00130 
00131 inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
00132                                         const std::string& name, 
00133                                         const std::string& desc, 
00134                                         CmdLineInterface& parser,
00135                                         int init,
00136                                         Visitor* v )
00137 : SwitchArg(flag, name, desc, false, v),
00138 _value( init )
00139 { 
00140         parser.add( this );
00141 }
00142 
00143 inline int MultiSwitchArg::getValue() { return _value; }
00144 
00145 inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string>& args)
00146 {
00147         if ( _ignoreable && Arg::ignoreRest() )
00148                 return false;
00149 
00150         if ( argMatches( args[*i] ))
00151         {
00152                 // so the isSet() method will work
00153                 _alreadySet = true;
00154 
00155                 // Matched argument: increment value.
00156                 ++_value;
00157 
00158                 _checkWithVisitor();
00159 
00160                 return true;
00161         }
00162         else if ( combinedSwitchesMatch( args[*i] ) )
00163         {
00164                 // so the isSet() method will work
00165                 _alreadySet = true;
00166 
00167                 // Matched argument: increment value.
00168                 ++_value;
00169 
00170                 // Check for more in argument and increment value.
00171                 while ( combinedSwitchesMatch( args[*i] ) ) 
00172                         ++_value;
00173 
00174                 _checkWithVisitor();
00175 
00176                 return false;
00177         }
00178         else
00179                 return false;
00180 }
00181 
00182 std::string MultiSwitchArg::shortID(const std::string& val) const
00183 {
00184         std::string id = Arg::shortID() + " ... ";
00185 
00186         return id;
00187 }
00188 
00189 std::string MultiSwitchArg::longID(const std::string& val) const
00190 {
00191         std::string id = Arg::longID() + "  (accepted multiple times)";
00192 
00193         return id;
00194 }
00195 
00196 //////////////////////////////////////////////////////////////////////
00197 //END MultiSwitchArg.cpp
00198 //////////////////////////////////////////////////////////////////////
00199 
00200 } //namespace TCLAP
00201 
00202 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011