Main MRPT website > C++ reference
MRPT logo

UnlabeledValueArg.h

Go to the documentation of this file.
00001 
00002 /****************************************************************************** 
00003  * 
00004  *  file:  UnlabeledValueArg.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_UNLABELED_VALUE_ARGUMENT_H
00025 #define TCLAP_UNLABELED_VALUE_ARGUMENT_H
00026 
00027 #include <string>
00028 #include <vector>
00029 
00030 #include <mrpt/otherlibs/tclap/ValueArg.h>
00031 #include <mrpt/otherlibs/tclap/OptionalUnlabeledTracker.h>
00032 
00033 
00034 namespace TCLAP {
00035 
00036 /**
00037  * The basic unlabeled argument that parses a value.
00038  * This is a template class, which means the type T defines the type
00039  * that a given object will attempt to parse when an UnlabeledValueArg
00040  * is reached in the list of args that the CmdLine iterates over.
00041  */
00042 template<class T>
00043 class UnlabeledValueArg : public ValueArg<T>
00044 {
00045 
00046         // If compiler has two stage name lookup (as gcc >= 3.4 does)
00047         // this is requried to prevent undef. symbols
00048         using ValueArg<T>::_ignoreable;
00049         using ValueArg<T>::_hasBlanks;
00050         using ValueArg<T>::_extractValue;
00051         using ValueArg<T>::_typeDesc;
00052         using ValueArg<T>::_name;
00053         using ValueArg<T>::_description;
00054         using ValueArg<T>::_alreadySet;
00055         using ValueArg<T>::toString;
00056 
00057         public:
00058 
00059                 /**
00060                  * UnlabeledValueArg constructor.
00061                  * \param name - A one word name for the argument.  Can be
00062                  * used as a long flag on the command line.
00063                  * \param desc - A description of what the argument is for or
00064                  * does.
00065                  * \param req - Whether the argument is required on the command
00066                  * line.
00067                  * \param value - The default value assigned to this argument if it
00068                  * is not present on the command line.
00069                  * \param typeDesc - A short, human readable description of the
00070                  * type that this object expects.  This is used in the generation
00071                  * of the USAGE statement.  The goal is to be helpful to the end user
00072                  * of the program.
00073                  * \param ignoreable - Allows you to specify that this argument can be
00074                  * ignored if the '--' flag is set.  This defaults to false (cannot
00075                  * be ignored) and should  generally stay that way unless you have 
00076                  * some special need for certain arguments to be ignored.
00077                  * \param v - Optional Vistor.  You should leave this blank unless
00078                  * you have a very good reason.
00079                  */
00080                 UnlabeledValueArg( const std::string& name, 
00081                                        const std::string& desc, 
00082                                                    bool req,
00083                                            T value,
00084                                            const std::string& typeDesc,
00085                                                    bool ignoreable = false,
00086                                            Visitor* v = NULL); 
00087 
00088                 /**
00089                  * UnlabeledValueArg constructor.
00090                  * \param name - A one word name for the argument.  Can be
00091                  * used as a long flag on the command line.
00092                  * \param desc - A description of what the argument is for or
00093                  * does.
00094                  * \param req - Whether the argument is required on the command
00095                  * line.
00096                  * \param value - The default value assigned to this argument if it
00097                  * is not present on the command line.
00098                  * \param typeDesc - A short, human readable description of the
00099                  * type that this object expects.  This is used in the generation
00100                  * of the USAGE statement.  The goal is to be helpful to the end user
00101                  * of the program.
00102                  * \param parser - A CmdLine parser object to add this Arg to
00103                  * \param ignoreable - Allows you to specify that this argument can be
00104                  * ignored if the '--' flag is set.  This defaults to false (cannot
00105                  * be ignored) and should  generally stay that way unless you have 
00106                  * some special need for certain arguments to be ignored.
00107                  * \param v - Optional Vistor.  You should leave this blank unless
00108                  * you have a very good reason.
00109                  */
00110                 UnlabeledValueArg( const std::string& name, 
00111                                        const std::string& desc, 
00112                                                    bool req,
00113                                            T value,
00114                                            const std::string& typeDesc,
00115                                                    CmdLineInterface& parser,
00116                                                    bool ignoreable = false,
00117                                            Visitor* v = NULL );                                         
00118                                                 
00119                 /**
00120                  * UnlabeledValueArg constructor.
00121                  * \param name - A one word name for the argument.  Can be
00122                  * used as a long flag on the command line.
00123                  * \param desc - A description of what the argument is for or
00124                  * does.
00125                  * \param req - Whether the argument is required on the command
00126                  * line.
00127                  * \param value - The default value assigned to this argument if it
00128                  * is not present on the command line.
00129                  * \param constraint - A pointer to a Constraint object used
00130                  * to constrain this Arg.
00131                  * \param ignoreable - Allows you to specify that this argument can be
00132                  * ignored if the '--' flag is set.  This defaults to false (cannot
00133                  * be ignored) and should  generally stay that way unless you have 
00134                  * some special need for certain arguments to be ignored.
00135                  * \param v - Optional Vistor.  You should leave this blank unless
00136                  * you have a very good reason.
00137                  */
00138                 UnlabeledValueArg( const std::string& name, 
00139                                        const std::string& desc, 
00140                                                    bool req,
00141                                            T value,
00142                                            Constraint<T>* constraint,
00143                                                    bool ignoreable = false,
00144                                            Visitor* v = NULL ); 
00145 
00146                 
00147                 /**
00148                  * UnlabeledValueArg constructor.
00149                  * \param name - A one word name for the argument.  Can be
00150                  * used as a long flag on the command line.
00151                  * \param desc - A description of what the argument is for or
00152                  * does.
00153                  * \param req - Whether the argument is required on the command
00154                  * line.
00155                  * \param value - The default value assigned to this argument if it
00156                  * is not present on the command line.
00157                  * \param constraint - A pointer to a Constraint object used
00158                  * to constrain this Arg.
00159                  * \param parser - A CmdLine parser object to add this Arg to
00160                  * \param ignoreable - Allows you to specify that this argument can be
00161                  * ignored if the '--' flag is set.  This defaults to false (cannot
00162                  * be ignored) and should  generally stay that way unless you have 
00163                  * some special need for certain arguments to be ignored.
00164                  * \param v - Optional Vistor.  You should leave this blank unless
00165                  * you have a very good reason.
00166                  */
00167                 UnlabeledValueArg( const std::string& name, 
00168                                        const std::string& desc, 
00169                                                    bool req,
00170                                            T value,
00171                                            Constraint<T>* constraint,
00172                                                    CmdLineInterface& parser,
00173                                                    bool ignoreable = false,
00174                                            Visitor* v = NULL);
00175                                                 
00176                 /**
00177                  * Handles the processing of the argument.
00178                  * This re-implements the Arg version of this method to set the
00179                  * _value of the argument appropriately.  Handling specific to
00180                  * unlabled arguments.
00181                  * \param i - Pointer the the current argument in the list.
00182                  * \param args - Mutable list of strings. 
00183                  */
00184                 virtual bool processArg(int* i, std::vector<std::string>& args); 
00185 
00186                 /**
00187                  * Overrides shortID for specific behavior.
00188                  */
00189                 virtual std::string shortID(const std::string& val="val") const;
00190 
00191                 /**
00192                  * Overrides longID for specific behavior.
00193                  */
00194                 virtual std::string longID(const std::string& val="val") const;
00195 
00196                 /**
00197                  * Overrides operator== for specific behavior.
00198                  */
00199                 virtual bool operator==(const Arg& a ) const;
00200 
00201                 /**
00202                  * Instead of pushing to the front of list, push to the back.
00203                  * \param argList - The list to add this to.
00204                  */
00205                 virtual void addToList( std::list<Arg*>& argList ) const;
00206 
00207 };
00208 
00209 /**
00210  * Constructor implemenation.
00211  */
00212 template<class T>
00213 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00214                                                             const std::string& desc, 
00215                                                                                 bool req,
00216                                                             T val,
00217                                                             const std::string& typeDesc,
00218                                                             bool ignoreable,
00219                                                             Visitor* v)
00220 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
00221 { 
00222         _ignoreable = ignoreable;
00223 
00224         OptionalUnlabeledTracker::check(req, toString());
00225 
00226 }
00227 
00228 template<class T>
00229 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00230                                                             const std::string& desc, 
00231                                                                                 bool req,
00232                                                             T val,
00233                                                             const std::string& typeDesc,
00234                                                             CmdLineInterface& parser,
00235                                                             bool ignoreable,
00236                                                             Visitor* v)
00237 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
00238 { 
00239         _ignoreable = ignoreable;
00240         OptionalUnlabeledTracker::check(req, toString());
00241         parser.add( this );
00242 }
00243 
00244 /**
00245  * Constructor implemenation.
00246  */
00247 template<class T>
00248 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00249                                         const std::string& desc, 
00250                                                                                 bool req,
00251                                         T val,
00252                                         Constraint<T>* constraint,
00253                                         bool ignoreable,
00254                                         Visitor* v)
00255 : ValueArg<T>("", name, desc, req, val, constraint, v)
00256 { 
00257         _ignoreable = ignoreable;
00258         OptionalUnlabeledTracker::check(req, toString());
00259 }
00260 
00261 template<class T>
00262 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00263                                                             const std::string& desc, 
00264                                                                                 bool req,
00265                                                             T val,
00266                                                             Constraint<T>* constraint,
00267                                                             CmdLineInterface& parser,
00268                                                             bool ignoreable,
00269                                                             Visitor* v)
00270 : ValueArg<T>("", name, desc, req, val, constraint,  v)
00271 { 
00272         _ignoreable = ignoreable;
00273         OptionalUnlabeledTracker::check(req, toString());
00274         parser.add( this );
00275 }
00276 
00277 /**
00278  * Implementation of processArg().
00279  */
00280 template<class T>
00281 bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string>& args) 
00282 {
00283         
00284         if ( _alreadySet )
00285                 return false;
00286         
00287         if ( _hasBlanks( args[*i] ) )
00288                 return false;
00289 
00290         // never ignore an unlabeled arg
00291         
00292         _extractValue( args[*i] );
00293         _alreadySet = true;
00294         return true;
00295 }
00296 
00297 /**
00298  * Overriding shortID for specific output.
00299  */
00300 template<class T>
00301 std::string UnlabeledValueArg<T>::shortID(const std::string& val) const
00302 {
00303         std::string id = "<" + _typeDesc + ">";
00304 
00305         return id;
00306 }
00307 
00308 /**
00309  * Overriding longID for specific output.
00310  */
00311 template<class T>
00312 std::string UnlabeledValueArg<T>::longID(const std::string& val) const
00313 {
00314         // Ideally we would like to be able to use RTTI to return the name
00315         // of the type required for this argument.  However, g++ at least, 
00316         // doesn't appear to return terribly useful "names" of the types.  
00317         std::string id = "<" + _typeDesc + ">";
00318 
00319         return id;      
00320 }
00321 
00322 /**
00323  * Overriding operator== for specific behavior.
00324  */
00325 template<class T>
00326 bool UnlabeledValueArg<T>::operator==(const Arg& a ) const
00327 {
00328         if ( _name == a.getName() || _description == a.getDescription() )
00329                 return true;
00330         else
00331                 return false;
00332 }
00333 
00334 template<class T>
00335 void UnlabeledValueArg<T>::addToList( std::list<Arg*>& argList ) const
00336 {
00337         argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
00338 }
00339 
00340 }
00341 #endif



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