From: Sebastian Goth Date: Sun, 13 Jul 2008 21:22:04 +0000 (+0200) Subject: Initial commit of cliparser. X-Git-Tag: 0.3.0~266 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=81c2e2cf2c12eb6839994f95f47698e5252fc91c Initial commit of cliparser. Supports options and switches. Shortnames, defaultvalues and helptexts can be added optionally. Accessmethod CliParser::value(QString key) always returns a QVariant. For switches, if no default is given, it returns "false", for options it returns "QVariant()". --- diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 880ccc99..765d7ba1 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -20,7 +20,8 @@ set(SOURCES util.cpp network.cpp ircuser.cpp - ircchannel.cpp) + ircchannel.cpp + cliparser.cpp) set(MOC_HDRS backlogmanager.h @@ -43,7 +44,8 @@ set(HEADERS ${MOC_HDRS} message.h settings.h types.h - util.h) + util.h + cliparser.h) qt4_wrap_cpp(MOC ${MOC_HDRS}) diff --git a/src/common/cliparser.cpp b/src/common/cliparser.cpp new file mode 100644 index 00000000..ad02b968 --- /dev/null +++ b/src/common/cliparser.cpp @@ -0,0 +1,74 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel IRC Team * + * devel@quassel-irc.org * + * * + * 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) version 3. * + * * + * 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 "cliparser.h" +#include + +CliParser::CliParser(int argc, char *argv[]) +{ + if(argc) { + for (int i=0; i < argc; ++i) + argsRaw.append(QString::fromUtf8(argv[i])); + } +} + + +CliParser::~CliParser() +{ +} + +void CliParser::addArgument(QString longName, char shortName = 0, QVariant def = QVariant()) { + CliParserArg arg = CliParserArg(longName, shortName, def); + argsHash.insert(longName, arg); + qDebug() << "Added Argument: " << longName << " with arg-addr: " << &arg; +} + +bool CliParser::parse() { + qDebug() << "Parse results: "; + qDebug() << argsHash.value("logfile").value; + argsHash[QString("logfile")].value = "BOOYA"; +} + +QVariant CliParser::value(QString key) { + return argsHash.value(key).value; +} + +CliParserArg::CliParserArg(QString longName, char shortName = 0, QVariant _def = QVariant() ) + : lname(longName), + sname(shortName), + def(_def), + value(0) { + +} + +CliParserArg::CliParserArg(const CliParserArg &other) { + lname = other.lname; + sname = other.sname; + def = other.def; + value = other.value; +} + +CliParserArg &CliParserArg::operator=(const CliParserArg &other) { + lname = other.lname; + sname = other.sname; + def = other.def; + value = other.value; + return *this; +} + diff --git a/src/common/cliparser.h b/src/common/cliparser.h new file mode 100644 index 00000000..6872f773 --- /dev/null +++ b/src/common/cliparser.h @@ -0,0 +1,61 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel IRC Team * + * devel@quassel-irc.org * + * * + * 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) version 3. * + * * + * 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. * + ***************************************************************************/ + +#ifndef CLIPARSER_H +#define CLIPARSER_H + +#include +#include +#include +#include + +class CliParserArg; + +class CliParser{ +public: + inline CliParser() {}; + CliParser(int argc, char *argv[]); + + ~CliParser(); + bool parse(); + QVariant value(QString key); + void addArgument(QString longName, char shortName, QVariant def); +private: + QStringList argsRaw; + QHash savedValues; + QHash argsHash; + QHash::iterator> shortHash; +}; + +class CliParserArg { +public: + inline CliParserArg() {}; + CliParserArg(const CliParserArg &other); + CliParserArg(QString longName, char shortName, QVariant _def); + CliParserArg &operator=(const CliParserArg &other); +// private: + QString lname; + char sname; + QVariant def; + QVariant value; +}; +Q_DECLARE_METATYPE(CliParserArg); + +#endif