OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / libs / extensionsystem / optionsparser.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "optionsparser.h"
35
36 #include <QtCore/QCoreApplication>
37
38 using namespace ExtensionSystem;
39 using namespace ExtensionSystem::Internal;
40
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";
45
46 OptionsParser::OptionsParser(const QStringList &args,
47         const QMap<QString, bool> &appOptions,
48         QMap<QString, QString> *foundAppOptions,
49         QString *errorString,
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),
58       m_hasError(false)
59 {
60     ++m_it; // jump over program name
61     if (m_errorString)
62         m_errorString->clear();
63     if (m_foundAppOptions)
64         m_foundAppOptions->clear();
65     m_pmPrivate->arguments.clear();
66 }
67
68 bool OptionsParser::parse()
69 {
70     while (!m_hasError) {
71         if (!nextToken()) // move forward
72             break;
73         if (checkForEndOfOptions())
74             break;
75         if (checkForNoLoadOption())
76             continue;
77         if (checkForProfilingOption())
78             continue;
79         if (checkForTestOption())
80             continue;
81         if (checkForAppOption())
82             continue;
83         if (checkForPluginOption())
84             continue;
85         if (checkForUnknownOption())
86             break;
87         // probably a file or something
88         m_pmPrivate->arguments << m_currentArg;
89     }
90     if (m_isDependencyRefreshNeeded)
91         m_pmPrivate->resolveDependencies();
92     return !m_hasError;
93 }
94
95 bool OptionsParser::checkForEndOfOptions()
96 {
97     if (m_currentArg != QLatin1String(END_OF_OPTIONS))
98         return false;
99     while (nextToken()) {
100         m_pmPrivate->arguments << m_currentArg;
101     }
102     return true;
103 }
104
105 bool OptionsParser::checkForTestOption()
106 {
107     if (m_currentArg != QLatin1String(TEST_OPTION))
108         return false;
109     if (nextToken(RequiredToken)) {
110         PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
111         if (!spec) {
112             if (m_errorString)
113                 *m_errorString = QCoreApplication::translate("PluginManager",
114                                                              "The plugin '%1' does not exist.").arg(m_currentArg);
115             m_hasError = true;
116         } else {
117             m_pmPrivate->testSpecs.append(spec);
118         }
119     }
120     return true;
121 }
122
123 bool OptionsParser::checkForNoLoadOption()
124 {
125     if (m_currentArg != QLatin1String(NO_LOAD_OPTION))
126         return false;
127     if (nextToken(RequiredToken)) {
128         PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
129         if (!spec) {
130             if (m_errorString)
131                 *m_errorString = QCoreApplication::translate("PluginManager",
132                                                              "The plugin '%1' does not exist.").arg(m_currentArg);
133             m_hasError = true;
134         } else {
135             m_pmPrivate->disablePluginIndirectly(spec);
136             m_isDependencyRefreshNeeded = true;
137         }
138     }
139     return true;
140 }
141
142 bool OptionsParser::checkForAppOption()
143 {
144     if (!m_appOptions.contains(m_currentArg))
145         return false;
146     QString option = m_currentArg;
147     QString argument;
148     if (m_appOptions.value(m_currentArg) && nextToken(RequiredToken)) {
149         //argument required
150         argument = m_currentArg;
151     }
152     if (m_foundAppOptions)
153         m_foundAppOptions->insert(option, argument);
154     return true;
155 }
156
157 bool OptionsParser::checkForProfilingOption()
158 {
159     if (m_currentArg != QLatin1String(PROFILE_OPTION))
160         return false;
161     m_pmPrivate->initProfiling();
162     return true;
163 }
164
165 bool OptionsParser::checkForPluginOption()
166 {
167     bool requiresParameter;
168     PluginSpec *spec = m_pmPrivate->pluginForOption(m_currentArg, &requiresParameter);
169     if (!spec)
170         return false;
171     spec->addArgument(m_currentArg);
172     if (requiresParameter && nextToken(RequiredToken)) {
173         spec->addArgument(m_currentArg);
174     }
175     return true;
176 }
177
178 bool OptionsParser::checkForUnknownOption()
179 {
180     if (!m_currentArg.startsWith(QLatin1Char('-')))
181         return false;
182     if (m_errorString)
183         *m_errorString = QCoreApplication::translate("PluginManager",
184                                                      "Unknown option %1").arg(m_currentArg);
185     m_hasError = true;
186     return true;
187 }
188
189 bool OptionsParser::nextToken(OptionsParser::TokenType type)
190 {
191     if (m_it == m_end) {
192         if (type == OptionsParser::RequiredToken) {
193             m_hasError = true;
194             if (m_errorString)
195                 *m_errorString = QCoreApplication::translate("PluginManager",
196                                                              "The option %1 requires an argument.").arg(m_currentArg);
197         }
198         return false;
199     }
200     m_currentArg = *m_it;
201     ++m_it;
202     return true;
203 }