-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
58 lines (53 loc) · 1.61 KB
/
main.cpp
File metadata and controls
58 lines (53 loc) · 1.61 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
#include <getopt.h>
#include <iostream>
#include <stdlib.h>
#include <string>
void handleoptionWithoutArguments(std::string optionName) {
std::cout << "Option:" << optionName << std::endl;
}
void handleoptionWithArguments(std::string optionName, char* args) {
std::cout << "Option:" << optionName << ", arguments: ";
std::string arguments = args;
if (arguments.find(",") == std::string::npos) {
std::cout << arguments << std::endl;
}
else {
int last_pos = 0;
size_t pos = 0;
while ((pos = arguments.find(",")) != std::string::npos) {
std::string token = arguments.substr(0, pos);
std::cout << token << " ";
arguments.erase(0, pos + 1);
}
std::cout << arguments << std::endl;
}
}
int main(int argc, char **argv) {
while (true) {
int option_index = 0;
static option long_options[] = {
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"list", required_argument, 0, 'l'},
{0, 0, 0, 0}
};
int option = getopt_long(argc, argv, "hvl:", long_options, nullptr);
if (option == -1) {
break;
}
switch (option) {
case 'h':
handleoptionWithoutArguments("Help");
break;
case 'v':
handleoptionWithoutArguments("Version");
break;
case 'l':
handleoptionWithArguments("List", optarg);
break;
default:
break;
}
}
return 0;
}