Newer
Older
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//============================================================================//
// A class to simplify the argument parsing procedure //
// //
// Chao Peng //
// 07/26/2017 //
//============================================================================//
#include "ConfigOption.h"
#include "ConfigParser.h"
#include <iostream>
#include <cstring>
ConfigOption::ConfigOption()
{
// place holder
}
ConfigOption::~ConfigOption()
{
// place holder
}
void ConfigOption::AddOpt(OptType type, char s_term)
{
AddOpt(type, s_term, s_term);
}
void ConfigOption::AddLongOpt(OptType type, const char *l_term)
{
AddLongOpt(type, l_term, *l_term);
}
void ConfigOption::AddOpts(OptType type, char s_term, const char *l_term)
{
AddOpts(type, s_term, l_term, s_term);
}
void ConfigOption::AddOpt(OptType type, char s_term, char mark)
{
if(s_opt_map.find(s_term) != s_opt_map.end()) {
std::cerr << "ConfigOption: Short option " << s_term
<< " has already been added, abort AddOpt."
<< std::endl;
return;
}
s_opt_map[s_term] = Opt(mark, type);
}
void ConfigOption::AddLongOpt(OptType type, const char *l_term, char mark)
{
if(l_opt_map.find(l_term) != l_opt_map.end()) {
std::cerr << "ConfigOption: Long option " << l_term
<< " has already been added, abort AddOpt."
<< std::endl;
return;
}
l_opt_map[l_term] = Opt(mark, type);
}
void ConfigOption::AddOpts(OptType type, char s_term, const char *l_term, char mark)
{
AddOpt(type, s_term, mark);
AddLongOpt(type, l_term, mark);
}
bool ConfigOption::parseLongOpt(const char *arg)
{
auto vars = ConfigParser::split(arg, strlen(arg), "=");
std::string key = std::move(vars.front());
vars.pop_front();
auto it = l_opt_map.find(key);
if(it == l_opt_map.end()) {
std::cerr << "Unknown option --" << key << std::endl;
return false;
}
switch(it->second.type)
{
case arg_require:
if(vars.empty()) {
std::cerr << "Lack of argument for option --" << key << std::endl;
return false;
}
opt_pack.emplace_back(it->second.mark, it->second.type, std::move(vars.front()));
break;
case arg_none:
opt_pack.emplace_back(it->second.mark, it->second.type);
break;
case help_message:
return false;
}
return true;
}
bool ConfigOption::parseShortOpt(char key, int argc, char *argv[], int &idx)
{
auto it = s_opt_map.find(key);
if(it == s_opt_map.end()) {
std::cerr << "Unknown option -" << key << std::endl;
return false;
}
switch(it->second.type)
{
case arg_require:
if((idx + 1) >= argc || *argv[idx + 1] == '-') {
std::cerr << "Lack of argument for option -" << key << std::endl;
return false;
}
opt_pack.emplace_back(it->second.mark, it->second.type, ConfigValue(argv[++idx]));
break;
case arg_none:
opt_pack.emplace_back(it->second.mark, it->second.type);
break;
case help_message:
return false;
}
return true;
}
bool ConfigOption::ParseArgs(int argc, char *argv[])
{
if (argc < 1)
return false;
argv0 = argv[0];
bool success = true;
arg_pack.clear();
opt_pack.clear();
for(int i = 1; i < argc; ++i)
{
char* ptr = argv[i];
// option
if(*ptr == '-') {
// long option
if(*(ptr + 1) == '-') {
success &= parseLongOpt(ptr + 2);
// short option
} else {
success &= parseShortOpt(*(ptr + 1), argc, argv, i);
}
// arguments
} else {
arg_pack.emplace_back(ptr);
}
}
return success;
}
void ConfigOption::SetDesc(const char *desc)
{
base_desc = desc;
}
void ConfigOption::SetDesc(char mark, const char *desc)
{
std::string option;
bool found_mark = false;
for(auto &it : s_opt_map)
{
if(it.second.mark != mark) continue;
option = " - ";
option.back() = it.first;
if(it.second.type == arg_require)
option += " <arg>";
option += ",";
found_mark = true;
}
for(auto &it : l_opt_map)
{
if(it.second.mark != mark) continue;
option += " --" + it.first;
if(it.second.type == arg_require)
option += "=<arg>";
option += ",";
found_mark = true;
}
if(!found_mark) {
std::cerr << "Config Option: Cannot find any option with mark <"
<< mark << ">, abort adding description." << std::endl;
return;
}
option.back() = ':';
option += " ";
option += desc;
option_desc.emplace_back(option);
}
std::string ConfigOption::GetInstruction()
{
std::string res = base_desc + "\noptions:\n";
auto pvar = res.find("%0");
if (pvar != std::string::npos) {
res.replace(pvar, 2, argv0);
}
for(auto &option : option_desc)
{
res += "\t" + option + "\n";
}
return res;
}