BASIS  version 1.2.3 (revision 2104)
MultiSwitchArg.h
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 <sbia/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          * Used to support the reset() method so that ValueArg can be
00050          * reset to their constructed value.
00051          */
00052         int _default;
00053 
00054     public:
00055 
00056         /**
00057          * MultiSwitchArg constructor.
00058          * \param flag - The one character flag that identifies this
00059          * argument on the command line.
00060          * \param name - A one word name for the argument.  Can be
00061          * used as a long flag on the command line.
00062          * \param desc - A description of what the argument is for or
00063          * does.
00064          * \param init - Optional. The initial/default value of this Arg. 
00065          * Defaults to 0.
00066          * \param v - An optional visitor.  You probably should not
00067          * use this unless you have a very good reason.
00068          */
00069         MultiSwitchArg(const std::string& flag, 
00070                 const std::string& name,
00071                 const std::string& desc,
00072                 int init = 0,
00073                 Visitor* v = NULL);
00074 
00075 
00076         /**
00077          * MultiSwitchArg constructor.
00078          * \param flag - The one character flag that identifies this
00079          * argument on the command line.
00080          * \param name - A one word name for the argument.  Can be
00081          * used as a long flag on the command line.
00082          * \param desc - A description of what the argument is for or
00083          * does.
00084          * \param parser - A CmdLine parser object to add this Arg to
00085          * \param init - Optional. The initial/default value of this Arg. 
00086          * Defaults to 0.
00087          * \param v - An optional visitor.  You probably should not
00088          * use this unless you have a very good reason.
00089          */
00090         MultiSwitchArg(const std::string& flag, 
00091                 const std::string& name,
00092                 const std::string& desc,
00093                 CmdLineInterface& parser,
00094                 int init = 0,
00095                 Visitor* v = NULL);
00096 
00097 
00098         /**
00099          * Handles the processing of the argument.
00100          * This re-implements the SwitchArg version of this method to set the
00101          * _value of the argument appropriately.
00102          * \param i - Pointer the the current argument in the list.
00103          * \param args - Mutable list of strings. Passed
00104          * in from main().
00105          */
00106         virtual bool processArg(int* i, std::vector<std::string>& args); 
00107 
00108         /**
00109          * Returns int, the number of times the switch has been set.
00110          */
00111         int getValue();
00112 
00113         /**
00114          * Returns the shortID for this Arg.
00115          */
00116         std::string shortID(const std::string& val) const;
00117 
00118         /**
00119          * Returns the longID for this Arg.
00120          */
00121         std::string longID(const std::string& val) const;
00122         
00123         void reset();
00124 
00125 };
00126 
00127 //////////////////////////////////////////////////////////////////////
00128 //BEGIN MultiSwitchArg.cpp
00129 //////////////////////////////////////////////////////////////////////
00130 inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
00131                     const std::string& name,
00132                     const std::string& desc,
00133                     int init,
00134                     Visitor* v )
00135 : SwitchArg(flag, name, desc, false, v),
00136 _value( init ),
00137 _default( init )
00138 { }
00139 
00140 inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
00141                     const std::string& name, 
00142                     const std::string& desc, 
00143                     CmdLineInterface& parser,
00144                     int init,
00145                     Visitor* v )
00146 : SwitchArg(flag, name, desc, false, v),
00147 _value( init ),
00148 _default( init )
00149 { 
00150     parser.add( this );
00151 }
00152 
00153 inline int MultiSwitchArg::getValue() { return _value; }
00154 
00155 inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string>& args)
00156 {
00157     if ( _ignoreable && Arg::ignoreRest() )
00158         return false;
00159 
00160     if ( argMatches( args[*i] ))
00161     {
00162         // so the isSet() method will work
00163         _alreadySet = true;
00164 
00165         // Matched argument: increment value.
00166         ++_value;
00167 
00168         _checkWithVisitor();
00169 
00170         return true;
00171     }
00172     else if ( combinedSwitchesMatch( args[*i] ) )
00173     {
00174         // so the isSet() method will work
00175         _alreadySet = true;
00176 
00177         // Matched argument: increment value.
00178         ++_value;
00179 
00180         // Check for more in argument and increment value.
00181         while ( combinedSwitchesMatch( args[*i] ) ) 
00182             ++_value;
00183 
00184         _checkWithVisitor();
00185 
00186         return false;
00187     }
00188     else
00189         return false;
00190 }
00191 
00192 inline std::string 
00193 MultiSwitchArg::shortID(const std::string& val) const
00194 {
00195     return Arg::shortID(val) + " ... ";
00196 }
00197 
00198 inline std::string 
00199 MultiSwitchArg::longID(const std::string& val) const
00200 {
00201     return Arg::longID(val) + "  (accepted multiple times)";
00202 }
00203 
00204 inline void
00205 MultiSwitchArg::reset()
00206 {
00207     MultiSwitchArg::_value = MultiSwitchArg::_default;
00208 }
00209 
00210 //////////////////////////////////////////////////////////////////////
00211 //END MultiSwitchArg.cpp
00212 //////////////////////////////////////////////////////////////////////
00213 
00214 } //namespace TCLAP
00215 
00216 #endif