src/Utils/Args.h

Go to the documentation of this file.
00001 /*
00002 * Copyright (C) 2005 Norman Casagrande
00003 * For informations write to nova77@gmail.com
00004 *
00005 * This library is free software; you can redistribute it and/or
00006 * modify it under the terms of the GNU Lesser General Public
00007 * License as published by the Free Software Foundation; either
00008 * version 2.1 of the License, or (at your option) any later version.
00009 *
00010 * This library is distributed in the hope that it will be useful,
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 * Lesser General Public License for more details.
00014 *
00015 * You should have received a copy of the GNU Lesser General Public
00016 * License along with this library; if not, write to the Free Software
00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 *
00019 */
00020 
00025 #ifndef __ARGS_H
00026 #define __ARGS_H
00027 
00028 #include <map>
00029 #include <vector>
00030 #include <set>
00031 #include <string>
00032 #include <iostream>
00033 #include <sstream>
00034 
00035 using namespace std;
00036 
00039 
00040 namespace nor_utils {
00041 
00047 enum ArgsOutType
00048 {
00049    AOT_OK, 
00050    AOT_NO_ARGUMENTS, 
00051    AOT_UNKOWN_ARGUMENT, 
00052    AOT_TOO_FEW_VALUES 
00053 };
00054 
00081 class Args
00082 {
00083 public:
00084 
00089    Args() : _currentGroup("general"), _maxColumns(60) {}
00090 
00101    void setGroup( const string& groupName ) { _currentGroup = groupName; }
00102 
00109    void declareArgument( const string& name );
00110 
00127    void declareArgument( const string& name, const string& description, const int numOptions = 0,
00128                          const string& options = "");
00129 
00154    void printGroup( const string& groupName, ostream& out = cout, const int indSpaces = 3 );
00155 
00164    ArgsOutType   readArguments( const int argc, char* argv[] );
00165 
00174    bool             hasArgument(const string& argument) const
00175                             { return _resArgs.find(argument) != _resArgs.end(); }
00176 
00189    vector<string>&  getValuesVector(const string& argument);
00190 
00208    template <typename T>
00209    void getValue(const string& argument, int index, T& valueToFill)
00210    { 
00211       if ( _resArgs.find(argument) == _resArgs.end() )
00212       {
00213          cerr << "ERROR: Looking for an argument (" << argument << ") that does not exists!" << endl;
00214          exit(1);
00215       }
00216 
00217       stringstream ss(_resArgs[argument][index]);    
00218       ss >> valueToFill;
00219    }
00220 
00221 
00222 private:
00223 
00253    string getWrappedString(const string& str, const int leftSpace = 0, 
00254                            const bool spacesInFirstLine = true) const;
00255 
00260    struct Argument
00261    {
00266       Argument() {}
00267 
00278       Argument(const string& name, const string& description, 
00279                const int numOptions = 0, const string& options = "")
00280          : name(name), description(description), numOptions(numOptions), options(options) {}
00281 
00282       string name; 
00283       string description; 
00284       int numOptions; 
00285       string options; 
00286    };
00287 
00293    string  _currentGroup;
00294 
00300    int     _maxColumns;
00301 
00302    map< string, vector<Argument> >       _groupedList; 
00303 
00304    map< string, int >            _argsList; 
00305    map< string, vector<string> > _resArgs; 
00306 };
00307 
00308 }
00309 
00310 #endif // __ARGS_H

Generated on Mon Nov 28 21:43:46 2005 for MultiBoost by  doxygen 1.4.5