Rewrite of various parts to enhance codingstyle, memory usage and readability.
[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
25 CliParser::CliParser(QStringList arguments)
26 {
27   argsRaw = arguments;
28 //   remove Qt internal debugging arguments 
29   argsRaw.removeOne("-sync");
30   argsRaw.removeOne("-nograb");
31   argsRaw.removeOne("-dograb");
32 }
33
34 void CliParser::addArgument(const CliParserArg::CliArgType type, const QString longName, const char shortName, const QString help, const QString def) {
35   CliParserArg arg;
36   if(type == CliParserArg::CliArgOption)
37     arg = CliParserArg(CliParserArg::CliArgOption, shortName, help, def);
38   else
39     arg = CliParserArg(CliParserArg::CliArgSwitch, shortName, help);
40   argsHash.insert(longName, arg);
41 }
42
43 bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString name, const QString value) {
44   if(argsHash.contains(name)){
45     if(type == CliParserArg::CliArgOption && argsHash.value(name).type == type) {
46       argsHash[name].value = value;
47       return true;
48     }
49     else if (type == CliParserArg::CliArgSwitch && argsHash.value(name).type == type) {
50       argsHash[name].boolValue = true;
51       return true;
52     }
53   }
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 && argsHash.value(longName).type == type) {
65       argsHash[longName].boolValue = true;
66       return true;
67     }
68   }
69   return false;
70 }
71
72 bool CliParser::parse() {
73   QStringList::const_iterator currentArg;
74   for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
75     if(currentArg->startsWith("--")) {
76       // long
77       QString name;
78       if(currentArg->contains("=")) {
79         // option
80         QStringList tmp = currentArg->mid(2).split("=");
81         name = tmp.at(0);
82         QString value;
83         // this is needed to allow --option=""
84         if(tmp.at(1).isNull()) value = QString("");
85         else value = tmp.at(1);
86         if(!addLongArg(CliParserArg::CliArgOption, name, value)) return false;
87       }
88       else {
89         // switch
90         name = currentArg->mid(2);
91         if(!addLongArg(CliParserArg::CliArgSwitch, name)) return false;
92       }
93     }
94     else if(currentArg->startsWith("-")) {
95       // short
96       char name;
97       QStringList::const_iterator nextArg = currentArg+1;
98       // if next arg is a short/long option/switch the current arg is one too
99       if(nextArg == argsRaw.constEnd() || nextArg->startsWith("-")) {
100         // switch
101         for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) {
102           name = currentArg->mid(1).toAscii().at(i);
103           if(!addShortArg(CliParserArg::CliArgSwitch, name)) return false;
104         }
105       }
106       // if next arg is is no option/switch it's an argument to a shortoption
107       else {
108         // option
109         QString value;
110         bool skipNext = false;
111         if(nextArg != argsRaw.constEnd()) {
112           value = nextArg->toLocal8Bit();
113           skipNext = true;
114         }
115         else value = currentArg->toLocal8Bit();
116         name = currentArg->mid(1).toAscii().at(0);
117         // we took one argument as argument to an option so skip it next time
118         if(skipNext) currentArg++;
119         if(!addShortArg(CliParserArg::CliArgOption, name, value)) return false;
120       }
121     }
122     else {
123       // we don't support plain arguments without -/--
124       if(currentArg->toLatin1() != argsRaw.at(0)) return false;
125     }
126   }
127   return true;
128 }
129
130 void CliParser::usage() {
131   qWarning("Usage: %s [arguments]",QFileInfo(argsRaw.at(0)).completeBaseName().toLatin1().constData());
132   
133   // get size of longName field
134   QStringList keys = argsHash.keys();
135   uint lnameFieldSize = 0;
136   foreach (QString key, keys) {
137     uint size = 0;
138     if(argsHash.value(key).type == CliParserArg::CliArgOption)
139       size += key.size()*2;
140     else
141       size += key.size();
142     // this is for " --...=[....] "
143     size += 8;
144     if(size > lnameFieldSize) lnameFieldSize = size;
145   }
146   
147   QHash<QString, CliParserArg>::const_iterator arg;
148   for(arg = argsHash.constBegin(); arg != argsHash.constEnd(); ++arg) {
149     QString output;
150     QString lnameField;
151     
152     if(arg.value().shortName) {
153       output.append(" -").append(arg.value().shortName).append(",");
154     }
155     else output.append("    ");
156     lnameField.append(" --").append(arg.key());
157     if(arg.value().type == CliParserArg::CliArgOption) {
158       lnameField.append("=[").append(arg.key().toUpper()).append("]");
159     }
160     output.append(lnameField.leftJustified(lnameFieldSize));
161     if(!arg.value().help.isEmpty()) {
162       output.append(arg.value().help);
163     }
164     if(arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) {
165       output.append(". Default is: ").append(arg.value().def);
166     }
167     qWarning(output.toLatin1());
168   }
169 }
170
171 QString CliParser::value(const QString &longName) {
172   if(argsHash.contains(longName) && argsHash.value(longName).type == CliParserArg::CliArgOption) {
173     if(!argsHash.value(longName).value.isNull())
174       return argsHash.value(longName).value;
175     else
176       return argsHash.value(longName).def;
177   }
178   else {
179     qWarning("Warning: Requested value of not defined argument '%s' or argument is a switch",longName.toLatin1().constData());
180     return QString();
181   }
182 }
183
184 bool CliParser::isSet(const QString &longName) {
185   if(argsHash.contains(longName)) {
186     if(argsHash.value(longName).type == CliParserArg::CliArgOption) return !argsHash.value(longName).value.isNull();
187     else return argsHash.value(longName).boolValue;
188   }
189   else {
190     qWarning("Warning: Requested isSet of not defined argument '%s'",longName.toLatin1().constData());
191     return false;
192   }
193 }
194
195 QString CliParser::lnameOfShortArg(const char arg) {
196   QHash<QString, CliParserArg>::const_iterator cur;
197   for (cur = argsHash.constBegin(); cur != argsHash.constEnd(); ++cur) {
198     if(cur.value().shortName == arg) return cur.key();
199   }
200   return QString();
201 }