-
Notifications
You must be signed in to change notification settings - Fork 58
/
parse_command_line.cc
170 lines (144 loc) · 5.02 KB
/
parse_command_line.cc
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// This file gives a small tour of the iod::parse_command_line features.
// (and also test it)
//
#define IOD_PCL_WITH_EXCEPTIONS
// symbols.hh contains the definitions of all _xxx variables.
// it is generated by running this command "iod_generate_symbols this_file.cc symbols.hh"
#include "symbols.hh"
// The command line parser.
#include "iod/parse_command_line.hh"
int main()
{
using namespace s;
using namespace iod;
// Simple options
{
const char* argv[] = {"", "--opt1" , "12", "--opt2", "abc"};
auto opts = parse_command_line(5, argv,
_opt1 = int(),
_opt2 = std::string());
assert(opts.opt1 == 12 and opts.opt2 == "abc");
}
// Positionals
{
const char* argv[] = {"", "abc", "1.23", "--opt1" , "12"};
auto opts = parse_command_line(5, argv,
cl::positionals(_opt2, _opt3),
_opt1 = int(),
_opt2 = std::string(),
_opt3 = float());
assert(opts.opt1 == 12 and opts.opt2 == "abc" and std::abs(opts.opt3 - 1.23) < 0.00001);
}
// Defaults
{
const char* argv[] = {""};
auto opts = parse_command_line(1, argv,
_opt1 = int(3),
_opt2 = std::string("abc"));
assert(opts.opt1 == 3 and opts.opt2 == "abc");
}
// Pointers
{
const char* argv[] = {"", "--opt1", "42"};
int opt1 = 0;
parse_command_line(3, argv,
_opt1 = &opt1);
assert(opt1 == 42);
}
// Switches
{
const char* argv[] = {"", "--opt1", "0", "-abc", "-d=0"};
auto opts = parse_command_line(4, argv,
_opt1 = bool(),
_a = bool(),
_b = bool(),
_c = bool(),
_d = bool());
assert(opts.opt1 == false and
opts.a == true and
opts.b == true and
opts.c == true and
opts.d == false);
}
// equals option assignments
{
const char* argv[] = {"", "--opt1=43", "-a=23"};
auto opts = parse_command_line(3, argv,
_opt1 = int(),
_a = int(3));
assert(opts.opt1 == 43 and opts.a == 23);
}
// shortcuts names
{
const char* argv[] = {"", "-1" , "3", "-2", "abc"};
auto opts = parse_command_line(5, argv,
_opt1 | _1 = int(),
_opt2 | _2 = std::string());
assert(opts.opt1 == 3 and opts.opt2 == "abc");
}
// vectors
{
const char* argv[] = {"", "-a", "1", "-a", "2", "-a", "3", "-a", "4" };
auto opts = parse_command_line(9, argv, _a = std::vector<int>());
assert(opts.a.size() == 4 and
opts.a[0] == 1 and
opts.a[1] == 2 and
opts.a[2] == 3 and
opts.a[3] == 4);
}
// required args
{
const char* argv[] = {"", "-opt1" , "3"};
std::string err;
try
{
auto opts = parse_command_line(3, argv,
cl::required(_opt1, _opt2),
_opt1 | _1 = int(),
_opt2 | _2 = std::string());
}
catch (std::runtime_error &e)
{
err = e.what();
};
assert(err == "Error missing required command line parameter opt2\n");
}
// Description.
{
const char* argv[] = {"./test_program", "--help"};
try
{
auto opts = parse_command_line(2, argv,
cl::required(_opt1, _a),
cl::positionals(_opt1, _opt2),
cl::description(
"This is a test program",
_opt1 = "Set the first option of our test program.\n second line",
_opt2 = "Set the second option of our test program.",
_opt3 = "A vector of strings."),
_opt1 | _1 = int(),
_opt2 | _2 = std::string(),
_opt3 = std::vector<std::string>(),
_a = bool(),
_b = bool(),
_c = bool(),
_d = bool()
);
// This should print:
// Usage: ./test_program [options...] [opt1] [opt2]
// This is a test program
// --opt1|-1 int [REQUIRED] Set the first option of our test program.
// second line
// --opt2|-2 string Set the second option of our test program.
// --opt3 vector<string> A vector of strings.
// -a [REQUIRED]
// -b
// -c
// -d
}
catch(...)
{
}
}
}