1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
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() : AbstractCliParser() {
31 void CliParser::addArgument(const QString &longName_, const CliParserArg &arg) {
32 QString longName = longName_;
33 longName.remove(QRegExp("\\s*<.*>\\s*")); // KCmdLineArgs takes args of the form "arg <defval>"
34 if(argsHash.contains(longName)) qWarning() << "Warning: Multiple definition of argument" << longName;
35 if(arg.shortName != 0 && !lnameOfShortArg(arg.shortName).isNull())
36 qWarning().nospace() << "Warning: Redefining shortName '" << arg.shortName << "' for " << longName << " previously defined for " << lnameOfShortArg(arg.shortName);
37 argsHash.insert(longName, arg);
40 bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString &name, const QString &value) {
41 if(argsHash.contains(name)){
42 if(type == CliParserArg::CliArgOption && argsHash.value(name).type == type) {
43 argsHash[name].value = escapedValue(value);
46 else if (type == CliParserArg::CliArgSwitch && argsHash.value(name).type == type) {
47 argsHash[name].boolValue = true;
51 qWarning() << "Warning: Unrecognized argument:" << name;
55 bool CliParser::addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString &value) {
56 QString longName = lnameOfShortArg(shortName);
57 if(!longName.isNull()) {
58 if(type == CliParserArg::CliArgOption && argsHash.value(longName).type == type) {
59 argsHash[longName].value = escapedValue(value);
62 else if (type == CliParserArg::CliArgSwitch) {
63 if(argsHash.value(longName).type == type) {
64 argsHash[longName].boolValue = true;
67 // arg is an option but detected as a switch -> argument is missing
69 qWarning().nospace() << "Warning: '" << shortName << "' is an option which needs an argument";
74 qWarning().nospace() << "Warning: Unrecognized argument: '" << shortName << "'";
78 QString CliParser::escapedValue(const QString &value) {
79 QString escapedValue = value;
80 if(escapedValue.startsWith("~"))
81 escapedValue.replace(0, 1, QDir::homePath());
86 bool CliParser::init(const QStringList &args) {
88 QStringList::const_iterator currentArg;
89 for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
90 if(currentArg->startsWith("--")) {
93 if(currentArg->contains("=")) {
95 QStringList tmp = currentArg->mid(2).split("=");
98 // this is needed to allow --option=""
99 if(tmp.at(1).isNull()) value = QString("");
100 else value = tmp.at(1);
101 if(!addLongArg(CliParserArg::CliArgOption, name, value)) return false;
105 name = currentArg->mid(2);
106 if(!addLongArg(CliParserArg::CliArgSwitch, name)) return false;
109 else if(currentArg->startsWith("-")) {
112 QStringList::const_iterator nextArg = currentArg+1;
113 // if next arg is a short/long option/switch the current arg is one too
114 if(nextArg == argsRaw.constEnd() || nextArg->startsWith("-")) {
116 for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) {
117 name = currentArg->mid(1).toAscii().at(i);
118 if(!addShortArg(CliParserArg::CliArgSwitch, name)) return false;
121 // if next arg is is no option/switch it's an argument to a shortoption
124 // short options are not freely mixable with other shortargs
125 if(currentArg->mid(1).toAscii().size() > 1) {
126 qWarning() << "Warning: Shortoptions may not be combined with other shortoptions or switches";
130 bool skipNext = false;
131 if(nextArg != argsRaw.constEnd()) {
132 value = nextArg->toLocal8Bit();
135 else value = currentArg->toLocal8Bit();
136 name = currentArg->mid(1).toAscii().at(0);
137 // we took one argument as argument to an option so skip it next time
138 if(skipNext) currentArg++;
139 if(!addShortArg(CliParserArg::CliArgOption, name, value)) return false;
143 // we don't support plain arguments without -/--
144 if(currentArg->toLatin1() != argsRaw.at(0)) return false;
150 void CliParser::usage() {
151 qWarning() << "Usage:" << QFileInfo(argsRaw.at(0)).completeBaseName() << "[arguments]";
153 // get size of longName field
154 QStringList keys = argsHash.keys();
155 uint lnameFieldSize = 0;
156 foreach (QString key, keys) {
158 if(argsHash.value(key).type == CliParserArg::CliArgOption)
159 size += key.size()*2;
162 // this is for " --...=[....] "
164 if(size > lnameFieldSize) lnameFieldSize = size;
167 QHash<QString, CliParserArg>::const_iterator arg;
168 for(arg = argsHash.constBegin(); arg != argsHash.constEnd(); ++arg) {
172 if(arg.value().shortName) {
173 output.append(" -").append(arg.value().shortName).append(",");
175 else output.append(" ");
176 lnameField.append(" --").append(arg.key());
177 if(arg.value().type == CliParserArg::CliArgOption) {
178 lnameField.append("=[").append(arg.key().toUpper()).append("]");
180 output.append(lnameField.leftJustified(lnameFieldSize));
181 if(!arg.value().help.isEmpty()) {
182 output.append(arg.value().help);
184 if(arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) {
185 output.append(". Default is: ").append(arg.value().def);
187 qWarning() << qPrintable(output);
191 QString CliParser::value(const QString &longName) {
192 if(argsHash.contains(longName) && argsHash.value(longName).type == CliParserArg::CliArgOption) {
193 if(!argsHash.value(longName).value.isNull())
194 return argsHash.value(longName).value;
196 return argsHash.value(longName).def;
199 qWarning() << "Warning: Requested value of not defined argument" << longName << "or argument is a switch";
204 bool CliParser::isSet(const QString &longName) {
205 if(argsHash.contains(longName)) {
206 if(argsHash.value(longName).type == CliParserArg::CliArgOption) return !argsHash.value(longName).value.isNull();
207 else return argsHash.value(longName).boolValue;
210 qWarning() << "Warning: Requested isSet of not defined argument" << longName;
215 QString CliParser::lnameOfShortArg(const char arg) {
216 QHash<QString, CliParserArg>::const_iterator cur;
217 for (cur = argsHash.constBegin(); cur != argsHash.constEnd(); ++cur) {
218 if(cur.value().shortName == arg) return cur.key();