XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
command_line_inputs.hpp
Go to the documentation of this file.
1 #ifndef COMMAND_LINE_INPUTS_HPP
2 #define COMMAND_LINE_INPUTS_HPP
3 
4 #include <stdexcept>
5 #include <functional>
6 #include <vector>
7 #include <string>
8 
9 template<typename T>
10 static T read_arg_after(int argc, char* argv[], int i, int* ierr);
11 
12 template<>
13 int read_arg_after(int argc, char* argv[], int i, int* ierr){
14  *ierr = 0;
15  if (i + 1 < argc) { // Make sure we aren't at the end of argv!
16  std::string arg = argv[i+1];
17  try {
18  std::size_t pos;
19  int x = std::stoi(arg, &pos);
20  if (pos >= arg.size()) {
21  return x;
22  }
23  } catch(...){
24  //
25  }
26  }
27  *ierr = 1;
28  return -1;
29 }
30 
32  public:
35  bool do_dryrun;
37 
38  private:
39  struct Option{
40  std::string name;
41  std::string explanation;
42  Option(std::string name, std::string explanation) : name(name), explanation(explanation) {}
43  };
44  std::vector<Option> options;
45 
47  bool found;
48 
49  bool check(std::string input, std::string name, std::string explanation){
50  options.push_back(Option(name, explanation));
51  if (input==name){
52  found=true;
53  return true;
54  }else{
55  return false;
56  }
57  };
58 
59  void print_options(){
60  std::string msg = "Available options are:\n";
61  for(int j = 0; j < options.size(); j++)
62  msg += "\t" + options[j].name + "\t" + options[j].explanation + "\n";
63  printf("%s", msg.c_str());
64  }
65 
66  void error(std::string msg){
67  printf("ERROR: %s\n", msg.c_str());
68  print_options();
69  exit(1);
70  }
71 
72  public:
73 
74  CommandLineInputs(int argc, char* argv[])
75  : run_particle_test(false),
76  update_particle_test(false),
77  do_dryrun(false)
78  {
79  int ierr;
80  first_input = true;
81  // Loop through inputs
82  for (int i = 1; i < argc; ++i) {
83  std::string input = std::string(argv[i]);
84  found = false;
85 
86  if(check(input,"--test","Checks particles against reference data")){
87  run_particle_test=true;
88  }
89  if(check(input,"--update-test","Updates reference data used in particle test")){
91  }
92  if(check(input,"--dryrun","Partially runs XGC setup to check for some common input errors")){
93  do_dryrun=true;
94  dryrun_n_mpi_ranks = -1; // default
95  }
96  if(check(input,"-n_ranks","Optional input if using --dryrun: Specify the number of MPI ranks you plan to use")){
97  if(!do_dryrun){
98  error("-n_ranks can only be used with --dryrun");
99  }
100  dryrun_n_mpi_ranks = read_arg_after<int>(argc, argv, i, &ierr);
101  if(ierr!=0) error("Expected integer after -n_ranks");
102  i++;
103  }
104  if(!found){
105  error("Unknown command line argument " + input);
106  }
107  first_input = false;
108  }
109  }
110 };
111 
112 #endif
std::string name
Definition: command_line_inputs.hpp:40
static T read_arg_after(int argc, char *argv[], int i, int *ierr)
Definition: command_line_inputs.hpp:13
bool run_particle_test
Definition: command_line_inputs.hpp:33
bool first_input
Definition: command_line_inputs.hpp:46
bool update_particle_test
Definition: command_line_inputs.hpp:34
Option(std::string name, std::string explanation)
Definition: command_line_inputs.hpp:42
Definition: command_line_inputs.hpp:31
void print_options()
Definition: command_line_inputs.hpp:59
CommandLineInputs(int argc, char *argv[])
Definition: command_line_inputs.hpp:74
std::string explanation
Definition: command_line_inputs.hpp:41
Option
Definition: streamed_parallel_for.hpp:13
bool check(std::string input, std::string name, std::string explanation)
Definition: command_line_inputs.hpp:49
void error(std::string msg)
Definition: command_line_inputs.hpp:66
bool do_dryrun
Definition: command_line_inputs.hpp:35
std::vector< Option > options
Definition: command_line_inputs.hpp:44
int dryrun_n_mpi_ranks
Definition: command_line_inputs.hpp:36
bool found
Definition: command_line_inputs.hpp:47
Definition: command_line_inputs.hpp:39