-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd_parser.cc
More file actions
61 lines (50 loc) · 1010 Bytes
/
cmd_parser.cc
File metadata and controls
61 lines (50 loc) · 1010 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <initializer_list>
using c_str = const char *;
class Option
{
public:
template <typename T>
Option(char short_name, c_str long_name, T &data);
template <typename T>
Option(T &data);
private:
T &m_data;
};
class Parser
{
public:
Parser(std::initializer_list<Option> options);
void parse(int argc, const char *argv[]);
};
struct Point
{
int x;
int y;
};
std::istream &operator>>(std::istream &in, Point &p)
{
return in >> p.x >> p.y;
}
std::ostream &operator<<(std::ostream &out, Point p)
{
return out << p.x << ' ' << p.y;
}
int main(int argc, const char *argv[])
{
int i;
bool b;
std::string s;
Point p;
Parser{argc, argv}
.arg('i', "int", i)
.arg('b', "bool", b)
.arg('s', "string", s)
.arg(p)
;
std::cout << "i = " << i << '\n'
<< "b = " << b << '\n'
<< "s = " << s << '\n'
<< "p = " << p << '\n';
}