From 81de872c7b76a81b3cad717439428fc54a875608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20St=C3=B8ren?= Date: Thu, 8 Aug 2019 10:45:02 +0200 Subject: [PATCH] #4543, #4544 Fixed missing command line help text from menu-command and --help option. Improved the error text when an unknown option is used. Improved the dialog used to show command line options, and make the text selectable --- .../Application/RiaApplication.cpp | 10 +++ ApplicationCode/Application/RiaApplication.h | 8 +-- .../Application/RiaConsoleApplication.cpp | 23 ++++--- .../Application/RiaConsoleApplication.h | 3 +- .../Application/RiaGuiApplication.cpp | 61 +++++++++++++------ .../Application/RiaGuiApplication.h | 3 +- ApplicationCode/Application/RiaMain.cpp | 16 +++-- .../Application/Tools/RiaArgumentParser.cpp | 6 +- .../ApplicationCommands/RicHelpFeatures.cpp | 2 +- 9 files changed, 82 insertions(+), 50 deletions(-) diff --git a/ApplicationCode/Application/RiaApplication.cpp b/ApplicationCode/Application/RiaApplication.cpp index cdc0ff382e..1d3cbbf22e 100644 --- a/ApplicationCode/Application/RiaApplication.cpp +++ b/ApplicationCode/Application/RiaApplication.cpp @@ -1127,6 +1127,8 @@ QString RiaApplication::commandLineParameterHelp() { QString helpText = QString("\n%1 v. %2\n").arg(RI_APPLICATION_NAME).arg(RiaApplication::getVersionStringApp(false)); helpText += "Copyright Equinor ASA, Ceetron Solution AS, Ceetron AS\n\n"; + helpText += m_commandLineHelpText; + return helpText; } @@ -1297,6 +1299,14 @@ void RiaApplication::setStartDir(const QString& startDir) m_startupDefaultDirectory = startDir; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiaApplication::setCommandLineHelpText(const QString& commandLineHelpText) +{ + m_commandLineHelpText = commandLineHelpText; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/Application/RiaApplication.h b/ApplicationCode/Application/RiaApplication.h index 816c045657..a890acf9b4 100644 --- a/ApplicationCode/Application/RiaApplication.h +++ b/ApplicationCode/Application/RiaApplication.h @@ -159,7 +159,8 @@ public: RiaPreferences* preferences(); void applyPreferences(const RiaPreferences* oldPreferences = nullptr); - static QString commandLineParameterHelp(); + QString commandLineParameterHelp(); + void setCommandLineHelpText(const QString& commandLineHelpText); void setCacheDataObject(const QString& key, const QVariant& dataObject); QVariant cacheDataObject(const QString& key) const; @@ -187,8 +188,7 @@ public: virtual ApplicationStatus handleArguments(cvf::ProgramOptions* progOpt) = 0; virtual int launchUnitTestsWithConsole(); virtual void addToRecentFiles(const QString& fileName) {} - virtual void showInformationMessage(const QString& infoText) = 0; - virtual void showErrorMessage(const QString& errMsg) = 0; + virtual void showFormattedTextInMessageBoxOrConsole(const QString& errMsg) = 0; virtual void launchGrpcServer() = 0; @@ -230,7 +230,7 @@ protected: std::map m_fileDialogDefaultDirectories; QString m_startupDefaultDirectory; - + QString m_commandLineHelpText; QMap m_sessionCache; // Session cache used to store username/passwords per session std::list m_commandQueue; diff --git a/ApplicationCode/Application/RiaConsoleApplication.cpp b/ApplicationCode/Application/RiaConsoleApplication.cpp index 5c4e6cf53d..1677b862cf 100644 --- a/ApplicationCode/Application/RiaConsoleApplication.cpp +++ b/ApplicationCode/Application/RiaConsoleApplication.cpp @@ -100,6 +100,13 @@ RiaApplication::ApplicationStatus RiaConsoleApplication::handleArguments(cvf::Pr return KEEP_GOING; } + if (progOpt->option("help") || progOpt->option("?")) + { + this->showFormattedTextInMessageBoxOrConsole("\nThe current command line options in ResInsight are:\n" + + this->commandLineParameterHelp()); + return RiaApplication::EXIT_COMPLETED; + } + // Unit testing // -------------------------------------------------------- if (cvf::Option o = progOpt->option("unittest")) @@ -324,19 +331,11 @@ RiaApplication::ApplicationStatus RiaConsoleApplication::handleArguments(cvf::Pr } //-------------------------------------------------------------------------------------------------- -/// +/// //-------------------------------------------------------------------------------------------------- -void RiaConsoleApplication::showInformationMessage(const QString& text) +void RiaConsoleApplication::showFormattedTextInMessageBoxOrConsole(const QString& errMsg) { - RiaLogging::info(text); -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RiaConsoleApplication::showErrorMessage(const QString& errMsg) -{ - RiaLogging::error(errMsg); + std::cout << errMsg.toStdString(); } //-------------------------------------------------------------------------------------------------- @@ -375,7 +374,7 @@ void RiaConsoleApplication::invokeProcessEvents(QEventLoop::ProcessEventsFlags f //-------------------------------------------------------------------------------------------------- void RiaConsoleApplication::onProjectOpeningError(const QString& errMsg) { - showErrorMessage(errMsg); + RiaLogging::error(errMsg); } //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/Application/RiaConsoleApplication.h b/ApplicationCode/Application/RiaConsoleApplication.h index 7506a1125e..5097c2a6d5 100644 --- a/ApplicationCode/Application/RiaConsoleApplication.h +++ b/ApplicationCode/Application/RiaConsoleApplication.h @@ -42,8 +42,7 @@ public: // Public RiaApplication overrides void initialize() override; ApplicationStatus handleArguments(cvf::ProgramOptions* progOpt) override; - void showInformationMessage(const QString& text) override; - void showErrorMessage(const QString& errMsg) override; + void showFormattedTextInMessageBoxOrConsole(const QString& errMsg) override; void launchGrpcServer() override; #ifdef ENABLE_GRPC RiaGrpcServer* grpcServer() const override; diff --git a/ApplicationCode/Application/RiaGuiApplication.cpp b/ApplicationCode/Application/RiaGuiApplication.cpp index f5b6d4740e..adcbf82629 100644 --- a/ApplicationCode/Application/RiaGuiApplication.cpp +++ b/ApplicationCode/Application/RiaGuiApplication.cpp @@ -120,6 +120,9 @@ #include #include #include +#include +#include +#include #include @@ -152,6 +155,7 @@ void AppEnum::setUp() /// //================================================================================================== + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -588,6 +592,13 @@ RiaApplication::ApplicationStatus RiaGuiApplication::handleArguments(cvf::Progra return KEEP_GOING; } + if (progOpt->option("help") || progOpt->option("?")) + { + this->showFormattedTextInMessageBoxOrConsole("The current command line options in ResInsight are:\n" + + this->commandLineParameterHelp()); + return RiaApplication::EXIT_COMPLETED; + } + // Unit testing // -------------------------------------------------------- if (cvf::Option o = progOpt->option("unittest")) @@ -1201,33 +1212,43 @@ void RiaGuiApplication::clearAllSelections() } //-------------------------------------------------------------------------------------------------- -/// +/// //-------------------------------------------------------------------------------------------------- -void RiaGuiApplication::showInformationMessage(const QString& text) +void RiaGuiApplication::showFormattedTextInMessageBoxOrConsole(const QString& text) { - QString helpText = text; + // Create a message dialog with cut/paste friendly text + QDialog dlg(RiuMainWindow::instance()); + dlg.setModal(true); - QMessageBox msgBox; - msgBox.setIcon(QMessageBox::Information); - msgBox.setWindowTitle("ResInsight"); + QGridLayout* layout = new QGridLayout; + dlg.setLayout(layout); - helpText.replace("&", "&"); - helpText.replace("<", "<"); - helpText.replace(">", ">"); + QTextEdit* textEdit = new QTextEdit(&dlg); + layout->addWidget(textEdit, 0, 1, 1, 2); + layout->setColumnStretch(1,3); - helpText = QString("
%1
").arg(helpText); - msgBox.setText(helpText); + QPushButton* okButton = new QPushButton; + okButton->setText("Ok"); + layout->addWidget(okButton,2,2); + QObject::connect(okButton, SIGNAL(clicked()), &dlg, SLOT(accept())); - msgBox.exec(); -} + // Convert text to text edit friendly format + QString formattedText = text; + formattedText.replace("&", "&"); + formattedText.replace("<", "<"); + formattedText.replace(">", ">"); + formattedText = QString("
%1
").arg(formattedText); + + textEdit->setText(formattedText); + + // Resize dialog to fit text etc. + textEdit->document()->adjustSize(); + QSizeF docSize = textEdit->document()->size(); + dlg.resize( 20 + docSize.width() + 2*layout->margin(), + 20 + docSize.height() + 2*layout->margin() + layout->spacing() + okButton->sizeHint().height()); + + dlg.exec(); -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RiaGuiApplication::showErrorMessage(const QString& errMsg) -{ - QErrorMessage errDialog(mainWindow()); - errDialog.showMessage(errMsg); } //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/Application/RiaGuiApplication.h b/ApplicationCode/Application/RiaGuiApplication.h index 6ff68c02f1..03918f5602 100644 --- a/ApplicationCode/Application/RiaGuiApplication.h +++ b/ApplicationCode/Application/RiaGuiApplication.h @@ -130,8 +130,7 @@ public: ApplicationStatus handleArguments(cvf::ProgramOptions* progOpt) override; int launchUnitTestsWithConsole() override; void addToRecentFiles(const QString& fileName) override; - void showInformationMessage(const QString& text) override; - void showErrorMessage(const QString& errMsg) override; + void showFormattedTextInMessageBoxOrConsole(const QString& errMsg) override; void launchGrpcServer() override; #ifdef ENABLE_GRPC RiaGrpcServer* grpcServer() const override; diff --git a/ApplicationCode/Application/RiaMain.cpp b/ApplicationCode/Application/RiaMain.cpp index d63188df3d..d6f30f473d 100644 --- a/ApplicationCode/Application/RiaMain.cpp +++ b/ApplicationCode/Application/RiaMain.cpp @@ -59,21 +59,25 @@ int main(int argc, char *argv[]) cvf::ProgramOptions progOpt; bool result = RiaArgumentParser::parseArguments(&progOpt); + const cvf::String usageText = progOpt.usageText(110, 30); app->initialize(); + app->setCommandLineHelpText( cvfqt::Utils::toQString(usageText) ); if (!result) { std::vector unknownOptions = progOpt.unknownOptions(); - QStringList unknownOptionsText; + QString unknownOptionsText; for (cvf::String option : unknownOptions) { - unknownOptionsText += QString("Unknown option: %1").arg(cvfqt::Utils::toQString(option)); + unknownOptionsText += QString("\tUnknown option: %1\n").arg(cvfqt::Utils::toQString(option)); } - const cvf::String usageText = progOpt.usageText(110, 30); - app->showErrorMessage(RiaApplication::commandLineParameterHelp() + - cvfqt::Utils::toQString(usageText) + - unknownOptionsText.join("\n")); + app->showFormattedTextInMessageBoxOrConsole("ERROR: Unknown command line options detected ! \n" + + unknownOptionsText + + "\n\n" + + "The current command line options in ResInsight are:\n" + + app->commandLineParameterHelp()); + if (dynamic_cast(app.get()) == nullptr) { return 1; diff --git a/ApplicationCode/Application/Tools/RiaArgumentParser.cpp b/ApplicationCode/Application/Tools/RiaArgumentParser.cpp index 4d100532c0..938c777650 100644 --- a/ApplicationCode/Application/Tools/RiaArgumentParser.cpp +++ b/ApplicationCode/Application/Tools/RiaArgumentParser.cpp @@ -103,8 +103,8 @@ bool RiaArgumentParser::parseArguments(cvf::ProgramOptions* progOpt) "[] ", "Supply list of cases to replace in project, performing command file for each case.", cvf::ProgramOptions::SINGLE_VALUE); - progOpt->registerOption("help", "", "Displays help text."); - progOpt->registerOption("?", "", "Displays help text."); + progOpt->registerOption("help", "", "Displays help text and exits."); + progOpt->registerOption("?", "", "Displays help text and exits."); progOpt->registerOption("regressiontest", "", "System command", cvf::ProgramOptions::SINGLE_VALUE); progOpt->registerOption("updateregressiontestbase", "", "System command", cvf::ProgramOptions::SINGLE_VALUE); #ifdef USE_UNIT_TESTS @@ -120,7 +120,7 @@ bool RiaArgumentParser::parseArguments(cvf::ProgramOptions* progOpt) // If positional parameter functionality is to be supported, the test for existence of positionalParameters must be removed // This is based on a pull request by @andlaus https://github.com/OPM/ResInsight/pull/162 - if (!parseOk || progOpt->hasOption("help") || progOpt->hasOption("?") || !progOpt->positionalParameters().empty()) + if (!parseOk || !progOpt->positionalParameters().empty()) { return false; } diff --git a/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.cpp b/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.cpp index d77a880452..1786d04e65 100644 --- a/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.cpp +++ b/ApplicationCode/Commands/ApplicationCommands/RicHelpFeatures.cpp @@ -192,7 +192,7 @@ void RicHelpCommandLineFeature::onActionTriggered(bool isChecked) RiaApplication* app = RiaApplication::instance(); QString text = app->commandLineParameterHelp(); - app->showInformationMessage(text); + app->showFormattedTextInMessageBoxOrConsole(text); } //--------------------------------------------------------------------------------------------------