/*************************************************************************** * Copyright (C) 2005 by Mircea Bardac * * dev@mircea.bardac.net * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "argumentparser.h" /** @author Mircea Bardac @version 1.0 */ /** * Class Constructor (takes main() function arguments and initializes * the parser engine * \param c number of arguments * \param v pointer to the char* arguments */ ArgumentParser::ArgumentParser (int c, char **v): argc(c), argv(v), stopOnUnknownArg(false) {}; /** * Class Destructor */ ArgumentParser::~ArgumentParser() { }; /** * Function to convert an abbreviation to its true name * \param abbrev abbreviation * \return the corresponding real name of the parameter */ QString ArgumentParser::convertAbbrev(const QString abbrev) { QMap::Iterator a=abbrevs.find(abbrev); if (a!=abbrevs.end()) { return a.data(); } else { if (stopOnUnknownArg) gotError=100; // just an internal signal // (will be modified to point the correct argument index) return ""; } } /** * Function to test if an argument takes a value or not * \param argname the argument tested * \return TRUE if argument takes a value or FALSE otherwise */ bool ArgumentParser::takesValue(const QString argname) { QMap::Iterator hv=argData.find(argname); if (hv==argData.end()) return false; return (hv.data().takesValue); } /** * Function to test if an argument is known or not * \param argname the argument tested * \return TRUE if argument is known or FALSE otherwise */ bool ArgumentParser::knownArg(const QString argname) { return (argData.contains(argname)); } /** * Function to parse the arguments * (the 'brain' of the class) */ void ArgumentParser::parse() { // printf("Number of arguments: %d\n",argc-1); gotError=0; for(int i=1;i0) { gotError=i; return;} if (takesValue(argname)) // "value" might be missing { i++; if (i==argc) { gotError=i-1; return; } argvalue=QString(argv[i]); } arguments.insert(argname,argvalue); continue; } // printf("Ignored argument: %s\n",argcurrent.latin1()); } } /* void ArgumentParser::showArgs() { if (gotError>0) printf("!!! Error at argument %d!\n",gotError); QMap::Iterator it; for (it=arguments.begin(); it!=arguments.end(); ++it) { printf("ARG: %s -- VALUE: %s\n",it.key().latin1(),it.data().latin1()); } } */ /** * Function to add an argument to the list of recognized arguments * \param argname full name of the argument * \param abbrev abbreviation of the argument (optional) * \param takesValue indicates wether the argument must have a value or not * \param helpdesc help description for the given argument */ void ArgumentParser::addArgument (const QString argname, const QString abbrev, const bool takesValue, const QString helpdesc) { ArgumentDetails d; d.takesValue=takesValue; d.helpdesc=helpdesc; argData.insert(argname,d); // printf(">abbrev: %s -> %s\n",abbrev.latin1(),argname.latin1()); if (abbrev!="") abbrevs.insert(abbrev,argname); } /** * Function to test if an agument was passed to the program * \param argname full name of the argument */ bool ArgumentParser::existsArg(const QString argname) { QMap::Iterator a=arguments.find(argname); return (a!=arguments.end()); } /** * Function to read the int value of a given argument * \param argname argument name * \return the argument int value */ int ArgumentParser::getInt(const QString argname) { QMap::Iterator a=arguments.find(argname); if (a!=arguments.end()) return a.data().toInt(); if (!knownArg(argname)) { printf("Warning: reading unknown argument \"%s\"!\n",argname.latin1()); return 0; } return 0; } /** * Function to read the string value of a given argument * \param argname argument name * \return the argument string value */ QString ArgumentParser::getString(QString argname) { QMap::Iterator a=arguments.find(argname); if (a!=arguments.end()) return a.data(); if (!knownArg(argname)) { printf("Warning: reading unknown argument \"%s\"!\n",argname.latin1()); return ""; } return ""; }