diff --git a/tools/oven/src/OvenCLIApplication.cpp b/tools/oven/src/OvenCLIApplication.cpp index eab8647e71..afcdfbfcd2 100644 --- a/tools/oven/src/OvenCLIApplication.cpp +++ b/tools/oven/src/OvenCLIApplication.cpp @@ -24,12 +24,23 @@ static const QString CLI_OUTPUT_PARAMETER = "o"; static const QString CLI_TYPE_PARAMETER = "t"; static const QString CLI_DISABLE_TEXTURE_COMPRESSION_PARAMETER = "disable-texture-compression"; +QUrl OvenCLIApplication::_inputUrlParameter; +QUrl OvenCLIApplication::_outputUrlParameter; +QString OvenCLIApplication::_typeParameter; + OvenCLIApplication::OvenCLIApplication(int argc, char* argv[]) : QCoreApplication(argc, argv) { + BakerCLI* cli = new BakerCLI(this); + QMetaObject::invokeMethod(cli, "bakeFile", Qt::QueuedConnection, Q_ARG(QUrl, _inputUrlParameter), + Q_ARG(QString, _outputUrlParameter.toString()), Q_ARG(QString, _typeParameter)); +} + +void OvenCLIApplication::parseCommandLine(int argc, char* argv[]) { // parse the command line parameters QCommandLineParser parser; + parser.setApplicationDescription("High Fidelity Oven"); parser.addOptions({ { CLI_INPUT_PARAMETER, "Path to file that you would like to bake.", "input" }, { CLI_OUTPUT_PARAMETER, "Path to folder that will be used as output.", "output" }, @@ -37,25 +48,45 @@ OvenCLIApplication::OvenCLIApplication(int argc, char* argv[]) : { CLI_DISABLE_TEXTURE_COMPRESSION_PARAMETER, "Disable texture compression." } }); - parser.addHelpOption(); - parser.process(*this); + auto versionOption = parser.addVersionOption(); + auto helpOption = parser.addHelpOption(); - if (parser.isSet(CLI_INPUT_PARAMETER) && parser.isSet(CLI_OUTPUT_PARAMETER)) { - BakerCLI* cli = new BakerCLI(this); - QUrl inputUrl(QDir::fromNativeSeparators(parser.value(CLI_INPUT_PARAMETER))); - QUrl outputUrl(QDir::fromNativeSeparators(parser.value(CLI_OUTPUT_PARAMETER))); - QString type = parser.isSet(CLI_TYPE_PARAMETER) ? parser.value(CLI_TYPE_PARAMETER) : QString::null; - - if (parser.isSet(CLI_DISABLE_TEXTURE_COMPRESSION_PARAMETER)) { - qDebug() << "Disabling texture compression"; - TextureBaker::setCompressionEnabled(false); - } - - QMetaObject::invokeMethod(cli, "bakeFile", Qt::QueuedConnection, Q_ARG(QUrl, inputUrl), - Q_ARG(QString, outputUrl.toString()), Q_ARG(QString, type)); - } else { - parser.showHelp(); - QCoreApplication::quit(); + 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(); + } + + if (!parser.isSet(CLI_INPUT_PARAMETER) || !parser.isSet(CLI_OUTPUT_PARAMETER)) { + std::cout << "Error: Input and Output not set" << std::endl; // Avoid Qt log spam + QCoreApplication mockApp(argc, argv); // required for call to showHelp() + parser.showHelp(); + Q_UNREACHABLE(); + } + + _inputUrlParameter = QDir::fromNativeSeparators(parser.value(CLI_INPUT_PARAMETER)); + _outputUrlParameter = QDir::fromNativeSeparators(parser.value(CLI_OUTPUT_PARAMETER)); + + _typeParameter = parser.isSet(CLI_TYPE_PARAMETER) ? parser.value(CLI_TYPE_PARAMETER) : QString::null; + + if (parser.isSet(CLI_DISABLE_TEXTURE_COMPRESSION_PARAMETER)) { + qDebug() << "Disabling texture compression"; + TextureBaker::setCompressionEnabled(false); + } } diff --git a/tools/oven/src/OvenCLIApplication.h b/tools/oven/src/OvenCLIApplication.h index 5d81166f69..21fc9e9ba1 100644 --- a/tools/oven/src/OvenCLIApplication.h +++ b/tools/oven/src/OvenCLIApplication.h @@ -21,7 +21,14 @@ class OvenCLIApplication : public QCoreApplication, public Oven { public: OvenCLIApplication(int argc, char* argv[]); + static void parseCommandLine(int argc, char* argv[]); + static OvenCLIApplication* instance() { return dynamic_cast(QCoreApplication::instance()); } + +private: + static QUrl _inputUrlParameter; + static QUrl _outputUrlParameter; + static QString _typeParameter; }; #endif // hifi_OvenCLIApplication_h diff --git a/tools/oven/src/main.cpp b/tools/oven/src/main.cpp index 7009089402..586dae06a5 100644 --- a/tools/oven/src/main.cpp +++ b/tools/oven/src/main.cpp @@ -18,14 +18,19 @@ int main (int argc, char** argv) { setupHifiApplication("Oven"); - // init the settings interface so we can save and load settings - Setting::init(); - // figure out if we're launching our GUI application or just the simple command line interface if (argc > 1) { + OvenCLIApplication::parseCommandLine(argc, argv); + + // init the settings interface so we can save and load settings + Setting::init(); + OvenCLIApplication app { argc, argv }; return app.exec(); } else { + // init the settings interface so we can save and load settings + Setting::init(); + OvenGUIApplication app { argc, argv }; return app.exec(); }