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 void CliParser::addArgument(const QString &longName, const CliParserArg &arg) {
28 if(argsHash.contains(longName)) qWarning() << "Warning: Multiple definition of argument" << longName;
29 if(arg.shortName != 0 && !lnameOfShortArg(arg.shortName).isNull())
30 qWarning().nospace() << "Warning: Redefining shortName '" << arg.shortName << "' for " << longName << " previously defined for " << lnameOfShortArg(arg.shortName);
31 argsHash.insert(longName, arg);
34 bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString &name, const QString &value) {
35 if(argsHash.contains(name)){
36 if(type == CliParserArg::CliArgOption && argsHash.value(name).type == type) {
37 argsHash[name].value = escapedValue(value);
40 else if (type == CliParserArg::CliArgSwitch && argsHash.value(name).type == type) {
41 argsHash[name].boolValue = true;
45 qWarning() << "Warning: Unrecognized argument:" << name;
49 bool CliParser::addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString &value) {
50 QString longName = lnameOfShortArg(shortName);
51 if(!longName.isNull()) {
52 if(type == CliParserArg::CliArgOption && argsHash.value(longName).type == type) {
53 argsHash[longName].value = escapedValue(value);
56 else if (type == CliParserArg::CliArgSwitch) {
57 if(argsHash.value(longName).type == type) {
58 argsHash[longName].boolValue = true;
61 // arg is an option but detected as a switch -> argument is missing
63 qWarning().nospace() << "Warning: '" << shortName << "' is an option which needs an argument";
68 qWarning().nospace() << "Warning: Unrecognized argument: '" << shortName << "'";
72 QString CliParser::escapedValue(const QString &value) {
73 QString escapedValue = value;
74 if(escapedValue.startsWith("~"))
75 escapedValue.replace(0, 1, QDir::homePath());
80 bool CliParser::parse(const QStringList &args) {
82 QStringList::const_iterator currentArg;
83 for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
84 if(currentArg->startsWith("--")) {
87 if(currentArg->contains("=")) {
89 QStringList tmp = currentArg->mid(2).split("=");
92 // this is needed to allow --option=""
93 if(tmp.at(1).isNull()) value = QString("");
94 else value = tmp.at(1);
95 if(!addLongArg(CliParserArg::CliArgOption, name, value)) return false;
99 name = currentArg->mid(2);
100 if(!addLongArg(CliParserArg::CliArgSwitch, name)) return false;
103 else if(currentArg->startsWith("-")) {
106 QStringList::const_iterator nextArg = currentArg+1;
107 // if next arg is a short/long option/switch the current arg is one too
108 if(nextArg == argsRaw.constEnd() || nextArg->startsWith("-")) {
110 for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) {
111 name = currentArg->mid(1).toAscii().at(i);
112 if(!addShortArg(CliParserArg::CliArgSwitch, name)) return false;
115 // if next arg is is no option/switch it's an argument to a shortoption
118 // short options are not freely mixable with other shortargs
119 if(currentArg->mid(1).toAscii().size() > 1) {
120 qWarning() << "Warning: Shortoptions may not be combined with other shortoptions or switches";
124 bool skipNext = false;
125 if(nextArg != argsRaw.constEnd()) {
126 value = nextArg->toLocal8Bit();
129 else value = currentArg->toLocal8Bit();
130 name = currentArg->mid(1).toAscii().at(0);
131 // we took one argument as argument to an option so skip it next time
132 if(skipNext) currentArg++;
133 if(!addShortArg(CliParserArg::CliArgOption, name, value)) return false;
137 // we don't support plain arguments without -/--
138 if(currentArg->toLatin1() != argsRaw.at(0)) return false;
144 void CliParser::usage() {
145 qWarning() << "Usage:" << QFileInfo(argsRaw.at(0)).completeBaseName() << "[arguments]";
147 // get size of longName field
148 QStringList keys = argsHash.keys();
149 uint lnameFieldSize = 0;
150 foreach (QString key, keys) {
152 if(argsHash.value(key).type == CliParserArg::CliArgOption)
153 size += key.size()*2;
156 // this is for " --...=[....] "
158 if(size > lnameFieldSize) lnameFieldSize = size;
161 QHash<QString, CliParserArg>::const_iterator arg;
162 for(arg = argsHash.constBegin(); arg != argsHash.constEnd(); ++arg) {
166 if(arg.value().shortName) {
167 output.append(" -").append(arg.value().shortName).append(",");
169 else output.append(" ");
170 lnameField.append(" --").append(arg.key());
171 if(arg.value().type == CliParserArg::CliArgOption) {
172 lnameField.append("=[").append(arg.key().toUpper()).append("]");
174 output.append(lnameField.leftJustified(lnameFieldSize));
175 if(!arg.value().help.isEmpty()) {
176 output.append(arg.value().help);
178 if(arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) {
179 output.append(". Default is: ").append(arg.value().def);
181 qWarning(output.toLatin1());
185 QString CliParser::value(const QString &longName) {
186 if(argsHash.contains(longName) && argsHash.value(longName).type == CliParserArg::CliArgOption) {
187 if(!argsHash.value(longName).value.isNull())
188 return argsHash.value(longName).value;
190 return argsHash.value(longName).def;
193 qWarning() << "Warning: Requested value of not defined argument" << longName << "or argument is a switch";
198 bool CliParser::isSet(const QString &longName) {
199 if(argsHash.contains(longName)) {
200 if(argsHash.value(longName).type == CliParserArg::CliArgOption) return !argsHash.value(longName).value.isNull();
201 else return argsHash.value(longName).boolValue;
204 qWarning() << "Warning: Requested isSet of not defined argument" << longName;
209 QString CliParser::lnameOfShortArg(const char arg) {
210 QHash<QString, CliParserArg>::const_iterator cur;
211 for (cur = argsHash.constBegin(); cur != argsHash.constEnd(); ++cur) {
212 if(cur.value().shortName == arg) return cur.key();