Molecular Dynamics Simulation  1.0
Functions
CLIParser.h File Reference
#include <iostream>
#include <string>
#include "io/cli/CLIParams.h"
#include "simulation/SimulationParams.h"
Include dependency graph for CLIParser.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

CLIParams parse_arguments (int argc, char *argsv[])
 Parses the command line arguments. More...
 
SimulationParams merge_parameters (const CLIParams &params_cli, const std::optional< SimulationParams > &file_params)
 Merges the simulation parameters retrieved via the command line with the ones from the XML file. Command line arguments have priority. More...
 

Function Documentation

◆ merge_parameters()

SimulationParams merge_parameters ( const CLIParams params_cli,
const std::optional< SimulationParams > &  file_params 
)

Merges the simulation parameters retrieved via the command line with the ones from the XML file. Command line arguments have priority.

Parameters
params_cliSimulation parameters retrieved via Command line arguments
file_paramsSimulation parameters retrieved via the parsed input file
Returns
SimulationParams containing the merged parameters. Command line arguments have priority.

Merges the command line arguments with the parameters retrieved from the file. Command line arguments have priority.

Definition at line 69 of file CLIParser.cpp.

69  {
70  if (!file_params) {
71  Logger::logger->warn("No simulation parameters provided. Try using a XML file as input.");
72  throw std::runtime_error("No simulation parameters provided. Try using a XML file as input.");
73  }
74 
75  SimulationParams params = *file_params;
76 
77  // Update the parameters with the ones from the command line
78  params.fresh = params_cli.fresh;
79 
80  return params;
81 }
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35
Contains all parameters needed to run a simulation.
bool fresh
Flag to indicate whether the simulation should be run from scratch, or whether cached data should be ...
bool fresh
Definition: CLIParams.h:14

◆ parse_arguments()

CLIParams parse_arguments ( int  argc,
char *  argsv[] 
)

Parses the command line arguments.

Parameters
argcNumber of arguments
argsvArray of arguments
Returns
SimulationParams containing the parsed arguments, or default values for the arguments that were not specified

Parses the command line arguments and returns a tuple containing all the arguments.

Definition at line 9 of file CLIParser.cpp.

9  {
10  std::filesystem::path input_file_path;
11  std::string log_level;
12  std::string log_output;
13 
14  bool fresh = false;
15 
16  // choosing 0 as one of the parameters (end_time, delta_t, fps, video_length) is equivalent to choosing the default value
17  boost::program_options::options_description options_desc("Allowed options");
18  options_desc.add_options()("help,h", "produce help message");
19  options_desc.add_options()(
20  "input_file_path,f", boost::program_options::value<std::filesystem::path>(&input_file_path),
21  "The path to the input file. Must be specified, otherwise the program will terminate. Can be inserted as positional argument.");
22  options_desc.add_options()("log_level,l", boost::program_options::value<std::string>(&log_level)->default_value("info"),
23  "The log level. Possible values: trace, debug, info, warning, error, critical, off");
24  options_desc.add_options()(
25  "log_output", boost::program_options::value<std::string>(&log_output)->default_value("std"),
26  "You can only choose between the output options std(only cl output) and file (only file output). Default: no file output");
27  options_desc.add_options()(
28  "fresh", boost::program_options::bool_switch(&fresh)->default_value(false),
29  "Rerun the simulation from scratch without using any cached data. This will delete the whole output directory.");
30 
31  boost::program_options::positional_options_description positional_options_desc;
32  positional_options_desc.add("input_file_path", -1);
33 
34  boost::program_options::variables_map variables_map;
35  boost::program_options::store(
36  boost::program_options::command_line_parser(argc, argsv).options(options_desc).positional(positional_options_desc).run(),
37  variables_map);
38  boost::program_options::notify(variables_map);
39 
40  if (log_output == "std" || log_output == "STD") {
41  Logger::logger->info("Log output: std");
42  } else if (log_output == "file" || log_output == "FILE") {
44  Logger::logger->info("Log output: file");
45  } else {
46  std::cout << "Error: Invalid log output given. Options: no file output: 'std' and file output: 'file'" << std::endl;
47  exit(-1);
48  }
49 
50  Logger::update_level(log_level);
51 
52  if (argc <= 1 || variables_map.count("help")) {
53  std::stringstream help_message;
54  help_message << options_desc << std::endl;
55  Logger::logger->info(help_message.str());
56  exit(-1);
57  }
58  if (!variables_map.count("input_file_path")) {
59  Logger::logger->error("No input file path given.");
60  std::stringstream help_message;
61  help_message << options_desc << std::endl;
62  Logger::logger->info(help_message.str());
63  exit(-1);
64  }
65 
66  return CLIParams{input_file_path, fresh};
67 }
static void update_level(std::string &log_level)
Sets the log level of the logger.
Definition: Logger.cpp:48
static std::shared_ptr< spdlog::logger > init_logger(LogType log_type=LogType::STD)
Initializes the logger.
Definition: Logger.cpp:23