src/Utils/Args.cpp

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 
00021 #include "Args.h"
00022 
00023 namespace nor_utils {
00024 
00025 
00026 void Args::declareArgument( const string& name )
00027 {
00028    _argsList[name] = 0;
00029 }
00030 
00031 // -----------------------------------------------------------------------
00032 
00033 void Args::declareArgument( const string& name, const string& description, const int numOptions,
00034                         const string& options )
00035 {
00036    // Add to standard argument list
00037    _argsList[name] = numOptions;
00038 
00039    // not very efficient but speed is not an issue here
00040    _groupedList[_currentGroup].push_back(Argument(name, description, numOptions, options));
00041 }
00042 
00043 // -----------------------------------------------------------------------
00044 
00045 void Args::printGroup(const string& groupName, ostream& out, const int indSpaces)
00046 {
00047    out << "\n" << groupName << ":" << endl;
00048 
00049    vector<Argument>& args = _groupedList[groupName];
00050 
00051    vector<Argument>::iterator it;
00052    for (it = args.begin(); it != args.end(); ++it)
00053    {
00054       for (int i = 0; i < indSpaces; ++i)
00055          out << ' ';
00056 
00057       out << it->name;
00058       
00059       if (!it->options.empty())
00060          out << " " << it->options;
00061       
00062       out << ":" << endl;
00063       out << getWrappedString(it->description, indSpaces+3) << endl;
00064 
00065    }
00066 
00067 }
00068 
00069 // -----------------------------------------------------------------------
00070 
00071 ArgsOutType Args::readArguments( const int argc, char* argv[] )
00072 {
00073   if (argc < 2 )
00074     return AOT_NO_ARGUMENTS;
00075 
00076   for (int i = 1; i < argc; ++i)
00077   {
00078     string aStr = argv[i];
00079 
00080     if ( _argsList.find(aStr) == _argsList.end() )
00081     {
00082       cerr << "ERROR: Unknown argument: " << aStr << endl;
00083       return AOT_UNKOWN_ARGUMENT;
00084     }
00085     else
00086     {
00087       // arg exists?
00088       const int numArgs = _argsList[aStr];
00089       
00090       if (numArgs == 0)
00091         _resArgs[aStr].push_back("");
00092       else
00093       {      
00094         if (argc < i + numArgs + 1)
00095         {
00096           cerr << "ERROR: The number of values for " << aStr << " is incorrect!" << endl;
00097           return AOT_TOO_FEW_VALUES; 
00098         }
00099       
00100         for (int j = 0; j < numArgs; ++j)
00101           _resArgs[aStr].push_back(argv[++i]);
00102       }
00103     }
00104     
00105   }
00106   
00107   return AOT_OK;
00108 }
00109 
00110 // -----------------------------------------------------------------------
00111 
00112 vector<string>&  Args::getValuesVector(const string& argument)
00113 { 
00114   if ( _resArgs.find(argument) == _resArgs.end() )
00115   {
00116     cout << "ERROR: Looking for an argument (" << argument << ") that do not exists!" << endl;
00117     exit(1);
00118   }
00119   
00120   return _resArgs[argument]; 
00121 }  
00122 
00123 // -----------------------------------------------------------------------
00124 
00125 string Args::getWrappedString(const string& str, const int leftSpace, const bool spacesInFirstLine) const
00126 {
00127    string result;
00128    insert_iterator<string> iIt(result, result.begin());
00129    string::const_iterator sIt;
00130 
00131    int col = leftSpace;
00132 
00133    if (spacesInFirstLine)
00134    {
00135       for (int i = 0; i < leftSpace; ++i)
00136          *iIt++ = ' ';
00137    }
00138 
00139    for (sIt = str.begin(); sIt != str.end(); ++sIt)
00140    {
00141       if ( (*sIt == ' ' && col > _maxColumns) || *sIt == '\n' )
00142       {
00143          col = leftSpace;
00144          *iIt++ = '\n';
00145          for (int i = 0; i < leftSpace; ++i)
00146             *iIt++ = ' ';
00147       }
00148       else
00149       {
00150          ++col;
00151          *iIt++ = *sIt;
00152       }
00153    }
00154 
00155    return result;
00156 }
00157 
00158 // -----------------------------------------------------------------------
00159 
00160 } // end of namespace nor_utils

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