2010-03-10 22:36:47 +00:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file optionsparser.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
|
2013-05-19 17:37:30 +03:00
|
|
|
* @brief
|
2010-03-10 22:36:47 +00:00
|
|
|
* @see The GNU Public License (GPL) Version 3
|
2013-05-19 17:37:30 +03:00
|
|
|
* @defgroup
|
2010-03-10 22:36:47 +00:00
|
|
|
* @{
|
2013-05-19 17:37:30 +03:00
|
|
|
*
|
2010-03-10 22:36:47 +00:00
|
|
|
*****************************************************************************/
|
2013-05-19 17:37:30 +03:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2010-03-10 22:36:47 +00:00
|
|
|
* (at your option) any later version.
|
2013-05-19 17:37:30 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2010-03-10 22:36:47 +00:00
|
|
|
* for more details.
|
2013-05-19 17:37:30 +03:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-03-10 22:36:47 +00:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
#include "optionsparser.h"
|
|
|
|
|
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
|
|
|
|
using namespace ExtensionSystem;
|
|
|
|
using namespace ExtensionSystem::Internal;
|
|
|
|
|
|
|
|
static const char *END_OF_OPTIONS = "--";
|
|
|
|
const char *OptionsParser::NO_LOAD_OPTION = "-noload";
|
2013-05-19 17:37:30 +03:00
|
|
|
const char *OptionsParser::TEST_OPTION = "-test";
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
OptionsParser::OptionsParser(const QStringList &args,
|
2013-05-19 17:37:30 +03:00
|
|
|
const QMap<QString, bool> &appOptions,
|
|
|
|
QMap<QString, QString> *foundAppOptions,
|
|
|
|
QString *errorString,
|
|
|
|
PluginManagerPrivate *pmPrivate)
|
2010-02-01 14:10:06 +00:00
|
|
|
: m_args(args), m_appOptions(appOptions),
|
2013-05-19 17:37:30 +03:00
|
|
|
m_foundAppOptions(foundAppOptions),
|
|
|
|
m_errorString(errorString),
|
|
|
|
m_pmPrivate(pmPrivate),
|
|
|
|
m_it(m_args.constBegin()),
|
|
|
|
m_end(m_args.constEnd()),
|
|
|
|
m_isDependencyRefreshNeeded(false),
|
|
|
|
m_hasError(false)
|
2010-02-01 14:10:06 +00:00
|
|
|
{
|
|
|
|
++m_it; // jump over program name
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_errorString) {
|
2010-02-01 14:10:06 +00:00
|
|
|
m_errorString->clear();
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
if (m_foundAppOptions) {
|
2010-02-01 14:10:06 +00:00
|
|
|
m_foundAppOptions->clear();
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
m_pmPrivate->arguments.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionsParser::parse()
|
|
|
|
{
|
|
|
|
while (!m_hasError) {
|
2013-05-19 17:37:30 +03:00
|
|
|
if (!nextToken()) { // move forward
|
2010-02-01 14:10:06 +00:00
|
|
|
break;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
if (checkForEndOfOptions()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
break;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
if (checkForNoLoadOption()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
continue;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
if (checkForTestOption()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
continue;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
if (checkForAppOption()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
continue;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
if (checkForPluginOption()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
continue;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
if (checkForUnknownOption()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
break;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
// probably a file or something
|
|
|
|
m_pmPrivate->arguments << m_currentArg;
|
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_isDependencyRefreshNeeded) {
|
2010-02-01 14:10:06 +00:00
|
|
|
m_pmPrivate->resolveDependencies();
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
return !m_hasError;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionsParser::checkForEndOfOptions()
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_currentArg != QLatin1String(END_OF_OPTIONS)) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return false;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
while (nextToken()) {
|
|
|
|
m_pmPrivate->arguments << m_currentArg;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionsParser::checkForTestOption()
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_currentArg != QLatin1String(TEST_OPTION)) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return false;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
if (nextToken(RequiredToken)) {
|
|
|
|
PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
|
|
|
|
if (!spec) {
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_errorString) {
|
2010-02-01 14:10:06 +00:00
|
|
|
*m_errorString = QCoreApplication::translate("PluginManager",
|
|
|
|
"The plugin '%1' does not exist.").arg(m_currentArg);
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
m_hasError = true;
|
|
|
|
} else {
|
|
|
|
m_pmPrivate->testSpecs.append(spec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionsParser::checkForNoLoadOption()
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_currentArg != QLatin1String(NO_LOAD_OPTION)) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return false;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
if (nextToken(RequiredToken)) {
|
|
|
|
PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
|
|
|
|
if (!spec) {
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_errorString) {
|
2010-02-01 14:10:06 +00:00
|
|
|
*m_errorString = QCoreApplication::translate("PluginManager",
|
|
|
|
"The plugin '%1' does not exist.").arg(m_currentArg);
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
m_hasError = true;
|
|
|
|
} else {
|
|
|
|
m_pmPrivate->pluginSpecs.removeAll(spec);
|
|
|
|
delete spec;
|
|
|
|
m_isDependencyRefreshNeeded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionsParser::checkForAppOption()
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
if (!m_appOptions.contains(m_currentArg)) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return false;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
QString option = m_currentArg;
|
|
|
|
QString argument;
|
|
|
|
if (m_appOptions.value(m_currentArg) && nextToken(RequiredToken)) {
|
2013-05-19 17:37:30 +03:00
|
|
|
// argument required
|
2010-02-01 14:10:06 +00:00
|
|
|
argument = m_currentArg;
|
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_foundAppOptions) {
|
2010-02-01 14:10:06 +00:00
|
|
|
m_foundAppOptions->insert(option, argument);
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionsParser::checkForPluginOption()
|
|
|
|
{
|
|
|
|
bool requiresParameter;
|
|
|
|
PluginSpec *spec = m_pmPrivate->pluginForOption(m_currentArg, &requiresParameter);
|
2013-05-19 17:37:30 +03:00
|
|
|
|
|
|
|
if (!spec) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return false;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
spec->addArgument(m_currentArg);
|
|
|
|
if (requiresParameter && nextToken(RequiredToken)) {
|
|
|
|
spec->addArgument(m_currentArg);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionsParser::checkForUnknownOption()
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
if (!m_currentArg.startsWith(QLatin1Char('-'))) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return false;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
if (m_errorString) {
|
2010-02-01 14:10:06 +00:00
|
|
|
*m_errorString = QCoreApplication::translate("PluginManager",
|
|
|
|
"Unknown option %1").arg(m_currentArg);
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
m_hasError = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionsParser::nextToken(OptionsParser::TokenType type)
|
|
|
|
{
|
|
|
|
if (m_it == m_end) {
|
|
|
|
if (type == OptionsParser::RequiredToken) {
|
|
|
|
m_hasError = true;
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_errorString) {
|
2010-02-01 14:10:06 +00:00
|
|
|
*m_errorString = QCoreApplication::translate("PluginManager",
|
|
|
|
"The option %1 requires an argument.").arg(m_currentArg);
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_currentArg = *m_it;
|
|
|
|
++m_it;
|
|
|
|
return true;
|
|
|
|
}
|