From 9e84603ca4e22f94cbb233aec547e146a669b3c4 Mon Sep 17 00:00:00 2001 From: Clement Date: Wed, 25 Jul 2018 16:06:52 -0700 Subject: [PATCH] Make proper -v and -h options for interface --- assignment-client/src/AssignmentClientApp.cpp | 12 +++- domain-server/src/DomainServer.cpp | 59 ++++++++++------- domain-server/src/DomainServer.h | 14 ++-- domain-server/src/main.cpp | 2 + interface/src/main.cpp | 66 ++++++++++++------- 5 files changed, 97 insertions(+), 56 deletions(-) diff --git a/assignment-client/src/AssignmentClientApp.cpp b/assignment-client/src/AssignmentClientApp.cpp index b37784cddc..acfbb8571c 100644 --- a/assignment-client/src/AssignmentClientApp.cpp +++ b/assignment-client/src/AssignmentClientApp.cpp @@ -11,6 +11,8 @@ #include "AssignmentClientApp.h" +#include + #include #include #include @@ -42,9 +44,8 @@ AssignmentClientApp::AssignmentClientApp(int argc, char* argv[]) : // parse command-line QCommandLineParser parser; parser.setApplicationDescription("High Fidelity Assignment Client"); - parser.addHelpOption(); - const QCommandLineOption helpOption = parser.addHelpOption(); + const QCommandLineOption versionOption = parser.addVersionOption(); QString typeDescription = "run single assignment client of given type\n# | Type\n============================"; for (Assignment::Type type = Assignment::FirstType; @@ -97,11 +98,16 @@ AssignmentClientApp::AssignmentClientApp(int argc, char* argv[]) : parser.addOption(parentPIDOption); if (!parser.parse(QCoreApplication::arguments())) { - qCritical() << parser.errorText() << endl; + std::cout << parser.errorText().toStdString() << std::endl; // Avoid Qt log spam parser.showHelp(); Q_UNREACHABLE(); } + if (parser.isSet(versionOption)) { + parser.showVersion(); + Q_UNREACHABLE(); + } + if (parser.isSet(helpOption)) { parser.showHelp(); Q_UNREACHABLE(); diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 86a9a58058..be842016d0 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -69,6 +70,12 @@ const QString ICE_SERVER_DEFAULT_HOSTNAME = "ice.highfidelity.com"; const QString ICE_SERVER_DEFAULT_HOSTNAME = "dev-ice.highfidelity.com"; #endif +QString DomainServer::_iceServerAddr { ICE_SERVER_DEFAULT_HOSTNAME }; +int DomainServer::_iceServerPort { ICE_SERVER_DEFAULT_PORT }; +bool DomainServer::_overrideDomainID { false }; +QUuid DomainServer::_overridingDomainID; +int DomainServer::_parentPID { -1 }; + bool DomainServer::forwardMetaverseAPIRequest(HTTPConnection* connection, const QString& metaversePath, const QString& requestSubobjectKey, @@ -148,24 +155,13 @@ bool DomainServer::forwardMetaverseAPIRequest(HTTPConnection* connection, DomainServer::DomainServer(int argc, char* argv[]) : QCoreApplication(argc, argv), _gatekeeper(this), - _httpManager(QHostAddress::AnyIPv4, DOMAIN_SERVER_HTTP_PORT, QString("%1/resources/web/").arg(QCoreApplication::applicationDirPath()), this), - _allAssignments(), - _unfulfilledAssignments(), - _isUsingDTLS(false), - _oauthProviderURL(), - _oauthClientID(), - _hostname(), - _ephemeralACScripts(), - _webAuthenticationStateSet(), - _cookieSessionHash(), - _automaticNetworkingSetting(), - _settingsManager(), - _iceServerAddr(ICE_SERVER_DEFAULT_HOSTNAME), - _iceServerPort(ICE_SERVER_DEFAULT_PORT) + _httpManager(QHostAddress::AnyIPv4, DOMAIN_SERVER_HTTP_PORT, QString("%1/resources/web/").arg(QCoreApplication::applicationDirPath()), this) { - PathUtils::removeTemporaryApplicationDirs(); + if (_parentPID != -1) { + watchParentProcess(_parentPID); + } - parseCommandLine(); + PathUtils::removeTemporaryApplicationDirs(); DependencyManager::set(); DependencyManager::set(); @@ -316,10 +312,11 @@ DomainServer::DomainServer(int argc, char* argv[]) : connect(_contentManager.get(), &DomainContentBackupManager::recoveryCompleted, this, &DomainServer::restart); } -void DomainServer::parseCommandLine() { +void DomainServer::parseCommandLine(int argc, char* argv[]) { QCommandLineParser parser; parser.setApplicationDescription("High Fidelity Domain Server"); - parser.addHelpOption(); + const QCommandLineOption versionOption = parser.addVersionOption(); + const QCommandLineOption helpOption = parser.addHelpOption(); const QCommandLineOption iceServerAddressOption("i", "ice-server address", "IP:PORT or HOSTNAME:PORT"); parser.addOption(iceServerAddressOption); @@ -336,8 +333,24 @@ void DomainServer::parseCommandLine() { const QCommandLineOption parentPIDOption(PARENT_PID_OPTION, "PID of the parent process", "parent-pid"); parser.addOption(parentPIDOption); - if (!parser.parse(QCoreApplication::arguments())) { - qWarning() << parser.errorText() << endl; + + QStringList arguments; + for (int i = 0; i < argc; ++i) { + arguments << argv[i]; + } + if (!parser.parse(arguments)) { + std::cout << parser.errorText().toStdString() << std::endl; // Avoid Qt log spam + QCoreApplication mockApp(argc, argv); // required for call to showHelp() + parser.showHelp(); + Q_UNREACHABLE(); + } + + if (parser.isSet(versionOption)) { + parser.showVersion(); + Q_UNREACHABLE(); + } + if (parser.isSet(helpOption)) { + QCoreApplication mockApp(argc, argv); // required for call to showHelp() parser.showHelp(); Q_UNREACHABLE(); } @@ -354,7 +367,7 @@ void DomainServer::parseCommandLine() { if (_iceServerAddr.isEmpty()) { qWarning() << "Could not parse an IP address and port combination from" << hostnamePortString; - QMetaObject::invokeMethod(this, "quit", Qt::QueuedConnection); + ::exit(0); } } @@ -370,8 +383,8 @@ void DomainServer::parseCommandLine() { int parentPID = parser.value(parentPIDOption).toInt(&ok); if (ok) { - qDebug() << "Parent process PID is" << parentPID; - watchParentProcess(parentPID); + _parentPID = parentPID; + qDebug() << "Parent process PID is" << _parentPID; } } } diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index c69267f379..7e96787371 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -59,6 +59,8 @@ public: DomainServer(int argc, char* argv[]); ~DomainServer(); + static void parseCommandLine(int argc, char* argv[]); + enum DomainType { NonMetaverse, MetaverseDomain, @@ -138,7 +140,6 @@ signals: private: QUuid getID(); - void parseCommandLine(); QString getContentBackupDir(); QString getEntitiesDirPath(); @@ -228,7 +229,7 @@ private: QQueue _unfulfilledAssignments; TransactionHash _pendingAssignmentCredits; - bool _isUsingDTLS; + bool _isUsingDTLS { false }; QUrl _oauthProviderURL; QString _oauthClientID; @@ -265,10 +266,11 @@ private: friend class DomainGatekeeper; friend class DomainMetadata; - QString _iceServerAddr; - int _iceServerPort; - bool _overrideDomainID { false }; // should we override the domain-id from settings? - QUuid _overridingDomainID { QUuid() }; // what should we override it with? + static QString _iceServerAddr; + static int _iceServerPort; + static bool _overrideDomainID; // should we override the domain-id from settings? + static QUuid _overridingDomainID; // what should we override it with? + static int _parentPID; bool _sendICEServerAddressToMetaverseAPIInProgress { false }; bool _sendICEServerAddressToMetaverseAPIRedo { false }; diff --git a/domain-server/src/main.cpp b/domain-server/src/main.cpp index d7856bf867..7aea9cc3d4 100644 --- a/domain-server/src/main.cpp +++ b/domain-server/src/main.cpp @@ -24,6 +24,8 @@ int main(int argc, char* argv[]) { setupHifiApplication(BuildInfo::DOMAIN_SERVER_NAME); + DomainServer::parseCommandLine(argc, argv); + Setting::init(); int currentExitCode = 0; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index c1ba6f0535..3e3c9da148 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -42,6 +42,48 @@ extern "C" { int main(int argc, const char* argv[]) { setupHifiApplication(BuildInfo::INTERFACE_NAME); + QStringList arguments; + for (int i = 0; i < argc; ++i) { + arguments << argv[i]; + } + + QCommandLineParser parser; + parser.setApplicationDescription("High Fidelity Interface"); + QCommandLineOption versionOption = parser.addVersionOption(); + QCommandLineOption helpOption = parser.addHelpOption(); + + QCommandLineOption urlOption("url", "", "value"); + QCommandLineOption noUpdaterOption("no-updater", "Do not show auto-updater"); + QCommandLineOption checkMinSpecOption("checkMinSpec", "Check if machine meets minimum specifications"); + QCommandLineOption runServerOption("runServer", "Whether to run the server"); + QCommandLineOption serverContentPathOption("serverContentPath", "Where to find server content", "serverContentPath"); + QCommandLineOption allowMultipleInstancesOption("allowMultipleInstances", "Allow multiple instances to run"); + QCommandLineOption overrideAppLocalDataPathOption("cache", "set test cache ", "dir"); + QCommandLineOption overrideScriptsPathOption(SCRIPTS_SWITCH, "set scripts ", "path"); + + parser.addOption(urlOption); + parser.addOption(noUpdaterOption); + parser.addOption(checkMinSpecOption); + parser.addOption(runServerOption); + parser.addOption(serverContentPathOption); + parser.addOption(overrideAppLocalDataPathOption); + parser.addOption(overrideScriptsPathOption); + parser.addOption(allowMultipleInstancesOption); + + if (!parser.parse(arguments)) { + std::cout << parser.errorText().toStdString() << std::endl; // Avoid Qt log spam + } + + if (parser.isSet(versionOption)) { + parser.showVersion(); + Q_UNREACHABLE(); + } + if (parser.isSet(helpOption)) { + QCoreApplication mockApp(argc, const_cast(argv)); // required for call to showHelp() + parser.showHelp(); + Q_UNREACHABLE(); + } + // Early check for --traceFile argument auto tracer = DependencyManager::set(); const char * traceFile = nullptr; @@ -95,30 +137,6 @@ int main(int argc, const char* argv[]) { qDebug() << "Crash handler started:" << crashHandlerStarted; } - QStringList arguments; - for (int i = 0; i < argc; ++i) { - arguments << argv[i]; - } - - QCommandLineParser parser; - QCommandLineOption urlOption("url", "", "value"); - QCommandLineOption noUpdaterOption("no-updater", "Do not show auto-updater"); - QCommandLineOption checkMinSpecOption("checkMinSpec", "Check if machine meets minimum specifications"); - QCommandLineOption runServerOption("runServer", "Whether to run the server"); - QCommandLineOption serverContentPathOption("serverContentPath", "Where to find server content", "serverContentPath"); - QCommandLineOption allowMultipleInstancesOption("allowMultipleInstances", "Allow multiple instances to run"); - QCommandLineOption overrideAppLocalDataPathOption("cache", "set test cache ", "dir"); - QCommandLineOption overrideScriptsPathOption(SCRIPTS_SWITCH, "set scripts ", "path"); - parser.addOption(urlOption); - parser.addOption(noUpdaterOption); - parser.addOption(checkMinSpecOption); - parser.addOption(runServerOption); - parser.addOption(serverContentPathOption); - parser.addOption(overrideAppLocalDataPathOption); - parser.addOption(overrideScriptsPathOption); - parser.addOption(allowMultipleInstancesOption); - parser.parse(arguments); - const QString& applicationName = getInterfaceSharedMemoryName(); bool instanceMightBeRunning = true;