1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "optionsparser.h"
36 #include <QtCore/QCoreApplication>
38 using namespace ExtensionSystem;
39 using namespace ExtensionSystem::Internal;
41 static const char END_OF_OPTIONS[] = "--";
42 const char *OptionsParser::NO_LOAD_OPTION = "-noload";
43 const char *OptionsParser::TEST_OPTION = "-test";
44 const char *OptionsParser::PROFILE_OPTION = "-profile";
46 OptionsParser::OptionsParser(const QStringList &args,
47 const QMap<QString, bool> &appOptions,
48 QMap<QString, QString> *foundAppOptions,
50 PluginManagerPrivate *pmPrivate)
51 : m_args(args), m_appOptions(appOptions),
52 m_foundAppOptions(foundAppOptions),
53 m_errorString(errorString),
54 m_pmPrivate(pmPrivate),
55 m_it(m_args.constBegin()),
56 m_end(m_args.constEnd()),
57 m_isDependencyRefreshNeeded(false),
60 ++m_it; // jump over program name
62 m_errorString->clear();
63 if (m_foundAppOptions)
64 m_foundAppOptions->clear();
65 m_pmPrivate->arguments.clear();
68 bool OptionsParser::parse()
71 if (!nextToken()) // move forward
73 if (checkForEndOfOptions())
75 if (checkForNoLoadOption())
77 if (checkForProfilingOption())
79 if (checkForTestOption())
81 if (checkForAppOption())
83 if (checkForPluginOption())
85 if (checkForUnknownOption())
87 // probably a file or something
88 m_pmPrivate->arguments << m_currentArg;
90 if (m_isDependencyRefreshNeeded)
91 m_pmPrivate->resolveDependencies();
95 bool OptionsParser::checkForEndOfOptions()
97 if (m_currentArg != QLatin1String(END_OF_OPTIONS))
100 m_pmPrivate->arguments << m_currentArg;
105 bool OptionsParser::checkForTestOption()
107 if (m_currentArg != QLatin1String(TEST_OPTION))
109 if (nextToken(RequiredToken)) {
110 PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
113 *m_errorString = QCoreApplication::translate("PluginManager",
114 "The plugin '%1' does not exist.").arg(m_currentArg);
117 m_pmPrivate->testSpecs.append(spec);
123 bool OptionsParser::checkForNoLoadOption()
125 if (m_currentArg != QLatin1String(NO_LOAD_OPTION))
127 if (nextToken(RequiredToken)) {
128 PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
131 *m_errorString = QCoreApplication::translate("PluginManager",
132 "The plugin '%1' does not exist.").arg(m_currentArg);
135 m_pmPrivate->disablePluginIndirectly(spec);
136 m_isDependencyRefreshNeeded = true;
142 bool OptionsParser::checkForAppOption()
144 if (!m_appOptions.contains(m_currentArg))
146 QString option = m_currentArg;
148 if (m_appOptions.value(m_currentArg) && nextToken(RequiredToken)) {
150 argument = m_currentArg;
152 if (m_foundAppOptions)
153 m_foundAppOptions->insert(option, argument);
157 bool OptionsParser::checkForProfilingOption()
159 if (m_currentArg != QLatin1String(PROFILE_OPTION))
161 m_pmPrivate->initProfiling();
165 bool OptionsParser::checkForPluginOption()
167 bool requiresParameter;
168 PluginSpec *spec = m_pmPrivate->pluginForOption(m_currentArg, &requiresParameter);
171 spec->addArgument(m_currentArg);
172 if (requiresParameter && nextToken(RequiredToken)) {
173 spec->addArgument(m_currentArg);
178 bool OptionsParser::checkForUnknownOption()
180 if (!m_currentArg.startsWith(QLatin1Char('-')))
183 *m_errorString = QCoreApplication::translate("PluginManager",
184 "Unknown option %1").arg(m_currentArg);
189 bool OptionsParser::nextToken(OptionsParser::TokenType type)
192 if (type == OptionsParser::RequiredToken) {
195 *m_errorString = QCoreApplication::translate("PluginManager",
196 "The option %1 requires an argument.").arg(m_currentArg);
200 m_currentArg = *m_it;