Fix an issue with the event loop
[quassel.git] / src / common / cliparser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
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 #include "cliparser.h"
21
22 #include <QDir>
23 #include <QDebug>
24 #include <QString>
25 #include <QFileInfo>
26
27 #include <iostream>
28
29 CliParser::CliParser() : AbstractCliParser() {
30
31 }
32
33 void CliParser::addArgument(const QString &longName_, const CliParserArg &arg) {
34   QString longName = longName_;
35   longName.remove(QRegExp("\\s*<.*>\\s*")); // KCmdLineArgs takes args of the form "arg <defval>"
36   if(argsHash.contains(longName)) qWarning() << "Warning: Multiple definition of argument" << longName;
37   if(arg.shortName != 0 && !lnameOfShortArg(arg.shortName).isNull())
38     qWarning().nospace() << "Warning: Redefining shortName '" << arg.shortName << "' for " << longName << " previously defined for " << lnameOfShortArg(arg.shortName);
39   argsHash.insert(longName, arg);
40 }
41
42 bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString &name, const QString &value) {
43   if(argsHash.contains(name)){
44     if(type == CliParserArg::CliArgOption && argsHash.value(name).type == type) {
45       argsHash[name].value = escapedValue(value);
46       return true;
47     }
48     else if (type == CliParserArg::CliArgSwitch && argsHash.value(name).type == type) {
49       argsHash[name].boolValue = true;
50       return true;
51     }
52   }
53   qWarning() << "Warning: Unrecognized argument:" << name;
54   return false;
55 }
56
57 bool CliParser::addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString &value) {
58   QString longName = lnameOfShortArg(shortName);
59   if(!longName.isNull()) {
60     if(type == CliParserArg::CliArgOption && argsHash.value(longName).type == type) {
61       argsHash[longName].value = escapedValue(value);
62       return true;
63     }
64     else if (type == CliParserArg::CliArgSwitch) {
65       if(argsHash.value(longName).type == type) {
66         argsHash[longName].boolValue = true;
67         return true;
68       }
69       // arg is an option but detected as a switch -> argument is missing
70       else {
71         qWarning().nospace() << "Warning: '" << shortName << "' is an option which needs an argument";
72         return false;
73       }
74     }
75   }
76   qWarning().nospace() << "Warning: Unrecognized argument: '" << shortName << "'";
77   return false;
78 }
79
80 QString CliParser::escapedValue(const QString &value) {
81   QString escapedValue = value;
82   if(escapedValue.startsWith("~"))
83     escapedValue.replace(0, 1, QDir::homePath());
84
85   return escapedValue;
86 }
87
88 bool CliParser::init(const QStringList &args) {
89   argsRaw = args;
90   QStringList::const_iterator currentArg;
91   for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
92     if(currentArg->startsWith("--")) {
93       // long
94       QString name;
95       if(currentArg->contains("=")) {
96         // option
97         QStringList tmp = currentArg->mid(2).split("=");
98         name = tmp.at(0);
99         QString value;
100         // this is needed to allow --option=""
101         if(tmp.at(1).isNull()) value = QString("");
102         else value = tmp.at(1);
103         if(!addLongArg(CliParserArg::CliArgOption, name, value)) return false;
104       }
105       else {
106         // switch
107         name = currentArg->mid(2);
108         if(!addLongArg(CliParserArg::CliArgSwitch, name)) return false;
109       }
110     }
111     else if(currentArg->startsWith("-")) {
112       // short
113       char name;
114       QStringList::const_iterator nextArg = currentArg+1;
115       // if next arg is a short/long option/switch the current arg is one too
116       if(nextArg == argsRaw.constEnd() || nextArg->startsWith("-")) {
117         // switch
118         for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) {
119           name = currentArg->mid(1).toAscii().at(i);
120           if(!addShortArg(CliParserArg::CliArgSwitch, name)) return false;
121         }
122       }
123       // if next arg is is no option/switch it's an argument to a shortoption
124       else {
125         // option
126         // short options are not freely mixable with other shortargs
127         if(currentArg->mid(1).toAscii().size() > 1) {
128           qWarning() << "Warning: Shortoptions may not be combined with other shortoptions or switches";
129           return false;
130         }
131         QString value;
132         bool skipNext = false;
133         if(nextArg != argsRaw.constEnd()) {
134           value = nextArg->toLocal8Bit();
135           skipNext = true;
136         }
137         else value = currentArg->toLocal8Bit();
138         name = currentArg->mid(1).toAscii().at(0);
139         // we took one argument as argument to an option so skip it next time
140         if(skipNext) currentArg++;
141         if(!addShortArg(CliParserArg::CliArgOption, name, value)) return false;
142       }
143     }
144     else {
145       // we don't support plain arguments without -/--
146       if(currentArg->toLatin1() != argsRaw.at(0)) return false;
147     }
148   }
149   return true;
150 }
151
152 void CliParser::usage() {
153   std::cout << "Usage: " << qPrintable(QFileInfo(argsRaw.at(0)).completeBaseName()) << " [arguments]" << std::endl;
154
155   // get size of longName field
156   QStringList keys = argsHash.keys();
157   uint lnameFieldSize = 0;
158   foreach (QString key, keys) {
159     uint size = 0;
160     if(argsHash.value(key).type == CliParserArg::CliArgOption)
161       size += key.size()*2;
162     else
163       size += key.size();
164     // this is for " --...=[....] "
165     size += 8;
166     if(size > lnameFieldSize) lnameFieldSize = size;
167   }
168
169   QHash<QString, CliParserArg>::const_iterator arg;
170   for(arg = argsHash.constBegin(); arg != argsHash.constEnd(); ++arg) {
171     QString output;
172     QString lnameField;
173
174     if(arg.value().shortName) {
175       output.append(" -").append(arg.value().shortName).append(",");
176     }
177     else output.append("    ");
178     lnameField.append(" --").append(arg.key());
179     if(arg.value().type == CliParserArg::CliArgOption) {
180       lnameField.append("=[").append(arg.key().toUpper()).append("]");
181     }
182     output.append(lnameField.leftJustified(lnameFieldSize));
183     if(!arg.value().help.isEmpty()) {
184       output.append(arg.value().help);
185     }
186     if(arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) {
187       output.append(". Default is: ").append(arg.value().def);
188     }
189     std::cout << qPrintable(output) << std::endl;
190   }
191 }
192
193 QString CliParser::value(const QString &longName) {
194   if(argsHash.contains(longName) && argsHash.value(longName).type == CliParserArg::CliArgOption) {
195     if(!argsHash.value(longName).value.isNull())
196       return argsHash.value(longName).value;
197     else
198       return argsHash.value(longName).def;
199   }
200   else {
201     qWarning() << "Warning: Requested value of not defined argument" << longName << "or argument is a switch";
202     return QString();
203   }
204 }
205
206 bool CliParser::isSet(const QString &longName) {
207   if(argsHash.contains(longName)) {
208     if(argsHash.value(longName).type == CliParserArg::CliArgOption) return !argsHash.value(longName).value.isNull();
209     else return argsHash.value(longName).boolValue;
210   }
211   else {
212     qWarning() << "Warning: Requested isSet of not defined argument" << longName;
213     return false;
214   }
215 }
216
217 QString CliParser::lnameOfShortArg(const char arg) {
218   QHash<QString, CliParserArg>::const_iterator cur;
219   for (cur = argsHash.constBegin(); cur != argsHash.constEnd(); ++cur) {
220     if(cur.value().shortName == arg) return cur.key();
221   }
222   return QString();
223 }