1 /***************************************************************************
2 * Copyright (C) 2005-2014 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "cliparser.h"
30 CliParser::CliParser() : AbstractCliParser()
35 void CliParser::addArgument(const QString &longName_, const CliParserArg &arg)
37 QString longName = longName_;
38 longName.remove(QRegExp("\\s*<.*>\\s*")); // KCmdLineArgs takes args of the form "arg <defval>"
39 if (argsMap.contains(longName)) qWarning() << "Warning: Multiple definition of argument" << longName;
40 if (arg.shortName != 0 && !lnameOfShortArg(arg.shortName).isNull())
41 qWarning().nospace() << "Warning: Redefining shortName '" << arg.shortName << "' for " << longName << " previously defined for " << lnameOfShortArg(arg.shortName);
42 argsMap.insert(longName, arg);
46 bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString &name, const QString &value)
48 if (argsMap.contains(name)) {
49 if (type == CliParserArg::CliArgOption && argsMap.value(name).type == type) {
50 argsMap[name].value = escapedValue(value);
53 else if (type == CliParserArg::CliArgSwitch && argsMap.value(name).type == type) {
54 argsMap[name].boolValue = true;
58 qWarning() << "Warning: Unrecognized argument:" << name;
63 bool CliParser::addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString &value)
65 QString longName = lnameOfShortArg(shortName);
66 if (!longName.isNull()) {
67 if (type == CliParserArg::CliArgOption && argsMap.value(longName).type == type) {
68 argsMap[longName].value = escapedValue(value);
71 else if (type == CliParserArg::CliArgSwitch) {
72 if (argsMap.value(longName).type == type) {
73 argsMap[longName].boolValue = true;
76 // arg is an option but detected as a switch -> argument is missing
78 qWarning().nospace() << "Warning: '" << shortName << "' is an option which needs an argument";
83 qWarning().nospace() << "Warning: Unrecognized argument: '" << shortName << "'";
88 QString CliParser::escapedValue(const QString &value)
90 QString escapedValue = value;
91 if (escapedValue.startsWith("~"))
92 escapedValue.replace(0, 1, QDir::homePath());
98 bool CliParser::init(const QStringList &args)
101 QStringList::const_iterator currentArg;
102 for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
103 if (currentArg->startsWith("--")) {
106 if (currentArg->contains("=")) {
108 QStringList tmp = currentArg->mid(2).split("=");
111 // this is needed to allow --option=""
112 if (tmp.at(1).isNull()) value = QString("");
113 else value = tmp.at(1);
114 if (!addLongArg(CliParserArg::CliArgOption, name, value)) return false;
118 name = currentArg->mid(2);
119 if (!addLongArg(CliParserArg::CliArgSwitch, name)) return false;
122 else if (currentArg->startsWith("-")) {
125 QStringList::const_iterator nextArg = currentArg+1;
126 // if next arg is a short/long option/switch the current arg is one too
127 if (nextArg == argsRaw.constEnd() || nextArg->startsWith("-")) {
129 for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) {
130 name = currentArg->mid(1).toAscii().at(i);
131 if (!addShortArg(CliParserArg::CliArgSwitch, name)) return false;
134 // if next arg is is no option/switch it's an argument to a shortoption
137 // short options are not freely mixable with other shortargs
138 if (currentArg->mid(1).toAscii().size() > 1) {
139 qWarning() << "Warning: Shortoptions may not be combined with other shortoptions or switches";
143 bool skipNext = false;
144 if (nextArg != argsRaw.constEnd()) {
145 value = nextArg->toLocal8Bit();
148 else value = currentArg->toLocal8Bit();
149 name = currentArg->mid(1).toAscii().at(0);
150 // we took one argument as argument to an option so skip it next time
151 if (skipNext) currentArg++;
152 if (!addShortArg(CliParserArg::CliArgOption, name, value)) return false;
156 // we don't support plain arguments without -/--
157 if (currentArg->toLatin1() != argsRaw.at(0)) return false;
164 void CliParser::usage()
166 std::cout << "Usage: " << qPrintable(QFileInfo(argsRaw.at(0)).completeBaseName()) << " [arguments]" << std::endl;
168 // get size of longName field
169 QStringList keys = argsMap.keys();
170 uint lnameFieldSize = 0;
171 foreach(QString key, keys) {
173 if (argsMap.value(key).type == CliParserArg::CliArgOption)
174 size += key.size()*2;
177 // this is for " --...=[....] "
179 if (size > lnameFieldSize) lnameFieldSize = size;
182 QMap<QString, CliParserArg>::const_iterator arg;
183 for (arg = argsMap.constBegin(); arg != argsMap.constEnd(); ++arg) {
187 if (arg.value().shortName) {
188 output.append(" -").append(arg.value().shortName).append(",");
190 else output.append(" ");
191 lnameField.append(" --").append(arg.key());
192 if (arg.value().type == CliParserArg::CliArgOption) {
193 lnameField.append("=[").append(arg.key().toUpper()).append("]");
195 output.append(lnameField.leftJustified(lnameFieldSize));
196 if (!arg.value().help.isEmpty()) {
197 output.append(arg.value().help);
199 if (arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) {
200 output.append(". Default is: ").append(arg.value().def);
202 std::cout << qPrintable(output) << std::endl;
207 QString CliParser::value(const QString &longName)
209 if (argsMap.contains(longName) && argsMap.value(longName).type == CliParserArg::CliArgOption) {
210 if (!argsMap.value(longName).value.isNull())
211 return argsMap.value(longName).value;
213 return argsMap.value(longName).def;
216 qWarning() << "Warning: Requested value of not defined argument" << longName << "or argument is a switch";
222 bool CliParser::isSet(const QString &longName)
224 if (argsMap.contains(longName)) {
225 if (argsMap.value(longName).type == CliParserArg::CliArgOption) return !argsMap.value(longName).value.isNull();
226 else return argsMap.value(longName).boolValue;
229 qWarning() << "Warning: Requested isSet of not defined argument" << longName;
235 QString CliParser::lnameOfShortArg(const char arg)
237 QMap<QString, CliParserArg>::const_iterator cur;
238 for (cur = argsMap.constBegin(); cur != argsMap.constEnd(); ++cur) {
239 if (cur.value().shortName == arg) return cur.key();