BASIS  version 1.2.3 (revision 2104)
UnlabeledValueArg.h
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 <sbia/tclap/ValueArg.h>
00031 #include <sbia/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.  Note that this is used for
00062          * identification, not as a long flag.
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.  Note that this is used for
00091          * identification, not as a long flag.
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.  Note that this is used for
00122          * identification, not as a long flag.
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.  Note that this is used for
00150          * identification, not as a long flag.
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 template<class T>
00210 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00211                                         const std::string& desc, 
00212                                         bool req,
00213                                         T val,
00214                                         const std::string& typeDesc,
00215                                         bool ignoreable,
00216                                         Visitor* v)
00217 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
00218 { 
00219     _ignoreable = ignoreable;
00220 
00221     OptionalUnlabeledTracker::check(req, toString());
00222 
00223 }
00224 
00225 template<class T>
00226 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00227                                         const std::string& desc, 
00228                                         bool req,
00229                                         T val,
00230                                         const std::string& typeDesc,
00231                                         CmdLineInterface& parser,
00232                                         bool ignoreable,
00233                                         Visitor* v)
00234 : ValueArg<T>("", name, desc, req, val, typeDesc, v)
00235 { 
00236     _ignoreable = ignoreable;
00237     OptionalUnlabeledTracker::check(req, toString());
00238     parser.add( this );
00239 }
00240 
00241 template<class T>
00242 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00243                                         const std::string& desc, 
00244                                         bool req,
00245                                         T val,
00246                                         Constraint<T>* constraint,
00247                                         bool ignoreable,
00248                                         Visitor* v)
00249 : ValueArg<T>("", name, desc, req, val, constraint, v)
00250 { 
00251     _ignoreable = ignoreable;
00252     OptionalUnlabeledTracker::check(req, toString());
00253 }
00254 
00255 template<class T>
00256 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00257                                         const std::string& desc, 
00258                                         bool req,
00259                                         T val,
00260                                         Constraint<T>* constraint,
00261                                         CmdLineInterface& parser,
00262                                         bool ignoreable,
00263                                         Visitor* v)
00264 : ValueArg<T>("", name, desc, req, val, constraint,  v)
00265 { 
00266     _ignoreable = ignoreable;
00267     OptionalUnlabeledTracker::check(req, toString());
00268     parser.add( this );
00269 }
00270 
00271 template<class T>
00272 bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string>& args) 
00273 {
00274     
00275     if ( _alreadySet )
00276         return false;
00277     
00278     if ( _hasBlanks( args[*i] ) )
00279         return false;
00280 
00281     // never ignore an unlabeled arg
00282     
00283     _extractValue( args[*i] );
00284     _alreadySet = true;
00285     return true;
00286 }
00287 
00288 template<class T>
00289 std::string UnlabeledValueArg<T>::shortID(const std::string& val) const
00290 {
00291     static_cast<void>(val); // Ignore input, don't warn
00292     return std::string("<") + _typeDesc + ">";
00293 }
00294 
00295 template<class T>
00296 std::string UnlabeledValueArg<T>::longID(const std::string& val) const
00297 {
00298     static_cast<void>(val); // Ignore input, don't warn
00299 
00300     // Ideally we would like to be able to use RTTI to return the name
00301     // of the type required for this argument.  However, g++ at least, 
00302     // doesn't appear to return terribly useful "names" of the types.  
00303     return std::string("<") + _typeDesc + ">";
00304 }
00305 
00306 template<class T>
00307 bool UnlabeledValueArg<T>::operator==(const Arg& a ) const
00308 {
00309     if ( _name == a.getName() || _description == a.getDescription() )
00310         return true;
00311     else
00312         return false;
00313 }
00314 
00315 template<class T>
00316 void UnlabeledValueArg<T>::addToList( std::list<Arg*>& argList ) const
00317 {
00318     argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
00319 }
00320 
00321 }
00322 #endif