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"
29 CliParser::CliParser() : AbstractCliParser()
34 void CliParser::addArgument(const QString &longName_, const CliParserArg &arg)
36 QString longName = longName_;
37 longName.remove(QRegExp("\\s*<.*>\\s*")); // KCmdLineArgs takes args of the form "arg <defval>"
38 if (argsMap.contains(longName)) qWarning() << "Warning: Multiple definition of argument" << longName;
39 if (arg.shortName != 0 && !lnameOfShortArg(arg.shortName).isNull())
40 qWarning().nospace() << "Warning: Redefining shortName '" << arg.shortName << "' for " << longName << " previously defined for " << lnameOfShortArg(arg.shortName);
41 argsMap.insert(longName, arg);
45 bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString &name, const QString &value)
47 if (argsMap.contains(name)) {
48 if (type == CliParserArg::CliArgOption && argsMap.value(name).type == type) {
49 argsMap[name].value = escapedValue(value);
52 else if (type == CliParserArg::CliArgSwitch && argsMap.value(name).type == type) {
53 argsMap[name].boolValue = true;
57 qWarning() << "Warning: Unrecognized argument:" << name;
62 bool CliParser::addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString &value)
64 QString longName = lnameOfShortArg(shortName);
65 if (!longName.isNull()) {
66 if (type == CliParserArg::CliArgOption && argsMap.value(longName).type == type) {
67 argsMap[longName].value = escapedValue(value);
70 else if (type == CliParserArg::CliArgSwitch) {
71 if (argsMap.value(longName).type == type) {
72 argsMap[longName].boolValue = true;
75 // arg is an option but detected as a switch -> argument is missing
77 qWarning().nospace() << "Warning: '" << shortName << "' is an option which needs an argument";
82 qWarning().nospace() << "Warning: Unrecognized argument: '" << shortName << "'";
87 QString CliParser::escapedValue(const QString &value)
89 QString escapedValue = value;
90 if (escapedValue.startsWith("~"))
91 escapedValue.replace(0, 1, QDir::homePath());
97 bool CliParser::init(const QStringList &args)
100 QStringList::const_iterator currentArg;
101 for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
102 if (currentArg->startsWith("--")) {
105 if (currentArg->contains("=")) {
107 QStringList tmp = currentArg->mid(2).split("=");
110 // this is needed to allow --option=""
111 if (tmp.at(1).isNull()) value = QString("");
112 else value = tmp.at(1);
113 if (!addLongArg(CliParserArg::CliArgOption, name, value)) return false;
117 name = currentArg->mid(2);
118 if (!addLongArg(CliParserArg::CliArgSwitch, name)) return false;
121 else if (currentArg->startsWith("-")) {
124 QStringList::const_iterator nextArg = currentArg+1;
125 // if next arg is a short/long option/switch the current arg is one too
126 if (nextArg == argsRaw.constEnd() || nextArg->startsWith("-")) {
128 for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) {
129 name = currentArg->mid(1).toAscii().at(i);
130 if (!addShortArg(CliParserArg::CliArgSwitch, name)) return false;
133 // if next arg is is no option/switch it's an argument to a shortoption
136 // short options are not freely mixable with other shortargs
137 if (currentArg->mid(1).toAscii().size() > 1) {
138 qWarning() << "Warning: Shortoptions may not be combined with other shortoptions or switches";
142 bool skipNext = false;
143 if (nextArg != argsRaw.constEnd()) {
144 value = nextArg->toLocal8Bit();
147 else value = currentArg->toLocal8Bit();
148 name = currentArg->mid(1).toAscii().at(0);
149 // we took one argument as argument to an option so skip it next time
150 if (skipNext) currentArg++;
151 if (!addShortArg(CliParserArg::CliArgOption, name, value)) return false;
155 // we don't support plain arguments without -/--
156 if (currentArg->toLatin1() != argsRaw.at(0)) return false;
163 void CliParser::usage()
165 std::cout << "Usage: " << qPrintable(QFileInfo(argsRaw.at(0)).completeBaseName()) << " [arguments]" << std::endl;
167 // get size of longName field
168 QStringList keys = argsMap.keys();
169 uint lnameFieldSize = 0;
170 foreach(QString key, keys) {
172 if (argsMap.value(key).type == CliParserArg::CliArgOption)
173 size += key.size()*2;
176 // this is for " --...=[....] "
178 if (size > lnameFieldSize) lnameFieldSize = size;
181 QMap<QString, CliParserArg>::const_iterator arg;
182 for (arg = argsMap.constBegin(); arg != argsMap.constEnd(); ++arg) {
186 if (arg.value().shortName) {
187 output.append(" -").append(arg.value().shortName).append(",");
189 else output.append(" ");
190 lnameField.append(" --").append(arg.key());
191 if (arg.value().type == CliParserArg::CliArgOption) {
192 lnameField.append("=[").append(arg.key().toUpper()).append("]");
194 output.append(lnameField.leftJustified(lnameFieldSize));
195 if (!arg.value().help.isEmpty()) {
196 output.append(arg.value().help);
198 if (arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) {
199 output.append(". Default is: ").append(arg.value().def);
201 std::cout << qPrintable(output) << std::endl;
206 QString CliParser::value(const QString &longName)
208 if (argsMap.contains(longName) && argsMap.value(longName).type == CliParserArg::CliArgOption) {
209 if (!argsMap.value(longName).value.isNull())
210 return argsMap.value(longName).value;
212 return argsMap.value(longName).def;
215 qWarning() << "Warning: Requested value of not defined argument" << longName << "or argument is a switch";
221 bool CliParser::isSet(const QString &longName)
223 if (argsMap.contains(longName)) {
224 if (argsMap.value(longName).type == CliParserArg::CliArgOption) return !argsMap.value(longName).value.isNull();
225 else return argsMap.value(longName).boolValue;
228 qWarning() << "Warning: Requested isSet of not defined argument" << longName;
234 QString CliParser::lnameOfShortArg(const char arg)
236 QMap<QString, CliParserArg>::const_iterator cur;
237 for (cur = argsMap.constBegin(); cur != argsMap.constEnd(); ++cur) {
238 if (cur.value().shortName == arg) return cur.key();