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