-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionParser.cpp
More file actions
189 lines (165 loc) · 4.35 KB
/
OptionParser.cpp
File metadata and controls
189 lines (165 loc) · 4.35 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
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
/* This code is released into the public domain,
* WITHOUT WARRANTY OF ANY KIND.
*/
#include "OptionParser.hpp"
#include <iostream>
#include <sstream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdarg>
#ifndef NDEBUG
#include <tr1/unordered_set>
#endif
namespace {
#ifndef NDEBUG
static bool has_ambiguous_flags(const OptionParser::FlagSpec *specs, int numspecs) {
char shortflags[256];
std::tr1::unordered_set<std::string> longflags;
std::memset(shortflags, 0, 256);
for (int i = 0; i < numspecs; ++i) {
const OptionParser::FlagSpec &spec = specs[i];
if (spec.short_flags) {
for (const char *c = spec.short_flags; *c; ++c) {
int cid = *c;
if (shortflags[cid]) { return true; }
++shortflags[cid];
}
}
if (spec.long_flag) {
std::string s(spec.long_flag);
if (longflags.count(s)) { return true; }
longflags.insert(std::move(s));
}
}
return false;
}
#endif
} // annoymous namespace
void OptionParser::print_usage(const OptionParser::FlagSpec *specs, std::ostream &ss, const char *progname, const char *description) {
assert(specs && progname);
ss << "usage: " << progname << " [options] inputs\n";
if (description) {
ss << "\n" << description;
}
if (specs->short_flags || specs->long_flag) {
ss << "\nOptions:\n";
for (const OptionParser::FlagSpec *s = specs; s->short_flags || s->long_flag; ++s) {
if (! s->help)
continue;
ss << " ";
if (s->short_flags && *s->short_flags) {
ss << "-" << *s->short_flags << (s->metaname ? s->metaname : "");
for (const char *c = s->short_flags + 1; *c; ++c) {
ss << ", -" << *c << (s->metaname ? s->metaname : "");
}
if (s->long_flag && *s->long_flag)
ss << ", ";
}
if (s->long_flag && *s->long_flag) {
ss << "--" << s->long_flag;
if (s->metaname)
ss << "=" << s->metaname;
}
ss << "\n";
ss << " " << s->help << "\n\n";
}
}
}
OptionParser::OptionParser(const OptionParser::FlagSpec *specs, int argc, char **argv):
specs(specs), argc(argc), argv(argv) {
assert(specs && argc >= 0 && argv);
numspecs = 0;
while (specs[numspecs].short_flags || specs[numspecs].long_flag)
++numspecs;
#ifndef NDEBUG
assert(! has_ambiguous_flags(specs, numspecs));
#endif
x = y = 0;
remain = 0;
flag_idx = -1;
arg_value = 0;
}
OptionParser::~OptionParser() {
}
void OptionParser::print_usage(std::ostream &ss, const char *description) {
OptionParser::print_usage(specs, ss, argv[0], description);
}
int OptionParser::next() {
while (remain || (x < argc)) {
char *flagtext;
char *value = 0;
int flagidx = -1;
if (remain) {
// short flag
flagtext = remain - 1;
flagtext[0] = '-';
value = flagtext + 2;
if (!*value) { value = 0; }
flagidx = match_short_flag(flagtext[1]);
if (!*++remain) { remain = 0; }
} else {
flagtext = argv[x++];
if (flagtext[0] == '-' && flagtext[1] != '\0') {
if (flagtext[1] == '-') {
if (flagtext[2] == '\0') { break; } // end of flags
// long flag
value = std::strchr(flagtext+2, '=');
if (value) { *value++ = '\0'; }
flagidx = match_long_flag(flagtext+2);
} else {
// short flags
remain = flagtext+1;
continue;
}
} else {
// positional argument
if (flagtext[0] != '\0') argv[y++] = flagtext;
continue;
}
}
if (flagidx < 0) {
std::ostringstream ss;
ss << "unknown flag '" << flagtext << "'";
throw UnknownFlag(flagtext, ss.str());
}
if (specs[flagidx].metaname) {
if (!value)
value = (x < argc ? argv[x++] : 0);
if (!value) {
std::ostringstream ss;
ss << "expected argument for flag '" << flagtext << "'";
throw ExpectedArg(flagtext, ss.str());
}
remain = 0;
} else
value = 0;
arg_value = value;
flag_idx = flagidx;
return specs[flagidx].id;
}
while (x < argc) argv[y++] = argv[x++];
x = y;
while (x < argc) argv[x++] = 0;
return -1;
}
const char *OptionParser::arg() const {
return arg_value;
}
int OptionParser::arg_count() const {
return y;
}
int OptionParser::match_long_flag(const char *flag) {
for (int i = 0; i < numspecs; ++i) {
const FlagSpec &s = specs[i];
if (s.long_flag && !std::strcmp(s.long_flag, flag)) { return i; }
}
return -1;
}
int OptionParser::match_short_flag(char f) {
for (int i = 0; i < numspecs; ++i ) {
const FlagSpec &s = specs[i];
if (s.short_flags && std::strchr(s.short_flags, f)) { return i; }
}
return -1;
}