-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCmdOptions.h
More file actions
139 lines (129 loc) · 4.57 KB
/
Copy pathCmdOptions.h
File metadata and controls
139 lines (129 loc) · 4.57 KB
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
#ifndef SPEEDTEST_CMDOPTIONS_H
#define SPEEDTEST_CMDOPTIONS_H
#include <getopt.h>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
enum OutputType { verbose, text, json };
typedef struct program_options_t {
bool help = false;
bool latency = false;
bool download = false;
bool upload = false;
bool share = false;
bool insecure = false;
bool select = false;
bool no_select = false;
bool list = false;
bool last = false;
bool yandex = false;
bool history = false;
std::string selected_server = "";
std::string city = "";
size_t list_count = 25;
size_t history_count = 10;
OutputType output_type = OutputType::verbose;
} ProgramOptions;
static struct option CmdLongOptions[] = {
{"help", no_argument, 0, 'h' },
{"latency", no_argument, 0, 'l' },
{"download", no_argument, 0, 'd' },
{"upload", no_argument, 0, 'u' },
{"share", no_argument, 0, 's' },
{"insecure", no_argument, 0, 'i' },
{"select", no_argument, 0, 'S' },
{"no-select", no_argument, 0, 'n' },
{"list", optional_argument, 0, 'L' },
{"last", no_argument, 0, 'y' },
{"yandex", no_argument, 0, 'Y' },
{"history", optional_argument, 0, 'H' },
{"city", required_argument, 0, 'c' },
{"test-server", required_argument, 0, 't' },
{"output", required_argument, 0, 'o' },
{0, 0, 0, 0 }
};
const char *optStr = "hldusiqSnyYL::H::c:t:o:";
bool ParseOptions(const int argc, const char **argv, ProgramOptions& options){
int long_index =0;
int opt = 0;
while ((opt = getopt_long(argc, (char **)argv, optStr, CmdLongOptions, &long_index )) != -1) {
switch (opt){
case 'h':
options.help = true;
break;
case 'l':
options.latency = true;
break;
case 'd':
options.download = true;
break;
case 'u':
options.upload = true;
break;
case 's':
options.share = true;
break;
case 'i':
options.insecure = true;
break;
case 'S':
options.select = true;
break;
case 'n':
options.no_select = true;
break;
case 'L':
options.list = true;
if (optarg) {
long count = strtol(optarg, nullptr, 10);
if (count < 1) {
std::cerr << "--list expects a positive number of servers" << std::endl;
return false;
}
options.list_count = static_cast<size_t>(count);
}
break;
case 'y':
options.last = true;
break;
case 'Y':
options.yandex = true;
break;
case 'H':
options.history = true;
if (optarg) {
long count = strtol(optarg, nullptr, 10);
if (count < 1) {
std::cerr << "--history expects a positive number of results" << std::endl;
return false;
}
options.history_count = static_cast<size_t>(count);
}
break;
case 'c':
options.city.append(optarg);
break;
case 't':
options.selected_server.append(optarg);
break;
case 'o':
if (strcmp(optarg, "verbose") == 0)
options.output_type = OutputType::verbose;
else if (strcmp(optarg, "text") == 0)
options.output_type = OutputType::text;
else if (strcmp(optarg, "json") == 0)
options.output_type = OutputType::json;
else {
std::cerr << "Unsupported output type " << optarg << std::endl;
std::cerr << "Supported output type: default, text, json" <<std::endl;
return false;
}
break;
default:
return false;
}
}
return true;
}
#endif //SPEEDTEST_CMDOPTIONS_H