1 /***************************************************************************
2 * Copyright (C) 2005-2015 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 if (argsMap.contains(longName)) qWarning() << "Warning: Multiple definition of argument" << longName;
38 if (arg.shortName != 0 && !lnameOfShortArg(arg.shortName).isNull())
39 qWarning().nospace() << "Warning: Redefining shortName '" << arg.shortName << "' for " << longName << " previously defined for " << lnameOfShortArg(arg.shortName);
40 argsMap.insert(longName, arg);
44 bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString &name, const QString &value)
46 if (argsMap.contains(name)) {
47 if (type == CliParserArg::CliArgOption && argsMap.value(name).type == type) {
48 argsMap[name].value = escapedValue(value);
51 else if (type == CliParserArg::CliArgSwitch && argsMap.value(name).type == type) {
52 argsMap[name].boolValue = true;
56 qWarning() << "Warning: Unrecognized argument:" << name;
61 bool CliParser::addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString &value)
63 QString longName = lnameOfShortArg(shortName);
64 if (!longName.isNull()) {
65 if (type == CliParserArg::CliArgOption && argsMap.value(longName).type == type) {
66 argsMap[longName].value = escapedValue(value);
69 else if (type == CliParserArg::CliArgSwitch) {
70 if (argsMap.value(longName).type == type) {
71 argsMap[longName].boolValue = true;
74 // arg is an option but detected as a switch -> argument is missing
76 qWarning().nospace() << "Warning: '" << shortName << "' is an option which needs an argument";
81 qWarning().nospace() << "Warning: Unrecognized argument: '" << shortName << "'";
86 QString CliParser::escapedValue(const QString &value)
88 QString escapedValue = value;
89 if (escapedValue.startsWith("~"))
90 escapedValue.replace(0, 1, QDir::homePath());
96 bool CliParser::init(const QStringList &args)
99 QStringList::const_iterator currentArg;
100 for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
101 if (currentArg->startsWith("--")) {
104 if (currentArg->contains("=")) {
106 QStringList tmp = currentArg->mid(2).split("=");
109 // this is needed to allow --option=""
110 if (tmp.at(1).isNull()) value = QString("");
111 else value = tmp.at(1);
112 if (!addLongArg(CliParserArg::CliArgOption, name, value)) return false;
116 name = currentArg->mid(2);
117 if (!addLongArg(CliParserArg::CliArgSwitch, name)) return false;
120 else if (currentArg->startsWith("-")) {
123 QStringList::const_iterator nextArg = currentArg+1;
124 // if next arg is a short/long option/switch the current arg is one too
125 if (nextArg == argsRaw.constEnd() || nextArg->startsWith("-")) {
127 for (int i = 0; i < currentArg->mid(1).toLatin1().size(); i++) {
128 name = currentArg->mid(1).toLatin1().at(i);
129 if (!addShortArg(CliParserArg::CliArgSwitch, name)) return false;
132 // if next arg is is no option/switch it's an argument to a shortoption
135 // short options are not freely mixable with other shortargs
136 if (currentArg->mid(1).toLatin1().size() > 1) {
137 qWarning() << "Warning: Shortoptions may not be combined with other shortoptions or switches";
141 bool skipNext = false;
142 if (nextArg != argsRaw.constEnd()) {
143 value = nextArg->toLocal8Bit();
146 else value = currentArg->toLocal8Bit();
147 name = currentArg->mid(1).toLatin1().at(0);
148 // we took one argument as argument to an option so skip it next time
149 if (skipNext) currentArg++;
150 if (!addShortArg(CliParserArg::CliArgOption, name, value)) return false;
154 // we don't support plain arguments without -/--
155 if (currentArg->toLatin1() != argsRaw.at(0)) return false;
162 void CliParser::usage()
164 std::cout << "Usage: " << qPrintable(QFileInfo(argsRaw.at(0)).completeBaseName()) << " [arguments]" << std::endl;
166 // get size of longName field
167 QStringList keys = argsMap.keys();
168 uint lnameFieldSize = 0;
169 foreach(QString key, keys) {
171 if (argsMap.value(key).type == CliParserArg::CliArgOption)
172 size += key.size()*2;
175 // this is for " --...=[....] "
177 if (size > lnameFieldSize) lnameFieldSize = size;
180 QMap<QString, CliParserArg>::const_iterator arg;
181 for (arg = argsMap.constBegin(); arg != argsMap.constEnd(); ++arg) {
185 if (arg.value().shortName) {
186 output.append(" -").append(arg.value().shortName).append(",");
188 else output.append(" ");
189 lnameField.append(" --").append(arg.key());
190 if (arg.value().type == CliParserArg::CliArgOption && !arg.value().valueName.isEmpty()) {
191 lnameField.append("=<").append(arg.value().valueName).append(">");
193 output.append(lnameField.leftJustified(lnameFieldSize));
194 if (!arg.value().help.isEmpty()) {
195 output.append(arg.value().help);
197 if (arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) {
198 output.append(". Default is: ").append(arg.value().def);
200 std::cout << qPrintable(output) << std::endl;
205 QString CliParser::value(const QString &longName)
207 if (argsMap.contains(longName) && argsMap.value(longName).type == CliParserArg::CliArgOption) {
208 if (!argsMap.value(longName).value.isNull())
209 return argsMap.value(longName).value;
211 return argsMap.value(longName).def;
214 qWarning() << "Warning: Requested value of not defined argument" << longName << "or argument is a switch";
220 bool CliParser::isSet(const QString &longName)
222 if (argsMap.contains(longName)) {
223 if (argsMap.value(longName).type == CliParserArg::CliArgOption) return !argsMap.value(longName).value.isNull();
224 else return argsMap.value(longName).boolValue;
227 qWarning() << "Warning: Requested isSet of not defined argument" << longName;
233 QString CliParser::lnameOfShortArg(const char arg)
235 QMap<QString, CliParserArg>::const_iterator cur;
236 for (cur = argsMap.constBegin(); cur != argsMap.constEnd(); ++cur) {
237 if (cur.value().shortName == arg) return cur.key();