BioDynaMo  v1.05.119-a4ff3934
command_line_options.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_PARAM_COMMAND_LINE_OPTIONS_H_
16 #define CORE_PARAM_COMMAND_LINE_OPTIONS_H_
17 
21 #define CXXOPTS_VECTOR_DELIMITER '\n'
22 #include <TError.h>
23 #include <cxxopts.h>
24 
25 #include <algorithm>
26 #include <iostream>
27 #include <ostream>
28 #include <string>
29 #include <utility>
30 
31 #include "bdm_version.h"
32 #include "core/simulation.h"
33 
34 namespace bdm {
35 
38  public:
39  CommandLineOptions(int argc, const char** argv);
40 
42 
44  template <typename T>
45  void AddOption(const std::string& opt, std::string def,
46  const std::string& description = "",
47  std::string group = "Simulation") {
48  AddOption(std::move(group))(opt, description,
49  cxxopts::value<T>()->default_value(def));
50  }
51 
53  std::string GetSimulationName() const;
54 
56  bool IsSet(std::string option);
57 
58  template <typename T>
59  T Get(const std::string& val) {
60  if (parser_ == nullptr) {
61  this->Parse();
62  }
63  return (*parser_)[val].as<T>();
64  }
65 
66  private:
67  friend std::ostream& operator<<(std::ostream& os,
68  const CommandLineOptions& clo);
69 
70  void AddCoreOptions();
71 
73  cxxopts::OptionAdder AddOption(std::string group = "Simulation");
74 
76  void Parse();
77 
82  void ExtractSimulationName(const char* path);
83 
85  void HandleCoreOptions();
86 
87  int argc_;
88  const char** argv_;
89  // The name of the simulation
90  std::string sim_name_;
91 
92  // Flag to determine if new options were added
93  bool first_parse_ = true;
94  cxxopts::Options options_;
95  cxxopts::ParseResult* parser_ = nullptr;
96 };
97 
98 } // namespace bdm
99 
100 #endif // CORE_PARAM_COMMAND_LINE_OPTIONS_H_
bdm::CommandLineOptions::options_
cxxopts::Options options_
Definition: command_line_options.h:94
bdm::CommandLineOptions::IsSet
bool IsSet(std::string option)
Check if the argument for the specified option was set.
Definition: command_line_options.cc:92
bdm::CommandLineOptions::GetSimulationName
std::string GetSimulationName() const
Return the simulation name that was parsed from argv[0].
Definition: command_line_options.cc:51
bdm::CommandLineOptions::argv_
const char ** argv_
Definition: command_line_options.h:88
bdm::CommandLineOptions::sim_name_
std::string sim_name_
Definition: command_line_options.h:90
bdm
Definition: agent.cc:39
bdm::CommandLineOptions::AddOption
void AddOption(const std::string &opt, std::string def, const std::string &description="", std::string group="Simulation")
Add an extra command line option.
Definition: command_line_options.h:45
bdm::CommandLineOptions::parser_
cxxopts::ParseResult * parser_
Definition: command_line_options.h:95
bdm::CommandLineOptions::first_parse_
bool first_parse_
Definition: command_line_options.h:93
bdm::CommandLineOptions::Get
T Get(const std::string &val)
Definition: command_line_options.h:59
bdm::CommandLineOptions::CommandLineOptions
CommandLineOptions(int argc, const char **argv)
Definition: command_line_options.cc:27
bdm::CommandLineOptions::Parse
void Parse()
Parse the options with the given command line arguments.
Definition: command_line_options.cc:54
bdm::CommandLineOptions::ExtractSimulationName
void ExtractSimulationName(const char *path)
Definition: command_line_options.cc:121
simulation.h
bdm::CommandLineOptions::operator<<
friend std::ostream & operator<<(std::ostream &os, const CommandLineOptions &clo)
Definition: command_line_options.cc:212
bdm::CommandLineOptions::AddCoreOptions
void AddCoreOptions()
Definition: command_line_options.cc:100
bdm::CommandLineOptions::HandleCoreOptions
void HandleCoreOptions()
Takes care of core options.
Definition: command_line_options.cc:131
bdm::CommandLineOptions
Class to contain and parse command line options.
Definition: command_line_options.h:37
bdm::CommandLineOptions::~CommandLineOptions
~CommandLineOptions()
Definition: command_line_options.cc:35
bdm::CommandLineOptions::argc_
int argc_
Definition: command_line_options.h:87