-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.cpp
42 lines (34 loc) · 986 Bytes
/
args.cpp
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
#include <cstring>
#include "args.hpp"
/*
Parses argv for options below.
Options:
--port -p Port number. Assigned to PORT.
--dir -d Directory to serve files from. Assigned to SERVE_DIR.
*/
void parse_args(int argc, char* argv[], int &port,
string &serve_dir) {
port = DEFAULT_PORT;
serve_dir = DEFAULT_DIR;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--port") == 0 || strcmp(argv[i], "-p") == 0) {
if (i+1 >= argc)
throw ArgError();
string value(argv[i+1]);
try {
port = stoi(value);
} catch (...) {
throw ArgError();
}
i++;
}
else if (strcmp(argv[i], "--dir") == 0 || strcmp(argv[i], "-d") == 0) {
if (i+1 >= argc)
throw ArgError();
serve_dir = string(argv[i+1]);
i++;
}
else
throw ArgError();
}
}