forked from taganaka/SpeedTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CmdOptions.h
86 lines (75 loc) · 2.59 KB
/
CmdOptions.h
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
//
// Created by Francesco Laurita on 9/9/16.
//
#ifndef SPEEDTEST_CMDOPTIONS_H
#define SPEEDTEST_CMDOPTIONS_H
#include <getopt.h>
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;
std::string selected_server = "";
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' },
{"test-server", required_argument, 0, 't' },
{"output", required_argument, 0, 'o' },
{0, 0, 0, 0 }
};
const char *optStr = "hldusiqt: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 '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