Stellarator-Tools
commandline_parser.hpp
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // The @header2, @begin_table, @item3 and @end_table commands are custom
3 // defined commands in Doxygen.in. They are defined under ALIASES. For the page
4 // created here, the 80 column limit is exceeded. Arguments of aliases are
5 // separated by ','. If you intended ',' to be a string you must use an escaped
6 // comma '\,'.
7 //
67 //------------------------------------------------------------------------------
68 //******************************************************************************
71 //******************************************************************************
72 
73 #ifndef commandline_parser_hpp
74 #define commandline_parser_hpp
75 
76 #include <iostream>
77 #include <map>
78 #include <string>
79 #include <sstream>
80 
82 typedef std::map<std::string, std::string> arg_map;
84 typedef std::pair<std::string, std::string> arg_element;
85 
86 //------------------------------------------------------------------------------
88 //------------------------------------------------------------------------------
90 public:
93 
94 //------------------------------------------------------------------------------
100 //------------------------------------------------------------------------------
101  static arg_map parse_commands(const size_t argc, const char * argv[]) {
102  if (argc == 0) {
103  help();
104  }
105 
107 
108 
109  for (size_t i = 1; i < argc; i++) {
110  std::string arg(argv[i]);
111 
112  if (arg == "-h") {
113  help();
114  } else {
115  size_t eqpos = arg.find('=');
116  if (eqpos != std::string::npos) {
117  std::string key = arg.substr(0, eqpos);
118  std::string value = arg.substr(eqpos + 1, std::string::npos);
119 
120  commands.insert(arg_element(key, value));
121  } else {
122  commands.insert(arg_element(arg, ""));
123  }
124  }
125  }
126 
127  return commands;
128  }
129 
130 //------------------------------------------------------------------------------
136 //------------------------------------------------------------------------------
137  commandline_parser(const size_t argc, const char * argv[]) :
138  commands(parse_commands(argc, argv)) {}
139 
140 //------------------------------------------------------------------------------
145 //------------------------------------------------------------------------------
146  bool is_set(const std::string &key) const {
147  return commands.find(key) != commands.end();
148  }
149 
150 //------------------------------------------------------------------------------
155 //------------------------------------------------------------------------------
156  template<typename TYPE>
157  TYPE get(const std::string &key) const {
158  if (!is_set(key)) {
159  help();
160  }
161 
162  std::stringstream value_stream(commands.at(key));
163  TYPE temp;
164  value_stream >> temp;
165 
166  return temp;
167  }
168 
169 //------------------------------------------------------------------------------
171 //------------------------------------------------------------------------------
172  static void help() {
173 // " '' "
174  std::cout << " " << std::endl;
175  std::cout << " SIESTA TEST " << std::endl;
176  std::cout << " " << std::endl;
177  std::cout << "Usage: xsiesta_test [-arg][=option] ... " << std::endl;
178  std::cout << " " << std::endl;
179  std::cout << "Options: " << std::endl;
180  std::cout << "All options are displayes as [arg][takesoption][Discription] " << std::endl;
181  std::cout << " -h N Display this information. " << std::endl;
182  std::cout << " " << std::endl;
183  std::cout << " -wout_file Y Path to the VMEC wout file. " << std::endl;
184  std::cout << " " << std::endl;
185  std::cout << " -restart_file Y Path to the SIESTA restart file. " << std::endl;
186  std::cout << " " << std::endl;
187  std::cout << " -test_name Y Name of the test to run. " << std::endl;
188  std::cout << " " << std::endl;
189  std::cout << " -tol Y Tolarance value. " << std::endl;
190  std::cout << " " << std::endl;
191  std::cout << " -min Y Minimum radial value. " << std::endl;
192  std::cout << " " << std::endl;
193  std::cout << " -max Y Maximum radial value. " << std::endl;
194  std::cout << " " << std::endl;
195  std::cout << " -relative N Use relative error. " << std::endl;
196  std::cout << " " << std::endl;
197  std::cout << " -dump N Dump values to the screen. " << std::endl;
198  std::cout << " " << std::endl;
199  std::cout << " -u Y Poloidal angle to dump. " << std::endl;
200  std::cout << " " << std::endl;
201  std::cout << " -v Y Toroidal angle to dump.. " << std::endl;
202  std::cout << " " << std::endl;
203  std::cout << std::endl;
204 
205  exit(0);
206  }
207 };
208 
209 #endif
static arg_map parse_commands(const size_t argc, const char *argv[])
Factory method to parse the commandline and produce the arguments.
Definition: commandline_parser.hpp:101
bool is_set(const std::string &key) const
Check if command arg was set.
Definition: commandline_parser.hpp:146
commandline_parser(const size_t argc, const char *argv[])
Construct a commandline_parser object by pasring command line arguments.
Definition: commandline_parser.hpp:137
TYPE get(const std::string &key) const
Get the value of the agument.
Definition: commandline_parser.hpp:157
static void help()
Display help.
Definition: commandline_parser.hpp:172
const arg_map commands
Parsed commands.
Definition: commandline_parser.hpp:92
std::pair< std::string, std::string > arg_element
Type for a key value pair.
Definition: commandline_parser.hpp:84
std::map< std::string, std::string > arg_map
Type for the argument map.
Definition: commandline_parser.hpp:82
Defines the base class of the type commandline_parser_class.
Definition: commandline_parser.f:65