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"
25 CliParser::CliParser(QStringList arguments)
28 // remove Qt internal debugging arguments
29 argsRaw.removeOne("-sync");
30 argsRaw.removeOne("-nograb");
31 argsRaw.removeOne("-dograb");
34 void CliParser::addArgument(const CliParserArg::CliArgType type, const QString longName, const char shortName, const QString help, const QString def) {
36 if(type == CliParserArg::CliArgOption)
37 arg = CliParserArg(CliParserArg::CliArgOption, shortName, help, def);
39 arg = CliParserArg(CliParserArg::CliArgSwitch, shortName, help);
40 argsHash.insert(longName, arg);
43 bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString name, const QString value) {
44 if(argsHash.contains(name)){
45 if(type == CliParserArg::CliArgOption && argsHash.value(name).type == type) {
46 argsHash[name].value = value;
49 else if (type == CliParserArg::CliArgSwitch && argsHash.value(name).type == type) {
50 argsHash[name].boolValue = true;
57 bool CliParser::addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString value) {
58 QString longName = lnameOfShortArg(shortName);
59 if(!longName.isNull()) {
60 if(type == CliParserArg::CliArgOption && argsHash.value(longName).type == type) {
61 argsHash[longName].value = value;
64 else if (type == CliParserArg::CliArgSwitch && argsHash.value(longName).type == type) {
65 argsHash[longName].boolValue = true;
72 bool CliParser::parse() {
73 QStringList::const_iterator currentArg;
74 for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
75 if(currentArg->startsWith("--")) {
78 if(currentArg->contains("=")) {
80 QStringList tmp = currentArg->mid(2).split("=");
83 // this is needed to allow --option=""
84 if(tmp.at(1).isNull()) value = QString("");
85 else value = tmp.at(1);
86 if(!addLongArg(CliParserArg::CliArgOption, name, value)) return false;
90 name = currentArg->mid(2);
91 if(!addLongArg(CliParserArg::CliArgSwitch, name)) return false;
94 else if(currentArg->startsWith("-")) {
97 QStringList::const_iterator nextArg = currentArg+1;
98 // if next arg is a short/long option/switch the current arg is one too
99 if(nextArg == argsRaw.constEnd() || nextArg->startsWith("-")) {
101 for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) {
102 name = currentArg->mid(1).toAscii().at(i);
103 if(!addShortArg(CliParserArg::CliArgSwitch, name)) return false;
106 // if next arg is is no option/switch it's an argument to a shortoption
110 bool skipNext = false;
111 if(nextArg != argsRaw.constEnd()) {
112 value = nextArg->toLocal8Bit();
115 else value = currentArg->toLocal8Bit();
116 name = currentArg->mid(1).toAscii().at(0);
117 // we took one argument as argument to an option so skip it next time
118 if(skipNext) currentArg++;
119 if(!addShortArg(CliParserArg::CliArgOption, name, value)) return false;
123 // we don't support plain arguments without -/--
124 if(currentArg->toLatin1() != argsRaw.at(0)) return false;
130 void CliParser::usage() {
131 qWarning("Usage: %s [arguments]",QFileInfo(argsRaw.at(0)).completeBaseName().toLatin1().constData());
133 // get size of longName field
134 QStringList keys = argsHash.keys();
135 uint lnameFieldSize = 0;
136 foreach (QString key, keys) {
138 if(argsHash.value(key).type == CliParserArg::CliArgOption)
139 size += key.size()*2;
142 // this is for " --...=[....] "
144 if(size > lnameFieldSize) lnameFieldSize = size;
147 QHash<QString, CliParserArg>::const_iterator arg;
148 for(arg = argsHash.constBegin(); arg != argsHash.constEnd(); ++arg) {
152 if(arg.value().shortName) {
153 output.append(" -").append(arg.value().shortName).append(",");
155 else output.append(" ");
156 lnameField.append(" --").append(arg.key());
157 if(arg.value().type == CliParserArg::CliArgOption) {
158 lnameField.append("=[").append(arg.key().toUpper()).append("]");
160 output.append(lnameField.leftJustified(lnameFieldSize));
161 if(!arg.value().help.isEmpty()) {
162 output.append(arg.value().help);
164 if(arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) {
165 output.append(". Default is: ").append(arg.value().def);
167 qWarning(output.toLatin1());
171 QString CliParser::value(const QString &longName) {
172 if(argsHash.contains(longName) && argsHash.value(longName).type == CliParserArg::CliArgOption) {
173 if(!argsHash.value(longName).value.isNull())
174 return argsHash.value(longName).value;
176 return argsHash.value(longName).def;
179 qWarning("Warning: Requested value of not defined argument '%s' or argument is a switch",longName.toLatin1().constData());
184 bool CliParser::isSet(const QString &longName) {
185 if(argsHash.contains(longName)) {
186 if(argsHash.value(longName).type == CliParserArg::CliArgOption) return !argsHash.value(longName).value.isNull();
187 else return argsHash.value(longName).boolValue;
190 qWarning("Warning: Requested isSet of not defined argument '%s'",longName.toLatin1().constData());
195 QString CliParser::lnameOfShortArg(const char arg) {
196 QHash<QString, CliParserArg>::const_iterator cur;
197 for (cur = argsHash.constBegin(); cur != argsHash.constEnd(); ++cur) {
198 if(cur.value().shortName == arg) return cur.key();