1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
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. *
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. *
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"
27 CliParser::CliParser(QStringList arguments)
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);
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);
45 else if (type == CliParserArg::CliArgSwitch && argsHash.value(name).type == type) {
46 argsHash[name].boolValue = true;
50 qWarning() << "Warning: Unrecognized argument:" << name;
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);
61 else if (type == CliParserArg::CliArgSwitch) {
62 if(argsHash.value(longName).type == type) {
63 argsHash[longName].boolValue = true;
66 // arg is an option but detected as a switch -> argument is missing
68 qWarning().nospace() << "Warning: '" << shortName << "' is an option which needs an argument";
73 qWarning().nospace() << "Warning: Unrecognized argument: '" << shortName << "'";
77 QString CliParser::escapedValue(const QString &value) {
78 QString escapedValue = value;
79 if(escapedValue.startsWith("~"))
80 escapedValue.replace(0, 1, QDir::homePath());
85 bool CliParser::parse() {
86 QStringList::const_iterator currentArg;
87 for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
88 if(currentArg->startsWith("--")) {
91 if(currentArg->contains("=")) {
93 QStringList tmp = currentArg->mid(2).split("=");
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;
103 name = currentArg->mid(2);
104 if(!addLongArg(CliParserArg::CliArgSwitch, name)) return false;
107 else if(currentArg->startsWith("-")) {
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("-")) {
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;
119 // if next arg is is no option/switch it's an argument to a shortoption
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";
128 bool skipNext = false;
129 if(nextArg != argsRaw.constEnd()) {
130 value = nextArg->toLocal8Bit();
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;
141 // we don't support plain arguments without -/--
142 if(currentArg->toLatin1() != argsRaw.at(0)) return false;
148 void CliParser::usage() {
149 qWarning() << "Usage:" << QFileInfo(argsRaw.at(0)).completeBaseName() << "[arguments]";
151 // get size of longName field
152 QStringList keys = argsHash.keys();
153 uint lnameFieldSize = 0;
154 foreach (QString key, keys) {
156 if(argsHash.value(key).type == CliParserArg::CliArgOption)
157 size += key.size()*2;
160 // this is for " --...=[....] "
162 if(size > lnameFieldSize) lnameFieldSize = size;
165 QHash<QString, CliParserArg>::const_iterator arg;
166 for(arg = argsHash.constBegin(); arg != argsHash.constEnd(); ++arg) {
170 if(arg.value().shortName) {
171 output.append(" -").append(arg.value().shortName).append(",");
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("]");
178 output.append(lnameField.leftJustified(lnameFieldSize));
179 if(!arg.value().help.isEmpty()) {
180 output.append(arg.value().help);
182 if(arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) {
183 output.append(". Default is: ").append(arg.value().def);
185 qWarning(output.toLatin1());
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;
194 return argsHash.value(longName).def;
197 qWarning() << "Warning: Requested value of not defined argument" << longName << "or argument is a switch";
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;
208 qWarning() << "Warning: Requested isSet of not defined argument" << longName;
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();