-
Notifications
You must be signed in to change notification settings - Fork 2
/
parseargv.cpp
84 lines (71 loc) · 1.4 KB
/
parseargv.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
74
75
76
77
78
79
80
81
82
83
84
#include <unistd.h>
#include <sstream>
#include <iostream>
#include <stdio.h>
void Usage()
{
std::cout << " hH?vVa:b:c::\n";
}
void Version()
{
std::cout << "verson:ver1.0\n";
}
template <class TI, class TO>
TO ToType(const TI& a)
{
std::stringstream ss;
ss << a;
TO b;
ss >> b;
return b;
}
bool InitArgv(int argc, char** argv)
{
int num = 3;
char format[] = "hH?vVa:b:c:";
/*
if (argc > 3)
{
std::cout <<"argc num wrong\n";
Usage();
return false;
}
*/
char c;
std::string s;
int b;
while ((c=getopt(argc, argv, format)) != EOF)
{
switch (c)
{
case 'h':
case 'H':
case '?':
Usage();
break;
case 'v':
case 'V':
Version();
break;
case 'a':
s = optarg;
std::cout <<"opt a:" << s <<std::endl;
break;
case 'b':
b = ToType<char*, int>(optarg);
std::cout <<"opt b:" << b<< std::endl;
break;
case 'c':
s=optarg;
std::cout <<"opt c:"<< s<< std::endl;
break;
default:
Usage();
}
}
}
int main(int argc, char** argv)
{
InitArgv(argc, argv);
return 0;
}