optionsparser.cpp 8.06 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

con's avatar
con committed
31 32
#include "optionsparser.h"

33
#include <QCoreApplication>
con's avatar
con committed
34 35 36 37

using namespace ExtensionSystem;
using namespace ExtensionSystem::Internal;

38
const char END_OF_OPTIONS[] = "--";
con's avatar
con committed
39
const char *OptionsParser::NO_LOAD_OPTION = "-noload";
40
const char *OptionsParser::LOAD_OPTION = "-load";
con's avatar
con committed
41
const char *OptionsParser::TEST_OPTION = "-test";
42
const char *OptionsParser::PROFILE_OPTION = "-profile";
con's avatar
con committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

OptionsParser::OptionsParser(const QStringList &args,
        const QMap<QString, bool> &appOptions,
        QMap<QString, QString> *foundAppOptions,
        QString *errorString,
        PluginManagerPrivate *pmPrivate)
    : m_args(args), m_appOptions(appOptions),
      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)
{
    ++m_it; // jump over program name
    if (m_errorString)
        m_errorString->clear();
    if (m_foundAppOptions)
        m_foundAppOptions->clear();
    m_pmPrivate->arguments.clear();
}

bool OptionsParser::parse()
{
    while (!m_hasError) {
        if (!nextToken()) // move forward
            break;
        if (checkForEndOfOptions())
            break;
73 74
        if (checkForLoadOption())
            continue;
con's avatar
con committed
75 76
        if (checkForNoLoadOption())
            continue;
77 78
        if (checkForProfilingOption())
            continue;
79
#ifdef WITH_TESTS
con's avatar
con committed
80 81
        if (checkForTestOption())
            continue;
82
#endif
con's avatar
con committed
83 84 85 86 87 88 89 90 91 92 93
        if (checkForAppOption())
            continue;
        if (checkForPluginOption())
            continue;
        if (checkForUnknownOption())
            break;
        // probably a file or something
        m_pmPrivate->arguments << m_currentArg;
    }
    if (m_isDependencyRefreshNeeded)
        m_pmPrivate->resolveDependencies();
94
    m_pmPrivate->enableOnlyTestedSpecs();
con's avatar
con committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    return !m_hasError;
}

bool OptionsParser::checkForEndOfOptions()
{
    if (m_currentArg != QLatin1String(END_OF_OPTIONS))
        return false;
    while (nextToken()) {
        m_pmPrivate->arguments << m_currentArg;
    }
    return true;
}

bool OptionsParser::checkForTestOption()
{
    if (m_currentArg != QLatin1String(TEST_OPTION))
        return false;
    if (nextToken(RequiredToken)) {
113
        if (m_currentArg == QLatin1String("all")) {
114
            foreach (PluginSpec *spec, m_pmPrivate->pluginSpecs) {
115 116
                if (spec && !m_pmPrivate->containsTestSpec(spec))
                    m_pmPrivate->testSpecs.append(PluginManagerPrivate::TestSpec(spec));
117
            }
con's avatar
con committed
118
        } else {
119 120 121 122 123 124
            QStringList args = m_currentArg.split(QLatin1Char(','));
            const QString pluginName = args.takeFirst();
            if (PluginSpec *spec = m_pmPrivate->pluginByName(pluginName)) {
                if (m_pmPrivate->containsTestSpec(spec)) {
                    if (m_errorString)
                        *m_errorString = QCoreApplication::translate("PluginManager",
125
                                                                     "The plugin \"%1\" is specified twice for testing.").arg(pluginName);
126 127 128 129 130
                    m_hasError = true;
                } else {
                    m_pmPrivate->testSpecs.append(PluginManagerPrivate::TestSpec(spec, args));
                }
            } else  {
131 132
                if (m_errorString)
                    *m_errorString = QCoreApplication::translate("PluginManager",
133
                                                                 "The plugin \"%1\" does not exist.").arg(pluginName);
134 135
                m_hasError = true;
            }
con's avatar
con committed
136 137 138 139 140
        }
    }
    return true;
}

141 142 143 144 145 146 147 148 149
bool OptionsParser::checkForLoadOption()
{
    if (m_currentArg != QLatin1String(LOAD_OPTION))
        return false;
    if (nextToken(RequiredToken)) {
        PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
        if (!spec) {
            if (m_errorString)
                *m_errorString = QCoreApplication::translate("PluginManager",
150
                                                             "The plugin \"%1\" does not exist.")
151 152 153 154 155 156 157 158 159 160
                    .arg(m_currentArg);
            m_hasError = true;
        } else {
            spec->setForceEnabled(true);
            m_isDependencyRefreshNeeded = true;
        }
    }
    return true;
}

con's avatar
con committed
161 162 163 164 165 166 167 168 169
bool OptionsParser::checkForNoLoadOption()
{
    if (m_currentArg != QLatin1String(NO_LOAD_OPTION))
        return false;
    if (nextToken(RequiredToken)) {
        PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
        if (!spec) {
            if (m_errorString)
                *m_errorString = QCoreApplication::translate("PluginManager",
170
                                                             "The plugin \"%1\" does not exist.").arg(m_currentArg);
con's avatar
con committed
171 172
            m_hasError = true;
        } else {
173
            spec->setForceDisabled(true);
con's avatar
con committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
            m_isDependencyRefreshNeeded = true;
        }
    }
    return true;
}

bool OptionsParser::checkForAppOption()
{
    if (!m_appOptions.contains(m_currentArg))
        return false;
    QString option = m_currentArg;
    QString argument;
    if (m_appOptions.value(m_currentArg) && nextToken(RequiredToken)) {
        //argument required
        argument = m_currentArg;
    }
    if (m_foundAppOptions)
        m_foundAppOptions->insert(option, argument);
    return true;
}

195 196 197 198 199 200 201 202
bool OptionsParser::checkForProfilingOption()
{
    if (m_currentArg != QLatin1String(PROFILE_OPTION))
        return false;
    m_pmPrivate->initProfiling();
    return true;
}

con's avatar
con committed
203 204 205 206 207 208 209
bool OptionsParser::checkForPluginOption()
{
    bool requiresParameter;
    PluginSpec *spec = m_pmPrivate->pluginForOption(m_currentArg, &requiresParameter);
    if (!spec)
        return false;
    spec->addArgument(m_currentArg);
210
    if (requiresParameter && nextToken(RequiredToken))
con's avatar
con committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        spec->addArgument(m_currentArg);
    return true;
}

bool OptionsParser::checkForUnknownOption()
{
    if (!m_currentArg.startsWith(QLatin1Char('-')))
        return false;
    if (m_errorString)
        *m_errorString = QCoreApplication::translate("PluginManager",
                                                     "Unknown option %1").arg(m_currentArg);
    m_hasError = true;
    return true;
}

bool OptionsParser::nextToken(OptionsParser::TokenType type)
{
    if (m_it == m_end) {
        if (type == OptionsParser::RequiredToken) {
            m_hasError = true;
            if (m_errorString)
                *m_errorString = QCoreApplication::translate("PluginManager",
                                                             "The option %1 requires an argument.").arg(m_currentArg);
        }
        return false;
    }
    m_currentArg = *m_it;
    ++m_it;
    return true;
}