-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathparse-option.h
215 lines (191 loc) · 6.64 KB
/
parse-option.h
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
/* Created on 2016-09-14
* Author: Zhang Binbin
* About: cpp command argv parser,
*/
#ifndef PARSE_OPTION_H_
#define PARSE_OPTION_H_
#include <stdlib.h>
#include <assert.h>
#include <string>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <map>
class ParseOptions {
public:
ParseOptions(const char *usage): usage_(usage) {}
~ParseOptions() {};
int NumArgs() {
return args_.size();
}
std::string GetArg(int i) {
if (i < 1 || i > args_.size()) {
std::cerr << "Invalid arg index " << i << "\n";
exit(1);
}
return args_[i - 1];
}
int Read(int argc, const char *const *argv) {
argc_ = argc;
argv_ = argv;
std::string key, value;
int i = 0;
bool has_equal_sign;
// argv_[0], program name
for (i = 1; i < argc; i++) {
if (std::strncmp(argv[i], "--", 2) == 0) {
bool has_equal_sign;
SplitLongArg(argv[i], &key, &value, &has_equal_sign);
Trim(&value);
if (key.compare("help") == 0) {
PrintUsage();
exit(0);
}
SetOption(key, value, has_equal_sign);
}
else break;
}
// the left are standard argument
for (; i < argc; i++) {
args_.push_back(argv[i]);
}
}
void PrintUsage() {
std::cerr << "\n" << usage_ << "\n";
std::cerr << "Options:\n";
std::map<std::string, std::string>::iterator it = doc_map_.begin();
for (; it != doc_map_.end(); it++) {
std::cerr << " --" << std::setw(25) << std::left << it->first
<< " : " << it->second << '\n';
}
}
bool SetOption(const std::string &key,
const std::string &value,
bool has_equal_sign) {
if (bool_map_.end() != bool_map_.find(key)) {
if (has_equal_sign && value == "") {
std::cerr << "Invalid option --" << key << " =\n";
PrintUsage();
exit(1);
}
*(bool_map_[key]) = ToBool(value);
return true;
}
// otherwise has no value
if (!has_equal_sign) {
std::cerr << "Invalid option -- " << key << "\n";
PrintUsage();
exit(1);
}
if (int_map_.end() != int_map_.find(key)) {
// atoi is not reliable
*(int_map_[key]) = atoi(value.c_str());
} else if (float_map_.end() != float_map_.find(key)) {
// atof is not reliable
*(float_map_[key]) = (float)atof(value.c_str());
} else if (string_map_.end() != string_map_.find(key)) {
*(string_map_[key]) = value;
} else {
return false;
}
return true;
}
bool ToBool(std::string str) {
// allow "" as a valid option for "true", so that --x is the same as --x=true
if ((str.compare("true") == 0) || (str.compare("t") == 0)
|| (str.compare("1") == 0) || (str.compare("") == 0)) {
return true;
}
if ((str.compare("false") == 0) || (str.compare("f") == 0)
|| (str.compare("0") == 0)) {
return false;
}
std::cerr << "Invalid format for boolean argument [expected true or false]: "
<< str << "\n";
return false; // otherwise
}
// TODO, safe convert to int and float
int ToInt(std::string str) {
}
int ToFloat(std::string str) {
}
void SplitLongArg(std::string in, std::string *key,
std::string *value, bool *has_equal_sign) {
assert(in.substr(0, 2) == "--"); // precondition.
size_t pos = in.find_first_of('=', 0);
if (pos == std::string::npos) { // we allow --option for bools
// defaults to empty. We handle this differently in different cases.
*key = in.substr(2, in.size()-2); // 2 because starts with --.
*value = "";
*has_equal_sign = false;
} else if (pos == 2) { // we also don't allow empty keys: --=value
PrintUsage();
std::cerr << "Invalid option (no key): " << in;
exit(1);
} else { // normal case: --option=value
*key = in.substr(2, pos-2); // 2 because starts with --.
*value = in.substr(pos + 1);
*has_equal_sign = true;
}
}
void Trim(std::string *str) {
const char *white_chars = " \t\n\r\f\v";
std::string::size_type pos = str->find_last_not_of(white_chars);
if (pos != std::string::npos) {
str->erase(pos + 1);
pos = str->find_first_not_of(white_chars);
if (pos != std::string::npos) str->erase(0, pos);
} else {
str->erase(str->begin(), str->end());
}
}
// Methods from the interface
void Register(const std::string &name,
bool *ptr, const std::string &doc) {
assert(ptr != NULL);
bool_map_[name] = ptr;
std::ostringstream ss;
std::string b = (*ptr) ? "true" : "false";
ss << doc << " (bool, default = " << b << ")";
doc_map_[name] = ss.str();
}
void Register(const std::string &name,
int *ptr, const std::string &doc) {
assert(ptr != NULL);
int_map_[name] = ptr;
std::ostringstream ss;
ss << doc << " (int, default = " << *ptr << ")";
doc_map_[name] = ss.str();
}
void Register(const std::string &name,
float *ptr, const std::string &doc) {
assert(ptr != NULL);
float_map_[name] = ptr;
std::ostringstream ss;
ss << doc << " (float, default = " << *ptr << ")";
doc_map_[name] = ss.str();
}
void Register(const std::string &name,
std::string *ptr, const std::string &doc) {
assert(ptr != NULL);
string_map_[name] = ptr;
std::ostringstream ss;
ss << doc << " (string, default = " << *ptr << ")";
doc_map_[name] = ss.str();
}
private:
// maps for option variables
std::map<std::string, bool*> bool_map_;
std::map<std::string, int*> int_map_;
std::map<std::string, float*> float_map_;
std::map<std::string, std::string*> string_map_;
// document map
std::map<std::string, std::string> doc_map_;
const char *usage_;
int argc_;
const char *const *argv_;
std::vector<const char *> args_;
};
#endif