8829d06645573d82a2ebe63a7f242ecc0a371b01
[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::addSwitch(const QString longName, const char shortName, const QString help) {
35   CliParserArg arg = CliParserArg(CliParserArg::CliArgSwitch, shortName, help);
36   argsHash.insert(longName, arg);
37   if(shortName) {
38    if(!shortHash.contains(shortName)) shortHash.insert(shortName, argsHash.find(longName));
39    else qWarning("Warning: shortName %c defined multiple times.", shortName);
40   }
41 }
42
43 void CliParser::addOption(const QString longName, const char shortName, const QString help, const QString def) {
44   CliParserArg arg = CliParserArg(CliParserArg::CliArgOption, shortName, help, def);
45   argsHash.insert(longName, arg);
46   if(shortName) {
47    if(!shortHash.contains(shortName)) shortHash.insert(shortName, argsHash.find(longName));
48    else qWarning("Warning: shortName %c defined multiple times.", shortName);
49   }
50 }
51
52 bool CliParser::parse() {
53   QStringList::const_iterator currentArg;
54   for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
55     if(currentArg->startsWith("--")) {
56       QString name;
57       // long
58       if(currentArg->contains("=")) {
59         // option
60         QStringList tmp = currentArg->mid(2).split("=");
61         name = tmp.at(0);
62         QString value = tmp.at(1);
63         if(argsHash.contains(name) && !value.isEmpty()){
64           argsHash[name].value = value;
65         }
66         else return false;
67       }
68       else {
69         // switch
70         name = currentArg->mid(2);
71         if(argsHash.contains(name)) {
72             argsHash[name].boolValue = true;
73         }
74         else return false;
75       }
76     }
77     else if(currentArg->startsWith("-")) {
78     char name;
79         // short
80       bool bla = true;
81       if(++currentArg == argsRaw.constEnd()) {
82         --currentArg;
83         bla = false;
84       }
85       // if next arg is a short/long option/switch the current arg is one too
86       if(currentArg->startsWith("-")) {
87         // switch
88         if(bla) --currentArg;
89         for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) {
90           name = currentArg->mid(1).toAscii().at(i);
91           if(shortHash.contains(name) && shortHash.value(name).value().type == CliParserArg::CliArgSwitch) {
92             shortHash[name].value().boolValue = true;
93           }
94           else return false;
95         }
96       }
97       // if next arg is is no option/switch it's an argument to a shortoption
98       else {
99         // option
100         QString value = currentArg->toLocal8Bit();
101         if(bla) --currentArg;
102         name = currentArg->mid(1).toAscii().at(0);
103         if(bla) currentArg++;
104         if(shortHash.contains(name) && shortHash.value(name).value().type == CliParserArg::CliArgOption) {
105           shortHash[name].value().value = value;
106         }
107         else return false;
108       }
109     }
110     else {
111       // we don't support plain arguments without -/--
112       if(currentArg->toLatin1() != argsRaw.at(0)) {
113         return false;
114       }
115     }
116   }
117   return true;
118 }
119
120 void CliParser::usage() {
121   qWarning("Usage: %s [arguments]",QFileInfo(argsRaw.at(0)).completeBaseName().toLatin1().constData());
122   
123   // get size of longName field
124   QStringList keys = argsHash.keys();
125   uint lnameFieldSize = 0;
126   foreach (QString key, keys) {
127     uint size = 0;
128     if(argsHash.value(key).type == CliParserArg::CliArgOption)
129       size += key.size()*2;
130     else
131       size += key.size();
132     // this is for " --...=[....] "
133     size += 8;
134     if(size > lnameFieldSize) lnameFieldSize = size;
135   }
136   
137   QHash<QString, CliParserArg>::const_iterator arg;
138   for(arg = argsHash.constBegin(); arg != argsHash.constEnd(); ++arg) {
139     QString output;
140     QString lnameField;
141     
142     if(arg.value().shortName) {
143       output.append(" -").append(arg.value().shortName).append(",");
144     }
145     else output.append("    ");
146     lnameField.append(" --").append(arg.key());
147     if(arg.value().type == CliParserArg::CliArgOption) {
148       lnameField.append("=[").append(arg.key().toUpper()).append("]");
149     }
150     output.append(lnameField.leftJustified(lnameFieldSize));
151     if(!arg.value().help.isEmpty()) {
152       output.append(arg.value().help);
153     }
154     if(arg.value().type == CliParserArg::CliArgOption) {
155       output.append(". Default is: ").append(arg.value().def);
156     }
157     qWarning(output.toLatin1());
158   }
159 }
160
161 QString CliParser::value(const QString &longName) {
162   if(argsHash.contains(longName) && argsHash.value(longName).type == CliParserArg::CliArgOption) {
163     if(!argsHash.value(longName).value.isEmpty())
164       return argsHash.value(longName).value;
165     else
166       return argsHash.value(longName).def;
167   }
168   else {
169     qWarning("Warning: Requested value of not defined argument '%s' or argument is a switch",longName.toLatin1().constData());
170     return QString();
171   }
172 }
173
174 bool CliParser::isSet(const QString &longName) {
175   if(argsHash.contains(longName)) {
176     if(argsHash.value(longName).type == CliParserArg::CliArgOption) return !argsHash.value(longName).value.isEmpty();
177     else return argsHash.value(longName).boolValue;
178   }
179   else {
180     qWarning("Warning: Requested isSet of not defined argument '%s'",longName.toLatin1().constData());
181     return false;
182   }
183 }
184
185 CliParserArg::CliParserArg(const CliArgType _type, const char _shortName, const QString _help, const QString _def)
186   : type(_type),
187     shortName(_shortName),
188     help(_help),
189     def(_def),
190     value(QString()),
191     boolValue(false)
192 {
193 }