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