X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fcliparser.cpp;h=9fbcacfe8e6c24a6d7f3d9a73f934464eb720f6c;hp=8b1b559e62df02bdcf838c0d16c9214150144e17;hb=c2a02924342a5d9a0ebbc1794e9f7df4ca13c50d;hpb=6eb132ee838e98925303db804fd9f36a0c2f1c1b diff --git a/src/common/cliparser.cpp b/src/common/cliparser.cpp index 8b1b559e..9fbcacfe 100644 --- a/src/common/cliparser.cpp +++ b/src/common/cliparser.cpp @@ -21,6 +21,7 @@ #include #include +#include CliParser::CliParser(QStringList arguments) { @@ -31,94 +32,114 @@ CliParser::CliParser(QStringList arguments) argsRaw.removeOne("-dograb"); } -void CliParser::addSwitch(const QString longName, const char shortName, const QString help) { - CliParserArg arg = CliParserArg(CliParserArg::CliArgSwitch, shortName, help); +void CliParser::addArgument(const QString &longName, const CliParserArg &arg) { + if(argsHash.contains(longName)) qWarning() << "Warning: Multiple definition of argument" << longName; + if(arg.shortName != 0 && !lnameOfShortArg(arg.shortName).isNull()) + qWarning().nospace() << "Warning: Redefining shortName '" << arg.shortName << "' for " << longName << " previously defined for " << lnameOfShortArg(arg.shortName); argsHash.insert(longName, arg); - if(shortName) { - if(!shortHash.contains(shortName)) shortHash.insert(shortName, argsHash.find(longName)); - else qWarning("Warning: shortName %c defined multiple times.", shortName); +} + +bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString &name, const QString &value) { + if(argsHash.contains(name)){ + if(type == CliParserArg::CliArgOption && argsHash.value(name).type == type) { + argsHash[name].value = value; + return true; + } + else if (type == CliParserArg::CliArgSwitch && argsHash.value(name).type == type) { + argsHash[name].boolValue = true; + return true; + } } + return false; } -void CliParser::addOption(const QString longName, const char shortName, const QString help, const QString def) { - CliParserArg arg = CliParserArg(CliParserArg::CliArgOption, shortName, help, def); - argsHash.insert(longName, arg); - if(shortName) { - if(!shortHash.contains(shortName)) shortHash.insert(shortName, argsHash.find(longName)); - else qWarning("Warning: shortName %c defined multiple times.", shortName); +bool CliParser::addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString &value) { + QString longName = lnameOfShortArg(shortName); + if(!longName.isNull()) { + if(type == CliParserArg::CliArgOption && argsHash.value(longName).type == type) { + argsHash[longName].value = value; + return true; + } + else if (type == CliParserArg::CliArgSwitch) { + if(argsHash.value(longName).type == type) { + argsHash[longName].boolValue = true; + return true; + } + // arg is an option but detected as a switch -> argument is missing + else { + qWarning().nospace() << "Warning: '" << shortName << "' is an option which needs an argument"; + return false; + } + } } + return false; } bool CliParser::parse() { QStringList::const_iterator currentArg; for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) { if(currentArg->startsWith("--")) { - QString name; // long + QString name; if(currentArg->contains("=")) { // option QStringList tmp = currentArg->mid(2).split("="); name = tmp.at(0); - QString value = tmp.at(1); - if(argsHash.contains(name) && !value.isEmpty()){ - argsHash[name].value = value; - } - else return false; + QString value; + // this is needed to allow --option="" + if(tmp.at(1).isNull()) value = QString(""); + else value = tmp.at(1); + if(!addLongArg(CliParserArg::CliArgOption, name, value)) return false; } else { // switch name = currentArg->mid(2); - if(argsHash.contains(name)) { - argsHash[name].boolValue = true; - } - else return false; + if(!addLongArg(CliParserArg::CliArgSwitch, name)) return false; } } else if(currentArg->startsWith("-")) { - char name; - // short - bool bla = true; - if(++currentArg == argsRaw.constEnd()) { - --currentArg; - bla = false; - } + // short + char name; + QStringList::const_iterator nextArg = currentArg+1; // if next arg is a short/long option/switch the current arg is one too - if(currentArg->startsWith("-")) { + if(nextArg == argsRaw.constEnd() || nextArg->startsWith("-")) { // switch - if(bla) --currentArg; for (int i = 0; i < currentArg->mid(1).toAscii().size(); i++) { name = currentArg->mid(1).toAscii().at(i); - if(shortHash.contains(name) && shortHash.value(name).value().type == CliParserArg::CliArgSwitch) { - shortHash[name].value().boolValue = true; - } - else return false; + if(!addShortArg(CliParserArg::CliArgSwitch, name)) return false; } } // if next arg is is no option/switch it's an argument to a shortoption else { // option - QString value = currentArg->toLocal8Bit(); - if(bla) --currentArg; - name = currentArg->mid(1).toAscii().at(0); - if(bla) currentArg++; - if(shortHash.contains(name) && shortHash.value(name).value().type == CliParserArg::CliArgOption) { - shortHash[name].value().value = value; + // short options are not freely mixable with other shortargs + if(currentArg->mid(1).toAscii().size() > 1) { + qWarning() << "Warning: Shortoptions may not be combined with other shortoptions or switches"; + return false; + } + QString value; + bool skipNext = false; + if(nextArg != argsRaw.constEnd()) { + value = nextArg->toLocal8Bit(); + skipNext = true; } - else return false; + else value = currentArg->toLocal8Bit(); + name = currentArg->mid(1).toAscii().at(0); + // we took one argument as argument to an option so skip it next time + if(skipNext) currentArg++; + if(!addShortArg(CliParserArg::CliArgOption, name, value)) return false; } } else { // we don't support plain arguments without -/-- - if(currentArg->toLatin1() != argsRaw.at(0)) { - return false; - } + if(currentArg->toLatin1() != argsRaw.at(0)) return false; } } return true; } void CliParser::usage() { - qWarning("Usage: %s [arguments]",QFileInfo(argsRaw.at(0)).completeBaseName().toLatin1().constData()); + qWarning() << "Usage:" << QFileInfo(argsRaw.at(0)).completeBaseName() << "[arguments]"; // get size of longName field QStringList keys = argsHash.keys(); @@ -151,7 +172,7 @@ void CliParser::usage() { if(!arg.value().help.isEmpty()) { output.append(arg.value().help); } - if(arg.value().type == CliParserArg::CliArgOption) { + if(arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) { output.append(". Default is: ").append(arg.value().def); } qWarning(output.toLatin1()); @@ -160,53 +181,32 @@ void CliParser::usage() { QString CliParser::value(const QString &longName) { if(argsHash.contains(longName) && argsHash.value(longName).type == CliParserArg::CliArgOption) { - if(!argsHash.value(longName).value.isEmpty()) + if(!argsHash.value(longName).value.isNull()) return argsHash.value(longName).value; else return argsHash.value(longName).def; } else { - qWarning("Warning: Requested value of not defined argument '%s' or argument is a switch",longName.toLatin1().constData()); + qWarning() << "Warning: Requested value of not defined argument" << longName << "or argument is a switch"; return QString(); } } bool CliParser::isSet(const QString &longName) { if(argsHash.contains(longName)) { - if(argsHash.value(longName).type == CliParserArg::CliArgOption) return !argsHash.value(longName).value.isEmpty(); + if(argsHash.value(longName).type == CliParserArg::CliArgOption) return !argsHash.value(longName).value.isNull(); else return argsHash.value(longName).boolValue; } else { - qWarning("Warning: Requested isSet of not defined argument '%s'",longName.toLatin1().constData()); + qWarning() << "Warning: Requested isSet of not defined argument" << longName; return false; } } -CliParserArg::CliParserArg(const CliArgType _type, const char _shortName, const QString _help, const QString _def) - : type(_type), - shortName(_shortName), - help(_help), - def(_def), - value(QString()), - boolValue(false) -{ -} - -CliParserArg::CliParserArg(const CliParserArg &other) { - type = other.type; - shortName = other.shortName; - help = other.help; - def = other.def; - value = other.value; - boolValue = other.boolValue; -} - -CliParserArg &CliParserArg::operator=(const CliParserArg &other) { - type = other.type; - shortName = other.shortName; - help = other.help; - def = other.def; - value = other.value; - boolValue = other.boolValue; - return *this; +QString CliParser::lnameOfShortArg(const char arg) { + QHash::const_iterator cur; + for (cur = argsHash.constBegin(); cur != argsHash.constEnd(); ++cur) { + if(cur.value().shortName == arg) return cur.key(); + } + return QString(); }