fixing request -> receive sync calls
[quassel.git] / src / common / cliparser.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef CLIPARSER_H
22 #define CLIPARSER_H
23
24 #include <QString>
25 #include <QStringList>
26 #include <QHash>
27
28 #ifdef HAVE_KDE
29 #  include <KCmdLineOptions>
30 #endif
31
32 class CliParser {
33 public:
34   CliParser();
35
36   bool init(const QStringList &arguments = QStringList());
37
38   QString value(const QString &longName);
39   bool isSet(const QString &longName);
40   inline void addSwitch(const QString &longName, const char shortName = 0, const QString &help = QString()) {
41     addArgument(longName, CliParserArg(CliParserArg::CliArgSwitch, shortName, help));
42   }
43   inline void addOption(const QString &longName, const char shortName = 0, const QString &help = QString(), const QString &def = QString()) {
44     addArgument(longName, CliParserArg(CliParserArg::CliArgOption, shortName, help, def));
45   }
46   void usage();
47
48 private:
49   struct CliParserArg {
50     enum CliArgType {
51       CliArgInvalid,
52       CliArgSwitch,
53       CliArgOption
54     };
55     CliParserArg(const CliArgType _type = CliArgInvalid, const char _shortName = 0, const QString _help = QString(), const QString _def = QString())
56     : type(_type),
57     shortName(_shortName),
58     help(_help),
59     def(_def),
60     value(QString()),
61     boolValue(false) {};
62
63     CliArgType type;
64     char shortName;
65     QString help;
66     QString def;
67     QString value;
68     bool boolValue;
69   };
70
71   void addArgument(const QString &longName, const CliParserArg &arg);
72
73 #ifndef HAVE_KDE
74   bool addLongArg(const CliParserArg::CliArgType type, const QString &name, const QString &value = QString());
75   bool addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString &value = QString());
76   QString escapedValue(const QString &value);
77   QString lnameOfShortArg(const char arg);
78
79   QStringList argsRaw;
80   QHash<QString, CliParserArg> argsHash;
81
82 #else
83   KCmdLineOptions _cmdLineOptions;
84 #endif
85 };
86
87 #endif