BASIS  version 1.2.3 (revision 2104)
UnlabeledMultiArg.h
00001 
00002 /****************************************************************************** 
00003  * 
00004  *  file:  UnlabeledMultiArg.h
00005  * 
00006  *  Copyright (c) 2003, Michael E. Smoot.
00007  *  All rights reverved.
00008  * 
00009  *  See the file COPYING in the top directory of this distribution for
00010  *  more information.
00011  *  
00012  *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 
00013  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
00014  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
00015  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
00016  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
00017  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
00018  *  DEALINGS IN THE SOFTWARE.  
00019  *  
00020  *****************************************************************************/ 
00021 
00022 
00023 #ifndef TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H
00024 #define TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H
00025 
00026 #include <string>
00027 #include <vector>
00028 
00029 #include <sbia/tclap/MultiArg.h>
00030 #include <sbia/tclap/OptionalUnlabeledTracker.h>
00031 
00032 namespace TCLAP {
00033 
00034 /**
00035  * Just like a MultiArg, except that the arguments are unlabeled.  Basically,
00036  * this Arg will slurp up everything that hasn't been matched to another 
00037  * Arg.
00038  */
00039 template<class T>
00040 class UnlabeledMultiArg : public MultiArg<T>
00041 {
00042 
00043     // If compiler has two stage name lookup (as gcc >= 3.4 does)
00044     // this is requried to prevent undef. symbols
00045     using MultiArg<T>::_ignoreable;
00046     using MultiArg<T>::_hasBlanks;
00047     using MultiArg<T>::_extractValue;
00048     using MultiArg<T>::_typeDesc;
00049     using MultiArg<T>::_name;
00050     using MultiArg<T>::_description;
00051     using MultiArg<T>::_alreadySet;
00052     using MultiArg<T>::toString;
00053 
00054     public:
00055         
00056         /**
00057          * Constructor.  
00058          * \param name - The name of the Arg. Note that this is used for
00059          * identification, not as a long flag.
00060          * \param desc - A description of what the argument is for or
00061          * does.
00062          * \param req - Whether the argument is required on the command
00063          *  line.
00064          * \param typeDesc - A short, human readable description of the
00065          * type that this object expects.  This is used in the generation
00066          * of the USAGE statement.  The goal is to be helpful to the end user
00067          * of the program.
00068          * \param ignoreable - Whether or not this argument can be ignored
00069          * using the "--" flag.
00070          * \param v - An optional visitor.  You probably should not
00071          * use this unless you have a very good reason.
00072          */
00073         UnlabeledMultiArg( const std::string& name,
00074                            const std::string& desc,
00075                            bool req,
00076                            const std::string& typeDesc,
00077                            bool ignoreable = false,
00078                            Visitor* v = NULL );
00079         /**
00080          * Constructor.  
00081          * \param name - The name of the Arg. Note that this is used for
00082          * identification, not as a long flag.
00083          * \param desc - A description of what the argument is for or
00084          * does.
00085          * \param req - Whether the argument is required on the command
00086          *  line.
00087          * \param typeDesc - A short, human readable description of the
00088          * type that this object expects.  This is used in the generation
00089          * of the USAGE statement.  The goal is to be helpful to the end user
00090          * of the program.
00091          * \param parser - A CmdLine parser object to add this Arg to
00092          * \param ignoreable - Whether or not this argument can be ignored
00093          * using the "--" flag.
00094          * \param v - An optional visitor.  You probably should not
00095          * use this unless you have a very good reason.
00096          */
00097         UnlabeledMultiArg( const std::string& name,
00098                            const std::string& desc,
00099                            bool req,
00100                            const std::string& typeDesc,
00101                            CmdLineInterface& parser,
00102                            bool ignoreable = false,
00103                            Visitor* v = NULL );
00104                          
00105         /**
00106          * Constructor.  
00107          * \param name - The name of the Arg. Note that this is used for
00108          * identification, not as a long flag.
00109          * \param desc - A description of what the argument is for or
00110          * does.
00111          * \param req - Whether the argument is required on the command
00112          *  line.
00113          * \param constraint - A pointer to a Constraint object used
00114          * to constrain this Arg.
00115          * \param ignoreable - Whether or not this argument can be ignored
00116          * using the "--" flag.
00117          * \param v - An optional visitor.  You probably should not
00118          * use this unless you have a very good reason.
00119          */
00120         UnlabeledMultiArg( const std::string& name,
00121                            const std::string& desc,
00122                            bool req,
00123                            Constraint<T>* constraint,
00124                            bool ignoreable = false,
00125                            Visitor* v = NULL );
00126 
00127         /**
00128          * Constructor.  
00129          * \param name - The name of the Arg. Note that this is used for
00130          * identification, not as a long flag.
00131          * \param desc - A description of what the argument is for or
00132          * does.
00133          * \param req - Whether the argument is required on the command
00134          *  line.
00135          * \param constraint - A pointer to a Constraint object used
00136          * to constrain this Arg.
00137          * \param parser - A CmdLine parser object to add this Arg to
00138          * \param ignoreable - Whether or not this argument can be ignored
00139          * using the "--" flag.
00140          * \param v - An optional visitor.  You probably should not
00141          * use this unless you have a very good reason.
00142          */
00143         UnlabeledMultiArg( const std::string& name, 
00144                            const std::string& desc, 
00145                            bool req,
00146                            Constraint<T>* constraint,
00147                            CmdLineInterface& parser,
00148                            bool ignoreable = false,
00149                            Visitor* v = NULL );
00150                          
00151         /**
00152          * Handles the processing of the argument.
00153          * This re-implements the Arg version of this method to set the
00154          * _value of the argument appropriately.  It knows the difference
00155          * between labeled and unlabeled.
00156          * \param i - Pointer the the current argument in the list.
00157          * \param args - Mutable list of strings. Passed from main().
00158          */
00159         virtual bool processArg(int* i, std::vector<std::string>& args); 
00160 
00161         /**
00162          * Returns the a short id string.  Used in the usage.
00163          * \param val - value to be used.
00164          */
00165         virtual std::string shortID(const std::string& val="val") const;
00166 
00167         /**
00168          * Returns the a long id string.  Used in the usage.
00169          * \param val - value to be used.
00170          */
00171         virtual std::string longID(const std::string& val="val") const;
00172 
00173         /**
00174          * Opertor ==.
00175          * \param a - The Arg to be compared to this.
00176          */
00177         virtual bool operator==(const Arg& a) const;
00178 
00179         /**
00180          * Pushes this to back of list rather than front.
00181          * \param argList - The list this should be added to.
00182          */
00183         virtual void addToList( std::list<Arg*>& argList ) const;
00184 };
00185 
00186 template<class T>
00187 UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name, 
00188                                         const std::string& desc, 
00189                                         bool req,
00190                                         const std::string& typeDesc,
00191                                         bool ignoreable,
00192                                         Visitor* v)
00193 : MultiArg<T>("", name, desc,  req, typeDesc, v)
00194 { 
00195     _ignoreable = ignoreable;
00196     OptionalUnlabeledTracker::check(true, toString());
00197 }
00198 
00199 template<class T>
00200 UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name, 
00201                                         const std::string& desc, 
00202                                         bool req,
00203                                         const std::string& typeDesc,
00204                                         CmdLineInterface& parser,
00205                                         bool ignoreable,
00206                                         Visitor* v)
00207 : MultiArg<T>("", name, desc,  req, typeDesc, v)
00208 { 
00209     _ignoreable = ignoreable;
00210     OptionalUnlabeledTracker::check(true, toString());
00211     parser.add( this );
00212 }
00213 
00214 
00215 template<class T>
00216 UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name, 
00217                                         const std::string& desc, 
00218                                         bool req,
00219                                         Constraint<T>* constraint,
00220                                         bool ignoreable,
00221                                         Visitor* v)
00222 : MultiArg<T>("", name, desc,  req, constraint, v)
00223 { 
00224     _ignoreable = ignoreable;
00225     OptionalUnlabeledTracker::check(true, toString());
00226 }
00227 
00228 template<class T>
00229 UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name, 
00230                                         const std::string& desc, 
00231                                         bool req,
00232                                         Constraint<T>* constraint,
00233                                         CmdLineInterface& parser,
00234                                         bool ignoreable,
00235                                         Visitor* v)
00236 : MultiArg<T>("", name, desc,  req, constraint, v)
00237 { 
00238     _ignoreable = ignoreable;
00239     OptionalUnlabeledTracker::check(true, toString());
00240     parser.add( this );
00241 }
00242 
00243 
00244 template<class T>
00245 bool UnlabeledMultiArg<T>::processArg(int *i, std::vector<std::string>& args) 
00246 {
00247 
00248     if ( _hasBlanks( args[*i] ) )
00249         return false;
00250 
00251     // never ignore an unlabeled multi arg
00252 
00253 
00254     // always take the first value, regardless of the start string 
00255     _extractValue( args[(*i)] );
00256 
00257     /*
00258     // continue taking args until we hit the end or a start string 
00259     while ( (unsigned int)(*i)+1 < args.size() &&
00260             args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
00261             args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 ) 
00262         _extractValue( args[++(*i)] );
00263     */
00264 
00265     _alreadySet = true;
00266 
00267     return true;
00268 }
00269 
00270 template<class T>
00271 std::string UnlabeledMultiArg<T>::shortID(const std::string& val) const
00272 {
00273     static_cast<void>(val); // Ignore input, don't warn
00274     return std::string("<") + _typeDesc + "> ...";
00275 }
00276 
00277 template<class T>
00278 std::string UnlabeledMultiArg<T>::longID(const std::string& val) const
00279 {
00280     static_cast<void>(val); // Ignore input, don't warn
00281     return std::string("<") + _typeDesc + ">  (accepted multiple times)";
00282 }
00283 
00284 template<class T>
00285 bool UnlabeledMultiArg<T>::operator==(const Arg& a) const
00286 {
00287     if ( _name == a.getName() || _description == a.getDescription() )
00288         return true;
00289     else
00290         return false;
00291 }
00292 
00293 template<class T>
00294 void UnlabeledMultiArg<T>::addToList( std::list<Arg*>& argList ) const
00295 {
00296     argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
00297 }
00298 
00299 }
00300 
00301 #endif