-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessOptions.cpp
More file actions
57 lines (48 loc) · 1.34 KB
/
ProcessOptions.cpp
File metadata and controls
57 lines (48 loc) · 1.34 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
#include "ProcessOptions.hpp"
#include <cstdlib>
#include <iostream>
#include <getopt.h>
ProcessOptions::ProcessOptions(int argc, char **argv)
{
const char *const short_opts = "hi:p:f:";
const option long_opts[] = {
{ "help", 0, nullptr, 'h' },
{ "ip", 0, nullptr, 'i' },
{ "port", 1, nullptr, 'p' },
{ "file", 0, nullptr, 'f' },
{ nullptr, 0, nullptr, 0 }
};
while (true)
{
if(argc < 2)
printHelp();
const auto opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
if (-1 == opt)
break;
switch (opt)
{
case 'p':
_options["port"] = std::string(optarg);
break;
case 'i':
_options["ip"] = std::string(optarg);
break;
case 'f':
_options["file"] = std::string(optarg);
break;
case 'h':
case '?': // Unrecognized option
default:
printHelp();
break;
}
}
}
void ProcessOptions::printHelp()
{
std::cout << " -h --help Display this usage information.\n"
" -f --file Persisted file name.\n"
" -i --ip Set IP address.\n"
" -p --port Set port.\n";
std::exit(1);
}