Skip to content
Snippets Groups Projects
Commit a04f4f38 authored by Wuttke, Joachim's avatar Wuttke, Joachim Committed by Wuttke, Joachim
Browse files

Upon invalid command-line option, show Qt message box

parent ff9c308e
No related branches found
No related tags found
1 merge request!705GUI/App/main, file paths: various improvements
......@@ -14,7 +14,9 @@
#include "App/AppOptions.h"
#include "GUI/Util/Path.h"
#include <QApplication>
#include <QSize>
#include <QMessageBox>
#include <QStringList>
#include <boost/program_options/config.hpp>
#include <boost/program_options/parsers.hpp>
......@@ -43,10 +45,19 @@ bool isValid(const QSize& win_size)
return win_size.width() > 640 && win_size.height() > 480;
}
void exitWithGuiMessage(const QString msg)
{
int argc = 0;
QApplication a(argc, nullptr);
QMessageBox msgBox;
msgBox.setText(msg);
msgBox.exec();
exit(-1);
}
} // namespace
ApplicationOptions::ApplicationOptions(int argc, char** argv)
: m_options_is_consistent(false)
{
m_options.add_options()("help,h", "print help message and exit");
m_options.add_options()("version,v", "print version and exit");
......@@ -72,16 +83,10 @@ bool ApplicationOptions::find(std::string name) const
return m_variables_map.count(name);
}
bool ApplicationOptions::isConsistent() const
{
return m_options_is_consistent;
}
//! parse command line arguments
void ApplicationOptions::parseCommandLine(int argc, char** argv)
{
m_options_is_consistent = false;
// parsing command line arguments
try {
// if positional option description is empty, no command line arguments
......@@ -97,11 +102,13 @@ void ApplicationOptions::parseCommandLine(int argc, char** argv)
// 'notify' raises any errors encountered
bpo::notify(m_variables_map);
m_options_is_consistent = true;
} catch (std::exception& e) {
// we get here if there is unrecognized options
std::cout << "main() -> " << e.what() << std::endl;
std::cout << m_options << std::endl;
std::stringstream s;
s << "BornAgain was launched with invalid command line option.\n"
<< "Parser error message: " << e.what() << ".\n"
<< "Available options:\n"
<< m_options << "\n";
exitWithGuiMessage(QString::fromStdString(s.str()));
}
}
......@@ -130,10 +137,8 @@ void ApplicationOptions::processOptions()
}
else if (m_variables_map.count(geometry)) {
if (!isValid(mainWindowSize())) {
std::cout << "Wrong window size, try --geometry=1600x900\n";
m_options_is_consistent = false;
}
if (!isValid(mainWindowSize()))
exitWithGuiMessage("Wrong window size, try --geometry=1600x900\n");
}
}
......
......@@ -35,9 +35,6 @@ public:
//! access to variable with given name defined in variables container
const bpo::variable_value& operator[](const std::string& s) const;
//! Returns true if options are consistent (no conflicts, no --help request)
bool isConsistent() const;
QSize mainWindowSize() const;
bool disableHighDPISupport() const;
......
......@@ -32,8 +32,6 @@ void messageHandler(QtMsgType, const QMessageLogContext&, const QString&) {}
int main(int argc, char* argv[])
{
ApplicationOptions options(argc, argv);
if (!options.isConsistent())
return 0;
QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
qRegisterMetaType<QVector<double>>("QVector<double>");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment