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::addSwitch(const QString longName, const char shortName, const QString help) {
35 CliParserArg arg = CliParserArg(CliParserArg::CliArgSwitch, shortName, help);
36 argsHash.insert(longName, arg);
38 if(!shortHash.contains(shortName)) shortHash.insert(shortName, argsHash.find(longName));
39 else qWarning("Warning: shortName %c defined multiple times.", shortName);
43 void CliParser::addOption(const QString longName, const char shortName, const QString help, const QString def) {
44 CliParserArg arg = CliParserArg(CliParserArg::CliArgOption, shortName, help, def);
45 argsHash.insert(longName, arg);
47 if(!shortHash.contains(shortName)) shortHash.insert(shortName, argsHash.find(longName));
48 else qWarning("Warning: shortName %c defined multiple times.", shortName);
52 bool CliParser::parse() {
53 QStringList::const_iterator currentArg;
54 for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) {
55 if(currentArg->startsWith("--")) {
58 if(currentArg->contains("=")) {
60 QStringList tmp = currentArg->mid(2).split("=");
62 QString value = tmp.at(1);
63 if(argsHash.contains(name) && !value.isEmpty()){
64 argsHash[name].value = value;
70 name = currentArg->mid(2);
71 if(argsHash.contains(name)) {
72 argsHash[name].boolValue = true;
77 else if(currentArg->startsWith("-")) {
81 if(++currentArg == argsRaw.constEnd()) {
85 // if next arg is a short/long option/switch the current arg is one too
86 if(currentArg->startsWith("-")) {
89 for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) {
90 name = currentArg->mid(1).toAscii().at(i);
91 if(shortHash.contains(name) && shortHash.value(name).value().type == CliParserArg::CliArgSwitch) {
92 shortHash[name].value().boolValue = true;
97 // if next arg is is no option/switch it's an argument to a shortoption
100 QString value = currentArg->toLocal8Bit();
101 if(bla) --currentArg;
102 name = currentArg->mid(1).toAscii().at(0);
103 if(bla) currentArg++;
104 if(shortHash.contains(name) && shortHash.value(name).value().type == CliParserArg::CliArgOption) {
105 shortHash[name].value().value = value;
111 // we don't support plain arguments without -/--
112 if(currentArg->toLatin1() != argsRaw.at(0)) {
120 void CliParser::usage() {
121 qWarning("Usage: %s [arguments]",QFileInfo(argsRaw.at(0)).completeBaseName().toLatin1().constData());
123 // get size of longName field
124 QStringList keys = argsHash.keys();
125 uint lnameFieldSize = 0;
126 foreach (QString key, keys) {
128 if(argsHash.value(key).type == CliParserArg::CliArgOption)
129 size += key.size()*2;
132 // this is for " --...=[....] "
134 if(size > lnameFieldSize) lnameFieldSize = size;
137 QHash<QString, CliParserArg>::const_iterator arg;
138 for(arg = argsHash.constBegin(); arg != argsHash.constEnd(); ++arg) {
142 if(arg.value().shortName) {
143 output.append(" -").append(arg.value().shortName).append(",");
145 else output.append(" ");
146 lnameField.append(" --").append(arg.key());
147 if(arg.value().type == CliParserArg::CliArgOption) {
148 lnameField.append("=[").append(arg.key().toUpper()).append("]");
150 output.append(lnameField.leftJustified(lnameFieldSize));
151 if(!arg.value().help.isEmpty()) {
152 output.append(arg.value().help);
154 if(arg.value().type == CliParserArg::CliArgOption) {
155 output.append(". Default is: ").append(arg.value().def);
157 qWarning(output.toLatin1());
161 QString CliParser::value(const QString &longName) {
162 if(argsHash.contains(longName) && argsHash.value(longName).type == CliParserArg::CliArgOption) {
163 if(!argsHash.value(longName).value.isEmpty())
164 return argsHash.value(longName).value;
166 return argsHash.value(longName).def;
169 qWarning("Warning: Requested value of not defined argument '%s' or argument is a switch",longName.toLatin1().constData());
174 bool CliParser::isSet(const QString &longName) {
175 if(argsHash.contains(longName)) {
176 if(argsHash.value(longName).type == CliParserArg::CliArgOption) return !argsHash.value(longName).value.isEmpty();
177 else return argsHash.value(longName).boolValue;
180 qWarning("Warning: Requested isSet of not defined argument '%s'",longName.toLatin1().constData());
185 CliParserArg::CliParserArg(const CliArgType _type, const char _shortName, const QString _help, const QString _def)
187 shortName(_shortName),
195 CliParserArg::CliParserArg(const CliParserArg &other) {
197 shortName = other.shortName;
201 boolValue = other.boolValue;
204 CliParserArg &CliParserArg::operator=(const CliParserArg &other) {
206 shortName = other.shortName;
210 boolValue = other.boolValue;