App: Pull special command line parsing into separate function

Change-Id: I143aa3f49b3e9fb5a1aa30cf9c9364c61b95ced4
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Eike Ziller
2018-06-06 10:32:39 +02:00
parent cbd4d05423
commit 93d35399b5

View File

@@ -337,6 +337,43 @@ void loadFonts()
QFontDatabase::addApplicationFont(fileInfo.absoluteFilePath()); QFontDatabase::addApplicationFont(fileInfo.absoluteFilePath());
} }
struct Options
{
QString settingsPath;
QString installSettingsPath;
QStringList customPluginPaths;
std::vector<char *> appArguments;
bool hasTestOption = false;
};
Options parseCommandLine(int argc, char *argv[])
{
Options options;
auto it = argv;
const auto end = argv + argc;
while (it != end) {
const auto arg = QString::fromLocal8Bit(*it);
const bool hasNext = it + 1 != end;
if (arg == SETTINGS_OPTION && hasNext) {
++it;
options.settingsPath = QDir::fromNativeSeparators(QString::fromLocal8Bit(*it));
} else if (arg == INSTALL_SETTINGS_OPTION && hasNext) {
++it;
options.installSettingsPath = QDir::fromNativeSeparators(QString::fromLocal8Bit(*it));
} else if (arg == PLUGINPATH_OPTION && hasNext) {
++it;
options.customPluginPaths += QDir::fromNativeSeparators(QString::fromLocal8Bit(*it));
} else { // arguments that are still passed on to the application
if (arg == TEST_OPTION)
options.hasTestOption = true;
options.appArguments.push_back(*it);
}
++it;
}
return options;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
@@ -362,68 +399,40 @@ int main(int argc, char **argv)
setrlimit(RLIMIT_NOFILE, &rl); setrlimit(RLIMIT_NOFILE, &rl);
#endif #endif
// Manually determine -settingspath and -installsettingspath command line options // Manually determine various command line options
// We can't use the regular way of the plugin manager, because that needs to parse plugin meta data // We can't use the regular way of the plugin manager,
// but the settings path can influence which plugins are enabled // because settings can change the way plugin manager behaves
QString settingsPath; Options options = parseCommandLine(argc, argv);
QString installSettingsPath;
QStringList customPluginPaths;
std::vector<char *> appArguments;
bool hasTestOption = false;
auto it = argv;
const auto end = argv + argc;
while (it != end) {
const auto arg = QString::fromLocal8Bit(*it);
const bool hasNext = it + 1 != end;
if (arg == SETTINGS_OPTION && hasNext) {
++it;
settingsPath = QDir::fromNativeSeparators(QString::fromLocal8Bit(*it));
} else if (arg == INSTALL_SETTINGS_OPTION && hasNext) {
++it;
installSettingsPath = QDir::fromNativeSeparators(QString::fromLocal8Bit(*it));
} else if (arg == PLUGINPATH_OPTION && hasNext) {
++it;
customPluginPaths += QDir::fromNativeSeparators(QString::fromLocal8Bit(*it));
} else {
if (arg == TEST_OPTION)
hasTestOption = true;
appArguments.push_back(*it);
}
++it;
}
applicationDirPath(argv[0]); applicationDirPath(argv[0]);
QScopedPointer<Utils::TemporaryDirectory> temporaryCleanSettingsDir; QScopedPointer<Utils::TemporaryDirectory> temporaryCleanSettingsDir;
if (settingsPath.isEmpty() && hasTestOption) { if (options.settingsPath.isEmpty() && options.hasTestOption) {
temporaryCleanSettingsDir.reset(new Utils::TemporaryDirectory("qtc-test-settings")); temporaryCleanSettingsDir.reset(new Utils::TemporaryDirectory("qtc-test-settings"));
if (!temporaryCleanSettingsDir->isValid()) if (!temporaryCleanSettingsDir->isValid())
return 1; return 1;
settingsPath = temporaryCleanSettingsDir->path(); options.settingsPath = temporaryCleanSettingsDir->path();
} }
if (!settingsPath.isEmpty()) if (!options.settingsPath.isEmpty())
QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, settingsPath); QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, options.settingsPath);
// Must be done before any QSettings class is created // Must be done before any QSettings class is created
QSettings::setDefaultFormat(QSettings::IniFormat); QSettings::setDefaultFormat(QSettings::IniFormat);
setupInstallSettings(installSettingsPath); setupInstallSettings(options.installSettingsPath);
// plugin manager takes control of this settings object // plugin manager takes control of this settings object
setHighDpiEnvironmentVariable(); setHighDpiEnvironmentVariable();
SharedTools::QtSingleApplication::setAttribute(Qt::AA_ShareOpenGLContexts); SharedTools::QtSingleApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
int numberofArguments = static_cast<int>(appArguments.size()); int numberofArguments = static_cast<int>(options.appArguments.size());
SharedTools::QtSingleApplication app((QLatin1String(Core::Constants::IDE_DISPLAY_NAME)), SharedTools::QtSingleApplication app((QLatin1String(Core::Constants::IDE_DISPLAY_NAME)),
numberofArguments, numberofArguments,
appArguments.data()); options.appArguments.data());
const QStringList pluginArguments = app.arguments(); const QStringList pluginArguments = app.arguments();
/*Initialize global settings and resetup install settings with QApplication::applicationDirPath */ /*Initialize global settings and resetup install settings with QApplication::applicationDirPath */
setupInstallSettings(installSettingsPath); setupInstallSettings(options.installSettingsPath);
QSettings *settings = userSettings(); QSettings *settings = userSettings();
QSettings *globalSettings = new QSettings(QSettings::IniFormat, QSettings::SystemScope, QSettings *globalSettings = new QSettings(QSettings::IniFormat, QSettings::SystemScope,
QLatin1String(Core::Constants::IDE_SETTINGSVARIANT_STR), QLatin1String(Core::Constants::IDE_SETTINGSVARIANT_STR),
@@ -490,7 +499,7 @@ int main(int argc, char **argv)
QNetworkProxyFactory::setUseSystemConfiguration(true); QNetworkProxyFactory::setUseSystemConfiguration(true);
// Load // Load
const QStringList pluginPaths = getPluginPaths() + customPluginPaths; const QStringList pluginPaths = getPluginPaths() + options.customPluginPaths;
PluginManager::setPluginPaths(pluginPaths); PluginManager::setPluginPaths(pluginPaths);
QMap<QString, QString> foundAppOptions; QMap<QString, QString> foundAppOptions;
if (pluginArguments.size() > 1) { if (pluginArguments.size() > 1) {