BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
AppOptions.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file App/AppOptions.cpp
6 //! @brief Implements class ProgramOptions.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "App/AppOptions.h"
16 #include "GUI/Util/Path.h"
17 #include <QApplication>
18 #include <QMessageBox>
19 #include <QSize>
20 #include <QStringList>
21 #include <boost/program_options/config.hpp>
22 #include <boost/program_options/parsers.hpp>
23 #include <fstream>
24 #include <iostream>
25 
26 namespace {
27 
28 const char* geometry = "geometry";
29 const char* nohighdpi = "nohighdpi";
30 
31 //! Converts string "1600x1000" to QSize(1600, 1000)
32 QSize windowSize(const QString& size_string)
33 {
34  QStringList list = size_string.split("x");
35 
36  if (list.size() != 2)
37  return QSize();
38 
39  return QSize(list[0].toInt(), list[1].toInt());
40 }
41 
42 //! Returns true if windows size makes sence.
43 bool isValid(const QSize& win_size)
44 {
45  return win_size.width() > 640 && win_size.height() > 480;
46 }
47 
48 void exitWithGuiMessage(const QString msg)
49 {
50  int argc = 0;
51  QApplication a(argc, nullptr);
52  QMessageBox msgBox;
53  msgBox.setText(msg);
54  msgBox.exec();
55  exit(-1);
56 }
57 
58 } // namespace
59 
61 {
62  m_options.add_options()("help,h", "print help message and exit");
63  m_options.add_options()("version,v", "print version and exit");
64  m_options.add_options()("with-debug", "run application with debug printout");
65  m_options.add_options()(geometry, bpo::value<std::string>(),
66  "Main window geometry, e.g. 1600x1000");
67  m_options.add_options()(nohighdpi, "Run without high-dpi support");
68 
69  parseCommandLine(argc, argv);
70 
72 }
73 
74 //! access variables
75 
76 const bpo::variable_value& ApplicationOptions::operator[](const std::string& s) const
77 {
78  return m_variables_map[s];
79 }
80 
81 bool ApplicationOptions::find(std::string name) const
82 {
83  return m_variables_map.count(name);
84 }
85 
86 //! parse command line arguments
87 
88 void ApplicationOptions::parseCommandLine(int argc, char** argv)
89 {
90  // parsing command line arguments
91  try {
92  // if positional option description is empty, no command line arguments
93  // without '--' or '-' will be accepted
94  // 'store' populates the variable map
95  bpo::store(bpo::command_line_parser(argc, argv)
96  .options(m_options)
97  .positional(m_positional_options)
98  .run(),
100  // bpo::store(bpo::parse_command_line(argc, argv, m_options), m_variables_map);
101 
102  // 'notify' raises any errors encountered
103  bpo::notify(m_variables_map);
104 
105  } catch (std::exception& e) {
106  std::stringstream s;
107  s << "BornAgain was launched with invalid command line option.\n"
108  << "Parser error message: " << e.what() << ".\n"
109  << "Available options:\n"
110  << m_options << "\n";
111  exitWithGuiMessage(QString::fromStdString(s.str()));
112  }
113 }
114 
115 boost::program_options::variables_map& ApplicationOptions::getVariables()
116 {
117  return m_variables_map;
118 }
119 
120 boost::program_options::options_description& ApplicationOptions::getOptions()
121 {
122  return m_options;
123 }
124 
126 {
127  if (m_variables_map.count("help")) {
128  std::cout << "BornAgain Graphical User Interface" << std::endl;
129  std::cout << m_options << std::endl;
130  exit(0);
131  }
132 
133  else if (m_variables_map.count("version")) {
134  std::cout << "BornAgain-" << GUI::Util::Path::getBornAgainVersionString().toStdString()
135  << std::endl;
136  exit(0);
137  }
138 
139  else if (m_variables_map.count(geometry)) {
140  if (!isValid(mainWindowSize()))
141  exitWithGuiMessage("Wrong window size, try --geometry=1600x900\n");
142  }
143 }
144 
146 {
147  QString size_str = QString::fromStdString(m_variables_map[geometry].as<std::string>());
148  return windowSize(size_str);
149 }
150 
152 {
153  return find(nohighdpi);
154 }
Collection of utilities to parse command line options.
Defines class Helpers functions.
bool find(std::string name) const
Returns true if option with given name has been set.
Definition: AppOptions.cpp:81
bpo::variables_map & getVariables()
Returns reference to the variables container.
Definition: AppOptions.cpp:115
void parseCommandLine(int argc, char **argv)
Parses command line arguments.
Definition: AppOptions.cpp:88
bpo::options_description & getOptions()
Returns reference to the options description.
Definition: AppOptions.cpp:120
bpo::options_description m_options
options description, to be filled with add() from different program modules
Definition: AppOptions.h:60
ApplicationOptions(int argc=0, char **argv=0)
Definition: AppOptions.cpp:60
bool disableHighDPISupport() const
Definition: AppOptions.cpp:151
bpo::variables_map m_variables_map
Definition: AppOptions.h:63
QSize mainWindowSize() const
Definition: AppOptions.cpp:145
bpo::positional_options_description m_positional_options
positional options description, to be filled with addPositional() from main module
Definition: AppOptions.h:62
const bpo::variable_value & operator[](const std::string &s) const
access to variable with given name defined in variables container
Definition: AppOptions.cpp:76
QString const & name(EShape k)
Definition: particles.cpp:20
QString getBornAgainVersionString()
Definition: Path.cpp:60