netxsimdg
Loading...
Searching...
No Matches
CommandLineParser.cpp
Go to the documentation of this file.
1
9
11
12#include <boost/program_options.hpp>
13#include <cstdlib>
14#include <iostream>
15#include <string>
16
17namespace Nextsim {
18
19const std::string CommandLineParser::allModuleString = ConfigurationHelpPrinter::allStr;
20const std::string CommandLineParser::availableModuleString = ConfigurationHelpPrinter::availStr;
21
28CommandLineParser::CommandLineParser(int argc, char* argv[])
29{
30 char oneConfigFile[] = "config-file";
31 char manyConfigFiles[] = "config-files";
32 char helpConfigStr[] = "help-config";
33
34 boost::program_options::options_description opt("neXtSIM_DG command line options:");
35 // clang-format off
36 std::string helpConfigHelp = std::string("arg = [") + availableModuleString + " | moduleName | " + allModuleString + "]\n"
37 "print help associated with one or more modules.\n"
38 "avail list all available module names.\n"
39 "moduleName print the help message associated with the given module.\n"
40 "all print all help messages associated with all available modules.";
41 opt.add_options()
42 ("help,h", "print help message")
43 (helpConfigStr, boost::program_options::value<std::string>(), helpConfigHelp.c_str())
44 (oneConfigFile, boost::program_options::value<std::string>(),
45 "specify a configuration file")
46 (manyConfigFiles,
47 boost::program_options::value<std::vector<std::string>>()->multitoken(),
48 "specify a list of configuration files" )
49 ;
50 // clang-format on
51 auto parsed = boost::program_options::command_line_parser(argc, argv)
52 .options(opt)
53 .style(boost::program_options::command_line_style::
54 unix_style) // | po::command_line_style::allow_long_disguise)
55 .allow_unregistered()
56 .run();
57 boost::program_options::store(parsed, m_arguments);
58
59 // Print help and exit
60 if (m_arguments.count("help") || m_arguments.empty()) {
61 std::cerr << opt << std::endl;
62 std::exit(EXIT_SUCCESS);
63 }
64
65 m_configHelp.clear();
66
67 // Store the config file names in order. The variables_map does not.
68 for (auto& lionOption : parsed.options) {
69 if (lionOption.string_key == oneConfigFile || lionOption.string_key == manyConfigFiles) {
70 for (auto& stringValue : lionOption.value) {
71 m_configFilenames.push_back(stringValue);
72 }
73 } else if (lionOption.string_key == helpConfigStr) {
74 m_configHelp = lionOption.value.front();
75 }
76 }
77}
78
82std::vector<std::string> CommandLineParser::getConfigFileNames() { return m_configFilenames; }
83
84} /* namespace Nextsim */
static const std::string availableModuleString
std::vector< std::string > getConfigFileNames()
static const std::string allModuleString