Initial commit of cliparser.
authorSebastian Goth <seezer@roath.org>
Sun, 13 Jul 2008 21:22:04 +0000 (23:22 +0200)
committerSebastian Goth <seezer@roath.org>
Wed, 16 Jul 2008 22:57:29 +0000 (00:57 +0200)
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()".

src/common/CMakeLists.txt
src/common/cliparser.cpp [new file with mode: 0644]
src/common/cliparser.h [new file with mode: 0644]

index 880ccc9..765d7ba 100644 (file)
@@ -20,7 +20,8 @@ set(SOURCES
     util.cpp
     network.cpp
     ircuser.cpp
     util.cpp
     network.cpp
     ircuser.cpp
-    ircchannel.cpp)
+    ircchannel.cpp
+    cliparser.cpp)
 
 set(MOC_HDRS
     backlogmanager.h
 
 set(MOC_HDRS
     backlogmanager.h
@@ -43,7 +44,8 @@ set(HEADERS ${MOC_HDRS}
     message.h
     settings.h
     types.h
     message.h
     settings.h
     types.h
-    util.h)
+    util.h
+    cliparser.h)
 
 qt4_wrap_cpp(MOC ${MOC_HDRS})
 
 
 qt4_wrap_cpp(MOC ${MOC_HDRS})
 
diff --git a/src/common/cliparser.cpp b/src/common/cliparser.cpp
new file mode 100644 (file)
index 0000000..ad02b96
--- /dev/null
@@ -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 <QDebug>
+
+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 (file)
index 0000000..6872f77
--- /dev/null
@@ -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 <QString>
+#include <QStringList>
+#include <QHash>
+#include <QVariant>
+
+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<QString, QVariant*> savedValues;
+  QHash<QString, CliParserArg> argsHash;
+  QHash<QString, QHash<QString, CliParserArg>::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