-
Notifications
You must be signed in to change notification settings - Fork 0
/
getopt.cpp
74 lines (65 loc) · 1.27 KB
/
getopt.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
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
#include <unistd.h>
#include <iostream>
#include <string>
#define VERSION_INFO "0.0.1"
using namespace std;
void show_help(char *prog_name)
{
cout << prog_name << endl;
cout << "-h\tDisplay this help message and exit\n";
cout << "-v\tDisplay version info and exit\n";
cout << "-o[VALUE]\tAn output file\n";
cout << "-n VALUE\tA name\n";
exit(EXIT_SUCCESS);
}
void show_version()
{
cout << VERSION_INFO << endl;
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
char opt;
bool have_o = false, have_n = false;
string o_value, n_value;
while ((opt = getopt(argc, argv, "hvn:o::")) != -1)
{
switch (opt)
{
case 'h':
show_help(argv[0]);
break;
case 'v':
show_version();
break;
case 'o':
have_o = true;
if(optarg)
{
o_value = optarg;
}
break;
case 'n':
if(optarg)
{
have_n = true;
n_value = optarg;
}
else
{
cerr << "No argument for -n!";
exit(EXIT_FAILURE);
}
break;
}
}
if(have_o)
{
cout << "O's value " << o_value << endl;
}
if(have_n)
{
cout << "N's value " << n_value << endl;
}
return EXIT_SUCCESS;
}