X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fcliparser.cpp;h=059ef35011bc7ab019b63fffc789c3db68f7c2be;hp=c433f26904e5d5fc8733789b620b78730d620ae2;hb=5cab348de53cb3b994273c06fe69e1f799d247b4;hpb=3244e636b430b415aa76ba1885798d41627f7cfb diff --git a/src/common/cliparser.cpp b/src/common/cliparser.cpp index c433f269..059ef350 100644 --- a/src/common/cliparser.cpp +++ b/src/common/cliparser.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel IRC Team * + * Copyright (C) 2005-09 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -19,31 +19,30 @@ ***************************************************************************/ #include "cliparser.h" +#include +#include #include #include -CliParser::CliParser(QStringList arguments) -{ - argsRaw = arguments; -// remove Qt internal debugging arguments - argsRaw.removeOne("-sync"); - argsRaw.removeOne("-nograb"); - argsRaw.removeOne("-dograb"); +#include + +CliParser::CliParser() : AbstractCliParser() { + } -void CliParser::addArgument(const CliParserArg::CliArgType type, const QString longName, const char shortName, const QString help, const QString def) { - CliParserArg arg; - if(type == CliParserArg::CliArgOption) - arg = CliParserArg(CliParserArg::CliArgOption, shortName, help, def); - else - arg = CliParserArg(CliParserArg::CliArgSwitch, shortName, help); +void CliParser::addArgument(const QString &longName_, const CliParserArg &arg) { + QString longName = longName_; + longName.remove(QRegExp("\\s*<.*>\\s*")); // KCmdLineArgs takes args of the form "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); } -bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString name, const QString value) { +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; + argsHash[name].value = escapedValue(value); return true; } else if (type == CliParserArg::CliArgSwitch && argsHash.value(name).type == type) { @@ -51,25 +50,43 @@ bool CliParser::addLongArg(const CliParserArg::CliArgType type, const QString na return true; } } + qWarning() << "Warning: Unrecognized argument:" << name; return false; } -bool CliParser::addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString value) { +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; + argsHash[longName].value = escapedValue(value); return true; } - else if (type == CliParserArg::CliArgSwitch && argsHash.value(longName).type == type) { - argsHash[longName].boolValue = true; - 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; + } } } + qWarning().nospace() << "Warning: Unrecognized argument: '" << shortName << "'"; return false; } -bool CliParser::parse() { +QString CliParser::escapedValue(const QString &value) { + QString escapedValue = value; + if(escapedValue.startsWith("~")) + escapedValue.replace(0, 1, QDir::homePath()); + + return escapedValue; +} + +bool CliParser::init(const QStringList &args) { + argsRaw = args; QStringList::const_iterator currentArg; for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) { if(currentArg->startsWith("--")) { @@ -106,6 +123,11 @@ bool CliParser::parse() { // if next arg is is no option/switch it's an argument to a shortoption else { // option + // 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()) { @@ -128,8 +150,8 @@ bool CliParser::parse() { } void CliParser::usage() { - qWarning("Usage: %s [arguments]",QFileInfo(argsRaw.at(0)).completeBaseName().toLatin1().constData()); - + std::cout << "Usage: " << qPrintable(QFileInfo(argsRaw.at(0)).completeBaseName()) << " [arguments]" << std::endl; + // get size of longName field QStringList keys = argsHash.keys(); uint lnameFieldSize = 0; @@ -143,12 +165,12 @@ void CliParser::usage() { size += 8; if(size > lnameFieldSize) lnameFieldSize = size; } - + QHash::const_iterator arg; for(arg = argsHash.constBegin(); arg != argsHash.constEnd(); ++arg) { QString output; QString lnameField; - + if(arg.value().shortName) { output.append(" -").append(arg.value().shortName).append(","); } @@ -164,7 +186,7 @@ void CliParser::usage() { if(arg.value().type == CliParserArg::CliArgOption && !arg.value().def.isNull()) { output.append(". Default is: ").append(arg.value().def); } - qWarning(output.toLatin1()); + std::cout << qPrintable(output) << std::endl; } } @@ -176,7 +198,7 @@ QString CliParser::value(const QString &longName) { 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(); } } @@ -187,7 +209,7 @@ bool CliParser::isSet(const QString &longName) { 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; } }