2 ******************************************************************************
4 * @file optionsparser.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "optionsparser.h"
31 #include <QtCore/QCoreApplication>
33 using namespace ExtensionSystem
;
34 using namespace ExtensionSystem::Internal
;
36 static const char *END_OF_OPTIONS
= "--";
37 const char *OptionsParser::NO_LOAD_OPTION
= "-noload";
38 const char *OptionsParser::TEST_OPTION
= "-test";
40 OptionsParser::OptionsParser(const QStringList
&args
,
41 const QMap
<QString
, bool> &appOptions
,
42 QMap
<QString
, QString
> *foundAppOptions
,
44 PluginManagerPrivate
*pmPrivate
)
45 : m_args(args
), m_appOptions(appOptions
),
46 m_foundAppOptions(foundAppOptions
),
47 m_errorString(errorString
),
48 m_pmPrivate(pmPrivate
),
49 m_it(m_args
.constBegin()),
50 m_end(m_args
.constEnd()),
51 m_isDependencyRefreshNeeded(false),
54 ++m_it
; // jump over program name
56 m_errorString
->clear();
58 if (m_foundAppOptions
) {
59 m_foundAppOptions
->clear();
61 m_pmPrivate
->arguments
.clear();
64 bool OptionsParser::parse()
67 if (!nextToken()) { // move forward
70 if (checkForEndOfOptions()) {
73 if (checkForNoLoadOption()) {
76 if (checkForTestOption()) {
79 if (checkForAppOption()) {
82 if (checkForPluginOption()) {
85 if (checkForUnknownOption()) {
88 // probably a file or something
89 m_pmPrivate
->arguments
<< m_currentArg
;
91 if (m_isDependencyRefreshNeeded
) {
92 m_pmPrivate
->resolveDependencies();
97 bool OptionsParser::checkForEndOfOptions()
99 if (m_currentArg
!= QLatin1String(END_OF_OPTIONS
)) {
102 while (nextToken()) {
103 m_pmPrivate
->arguments
<< m_currentArg
;
108 bool OptionsParser::checkForTestOption()
110 if (m_currentArg
!= QLatin1String(TEST_OPTION
)) {
113 if (nextToken(RequiredToken
)) {
114 PluginSpec
*spec
= m_pmPrivate
->pluginByName(m_currentArg
);
117 *m_errorString
= QCoreApplication::translate("PluginManager",
118 "The plugin '%1' does not exist.").arg(m_currentArg
);
122 m_pmPrivate
->testSpecs
.append(spec
);
128 bool OptionsParser::checkForNoLoadOption()
130 if (m_currentArg
!= QLatin1String(NO_LOAD_OPTION
)) {
133 if (nextToken(RequiredToken
)) {
134 PluginSpec
*spec
= m_pmPrivate
->pluginByName(m_currentArg
);
137 *m_errorString
= QCoreApplication::translate("PluginManager",
138 "The plugin '%1' does not exist.").arg(m_currentArg
);
142 m_pmPrivate
->pluginSpecs
.removeAll(spec
);
144 m_isDependencyRefreshNeeded
= true;
150 bool OptionsParser::checkForAppOption()
152 if (!m_appOptions
.contains(m_currentArg
)) {
155 QString option
= m_currentArg
;
157 if (m_appOptions
.value(m_currentArg
) && nextToken(RequiredToken
)) {
159 argument
= m_currentArg
;
161 if (m_foundAppOptions
) {
162 m_foundAppOptions
->insert(option
, argument
);
167 bool OptionsParser::checkForPluginOption()
169 bool requiresParameter
;
170 PluginSpec
*spec
= m_pmPrivate
->pluginForOption(m_currentArg
, &requiresParameter
);
175 spec
->addArgument(m_currentArg
);
176 if (requiresParameter
&& nextToken(RequiredToken
)) {
177 spec
->addArgument(m_currentArg
);
182 bool OptionsParser::checkForUnknownOption()
184 if (!m_currentArg
.startsWith(QLatin1Char('-'))) {
188 *m_errorString
= QCoreApplication::translate("PluginManager",
189 "Unknown option %1").arg(m_currentArg
);
195 bool OptionsParser::nextToken(OptionsParser::TokenType type
)
198 if (type
== OptionsParser::RequiredToken
) {
201 *m_errorString
= QCoreApplication::translate("PluginManager",
202 "The option %1 requires an argument.").arg(m_currentArg
);
207 m_currentArg
= *m_it
;