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