-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_param.h
More file actions
31 lines (25 loc) · 908 Bytes
/
parse_param.h
File metadata and controls
31 lines (25 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef PARSE_PARAM_H
#define PARSE_PARAM_H
#include <algorithm>
#include <iostream>
#include <sstream>
// https://stackoverflow.com/questions/865668/how-to-parse-command-line-arguments-in-c
template <typename T>
void parse_param(int argc, char ** argv, std::string const & option, T & result, std::string const & err_msg) {
char ** end = argv + argc;
char ** itr = std::find(argv, end, option);
if ((itr != end) && (++itr != end)) {
std::stringstream is(*itr);
is >> std::skipws;
is >> result;
} else {
std::cerr << "Error: " << err_msg << std::endl;
exit(EXIT_FAILURE);
}
}
void parse_param(int argc, char ** argv, std::string const & option, bool & result) {
char ** end = argv + argc;
char ** itr = std::find(argv, end, option);
result = (itr != end);
}
#endif // PARSE_PARAM_H