Even more warnings.
[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 <QString>
23 #include <QFileInfo>
24 #include <QDebug>
25
26 CliParser::CliParser(QStringList arguments)
27 {
28   argsRaw = arguments;
29 //   remove Qt internal debugging arguments 
30   argsRaw.removeOne("-sync");
31   argsRaw.removeOne("-nograb");
32   argsRaw.removeOne("-dograb");
33 }
34
35 void CliParser::addArgument(const QString &longName, const CliParserArg &arg) {
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 = 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 = 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 bool CliParser::parse() {
81   QStringList::const_iterator currentArg;
82   for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
83     if(currentArg->startsWith("--")) {
84       // long
85       QString name;
86       if(currentArg->contains("=")) {
87         // option
88         QStringList tmp = currentArg->mid(2).split("=");
89         name = tmp.at(0);
90         QString value;
91         // this is needed to allow --option=""
92         if(tmp.at(1).isNull()) value = QString("");
93         else value = tmp.at(1);
94         if(!addLongArg(CliParserArg::CliArgOption, name, value)) return false;
95       }
96       else {
97         // switch
98         name = currentArg->mid(2);
99         if(!addLongArg(CliParserArg::CliArgSwitch, name)) return false;
100       }
101     }
102     else if(currentArg->startsWith("-")) {
103       // short
104       char name;
105       QStringList::const_iterator nextArg = currentArg+1;
106       // if next arg is a short/long option/switch the current arg is one too
107       if(nextArg == argsRaw.constEnd() || nextArg->startsWith("-")) {
108         // switch
109         for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) {
110           name = currentArg->mid(1).toAscii().at(i);
111           if(!addShortArg(CliParserArg::CliArgSwitch, name)) return false;
112         }
113       }
114       // if next arg is is no option/switch it's an argument to a shortoption
115       else {
116         // option
117         // short options are not freely mixable with other shortargs
118         if(currentArg->mid(1).toAscii().size() > 1) {
119           qWarning() << "Warning: Shortoptions may not be combined with other shortoptions or switches";
120           return false;
121         }
122         QString value;
123         bool skipNext = false;
124         if(nextArg != argsRaw.constEnd()) {
125           value = nextArg->toLocal8Bit();
126           skipNext = true;
127         }
128         else value = currentArg->toLocal8Bit();
129         name = currentArg->mid(1).toAscii().at(0);
130         // we took one argument as argument to an option so skip it next time
131         if(skipNext) currentArg++;
132         if(!addShortArg(CliParserArg::CliArgOption, name, value)) return false;
133       }
134     }
135     else {
136       // we don't support plain arguments without -/--
137       if(currentArg->toLatin1() != argsRaw.at(0)) return false;
138     }
139   }
140   return true;
141 }
142
143 void CliParser::usage() {
144   qWarning() << "Usage:" << QFileInfo(argsRaw.at(0)).completeBaseName() << "[arguments]";
145   
146   // get size of longName field
147   QStringList keys = argsHash.keys();
148   uint lnameFieldSize = 0;
149   foreach (QString key, keys) {
150     uint size = 0;
151     if(argsHash.value(key).type == CliParserArg::CliArgOption)
152       size += key.size()*2;
153     else
154       size += key.size();
155     // this is for " --...=[....] "
156     size += 8;
157     if(size > lnameFieldSize) lnameFieldSize = size;
158   }
159   
160   QHash<QString, CliParserArg>::const_iterator arg;
161   for(arg = argsHash.constBegin(); arg != argsHash.constEnd(); ++arg) {
162     QString output;
163     QString lnameField;
164     
165     if(arg.value().shortName) {
166       output.append(" -").append(arg.value().shortName).append(",");
167     }
168     else output.append("    ");
169     lnameField.append(" --").append(arg.key());
170     if(arg.value().type == CliParserArg::CliArgOption) {
171       lnameField.append("=[").append(arg.key().toUpper()).append("]");
172     }
173     output.append(lnameField.leftJustified(lnameFieldSize));
174     if(!arg.value().help.isEmpty()) {
175       output.append(arg.value().help);
176     }
177     if(arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) {
178       output.append(". Default is: ").append(arg.value().def);
179     }
180     qWarning(output.toLatin1());
181   }
182 }
183
184 QString CliParser::value(const QString &longName) {
185   if(argsHash.contains(longName) && argsHash.value(longName).type == CliParserArg::CliArgOption) {
186     if(!argsHash.value(longName).value.isNull())
187       return argsHash.value(longName).value;
188     else
189       return argsHash.value(longName).def;
190   }
191   else {
192     qWarning() << "Warning: Requested value of not defined argument" << longName << "or argument is a switch";
193     return QString();
194   }
195 }
196
197 bool CliParser::isSet(const QString &longName) {
198   if(argsHash.contains(longName)) {
199     if(argsHash.value(longName).type == CliParserArg::CliArgOption) return !argsHash.value(longName).value.isNull();
200     else return argsHash.value(longName).boolValue;
201   }
202   else {
203     qWarning() << "Warning: Requested isSet of not defined argument" << longName;
204     return false;
205   }
206 }
207
208 QString CliParser::lnameOfShortArg(const char arg) {
209   QHash<QString, CliParserArg>::const_iterator cur;
210   for (cur = argsHash.constBegin(); cur != argsHash.constEnd(); ++cur) {
211     if(cur.value().shortName == arg) return cur.key();
212   }
213   return QString();
214 }