Stellarator-Tools
vmec_test_commandline_parser.hpp
Go to the documentation of this file.
1 //******************************************************************************
4 //******************************************************************************
5 
6 #ifndef vmec_test_commandline_parser_hpp
7 #define vmec_test_commandline_parser_hpp
8 
9 #include <map>
10 #include <string>
11 #include <sstream>
12 #include <functional>
13 
14 namespace vmec_test {
15 
17  typedef std::map<std::string, std::string> arg_map;
19  typedef std::pair<std::string, std::string> arg_element;
20 
21 //------------------------------------------------------------------------------
23 //------------------------------------------------------------------------------
25  public:
29  const std::function<void(void)> help;
30 
31 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39  static arg_map parse_commands(const size_t argc,
40  const char * argv[],
41  const std::function<void(void)> help) {
42  if (argc == 0) {
43  help();
44  }
45 
47 
48  for (size_t i = 1; i < argc; i++) {
49  std::string arg(argv[i]);
50 
51  if (arg == "-h") {
52  help();
53  } else {
54  size_t eqpos = arg.find('=');
55  if (eqpos != std::string::npos) {
56  std::string key = arg.substr(0, eqpos);
57  std::string value = arg.substr(eqpos + 1, std::string::npos);
58 
59  commands.insert(arg_element(key, value));
60  } else {
61  commands.insert(arg_element(arg, ""));
62  }
63  }
64  }
65 
66  return commands;
67  }
68 
69 //------------------------------------------------------------------------------
76 //------------------------------------------------------------------------------
77  commandline_parser(const size_t argc,
78  const char * argv[],
79  const std::function<void(void)> help) :
80  commands(parse_commands(argc, argv, help)) {}
81 
82 //------------------------------------------------------------------------------
87 //------------------------------------------------------------------------------
88  bool is_set(const std::string &key) const {
89  return commands.find(key) != commands.end();
90  }
91 
92 //------------------------------------------------------------------------------
97 //------------------------------------------------------------------------------
98  template<typename TYPE>
99  TYPE get(const std::string &key) const {
100  if (!is_set(key)) {
101  help();
102  }
103 
104  std::stringstream value_stream(commands.at(key));
105  TYPE temp;
106  value_stream >> temp;
107 
108  return temp;
109  }
110  };
111 }
112 
113 #endif
commandline_parser(const size_t argc, const char *argv[], const std::function< void(void)> help)
Construct a commandline_parser object by pasring command line arguments.
Definition: vmec_test_commandline_parser.hpp:77
TYPE get(const std::string &key) const
Get the value of the agument.
Definition: vmec_test_commandline_parser.hpp:99
static arg_map parse_commands(const size_t argc, const char *argv[], const std::function< void(void)> help)
Factory method to parse the commandline and produce the arguments.
Definition: vmec_test_commandline_parser.hpp:39
const std::function< void(void)> help
Help callback function.
Definition: vmec_test_commandline_parser.hpp:29
bool is_set(const std::string &key) const
Check if command arg was set.
Definition: vmec_test_commandline_parser.hpp:88
const arg_map commands
Parsed commands.
Definition: vmec_test_commandline_parser.hpp:27
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
std::map< std::string, std::string > arg_map
Type for the argument map.
Definition: vmec_test_commandline_parser.hpp:17