-
Notifications
You must be signed in to change notification settings - Fork 0
/
opts.c++
216 lines (198 loc) · 6.25 KB
/
opts.c++
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
#include "opts.h"
#include <unistd.h>
#include <algorithm>
#include <iostream>
#include "io.h"
static_assert(
std::is_sorted(
std::begin(ArgParser::long_opts), std::end(ArgParser::long_opts),
[](auto a, auto b) {
return std::get<0>(a) < std::get<0>(b);
}), "long_opts must be sorted");
static_assert(
std::all_of(
std::begin(ArgParser::short_opts), std::end(ArgParser::short_opts),
[](auto v) {
return std::visit([](auto&& f) { return f != nullptr; }, v);
}), "missing short_opts func");
[[noreturn]] void usage(std::string_view argv0) {
mPrintLn(std::cerr, "usage: {} [options] <pattern> [path ...]", argv0);
mPrintLn(
std::cerr,
"\nRecursively search for pattern in path.\n"
"Uses the re2 regular expression library.\n\n"
"Options:\n"
" -A --after-context <num> Show num lines of context after each match\n"
" -B --before-context <num>\n"
" Show num lines of context before each match\n"
" -C --context <num> Show num lines before and after each match\n"
" -c --count Show count of matches only\n"
" -l --files-with-matches Only print filenames that contain matches\n"
" (don't print the matching lines)\n"
" --long-lines Print long lines (default truncates to ~2k)\n"
" -Q --literal Match pattern as literal, not regexp\n"
" -h --help Print this usage message and exit.\n"
" --version Print the program version.");
exit(2);
}
namespace {
constexpr std::pair<std::string_view, ArgParser::func>
lookup_long_opt(std::string_view opt) {
auto it = std::lower_bound(
std::begin(ArgParser::long_opts), std::end(ArgParser::long_opts), opt,
[](auto a, auto b) {
return std::get<0>(a) < b;
});
if (it != std::end(ArgParser::long_opts)
&& std::get<0>(*it).starts_with(opt)) {
if (it + 1 != std::end(ArgParser::long_opts)
&& std::get<0>(*(it + 1)).starts_with(opt)) {
throw ArgumentError{"ambiguous option --{}", opt};
}
return *it;
}
throw ArgumentError{"unrecognized option --{}", opt};
}
void swap_portions(char const* argv[], int* first_nonopt, int* last_nonopt,
int optind) {
int bottom = *first_nonopt;
int middle = *last_nonopt;
int top = optind;
while (top > middle && middle > bottom) {
if (top - middle > middle - bottom) {
std::swap_ranges(argv + bottom, argv + middle,
argv + top - (middle - bottom));
top -= (middle - bottom);
}
else {
std::swap_ranges(argv + bottom, argv + bottom + (top - middle),
argv + middle);
bottom += (top - middle);
}
}
*first_nonopt += (optind - *last_nonopt);
*last_nonopt = optind;
}
} // namespace
void ArgParser::parse_args(const int argc, char const* argv[], Opts& opts) {
opts.argv0 = *argv;
opts.stdout_is_tty = isatty(fileno(stdout));
int optind = 1;
int first_nonopt = 1;
int last_nonopt = 1;
while (true) {
if (first_nonopt != last_nonopt && last_nonopt != optind) {
swap_portions(argv, &first_nonopt, &last_nonopt, optind);
}
else if (last_nonopt != optind) {
first_nonopt = optind;
}
std::string_view opt;
while (optind < argc && !(opt = argv[optind]).starts_with('-')) {
++optind;
}
last_nonopt = optind;
if (optind != argc && opt == "--") {
++optind;
if (first_nonopt != last_nonopt && last_nonopt != optind) {
swap_portions(argv, &first_nonopt, &last_nonopt, optind);
}
else if (first_nonopt == last_nonopt) {
first_nonopt = optind;
}
last_nonopt = argc;
optind = argc;
}
if (optind == argc) {
if (first_nonopt != last_nonopt) {
optind = first_nonopt;
}
break;
}
opt.remove_prefix(1);
if (opt.starts_with('-')) {
++optind;
opt.remove_prefix(1);
auto eq = opt.find('=');
auto arg = [=] {
if (eq != opt.npos) {
return std::string_view(opt.begin() + eq + 1, opt.size() - eq - 1);
}
return std::string_view();
}();
if (eq != opt.npos) {
opt.remove_suffix(opt.size() - eq);
}
auto [optopt, func] = lookup_long_opt(opt);
std::visit([&](auto&& f) {
using T = std::decay_t<decltype(f)>;
if constexpr (std::is_same_v<T, opt_func>) {
if (eq != opt.npos) {
throw ArgumentError{"--{} takes no argument", optopt};
}
f(opts);
}
else {
static_assert(std::is_same_v<T, arg_func>);
if (eq == opt.npos) {
if (optind < argc) {
arg = argv[optind++];
}
else {
throw ArgumentError{"--{} requires argument", optopt};
}
}
f(opts, arg);
}
}, func);
}
else {
while (opt.size()) {
const auto c = opt.front();
const auto i = short_opt_chars.find(c);
if (i == short_opt_chars.npos) {
throw ArgumentError{"invalid option -{}", c};
}
opt.remove_prefix(1);
if (!opt.size()) {
++optind;
}
auto func = short_opts[i];
std::visit([&](auto&& f) {
using T = std::decay_t<decltype(f)>;
if constexpr (std::is_same_v<T, opt_func>) {
f(opts);
}
else {
static_assert(std::is_same_v<T, arg_func>);
std::string_view arg;
if (opt.size()) {
std::swap(arg, opt);
++optind;
}
else if (optind == argc) {
throw ArgumentError{"-{} requires argument", c};
}
else {
arg = argv[optind++];
}
f(opts, arg);
}
}, func);
}
}
}
if (opts.hflag || opts.version) {
return;
}
if (optind == argc) {
throw ArgumentError{"missing pattern"};
}
opts.pattern = argv[optind++];
if (optind < argc) {
opts.paths = std::vector<std::string_view>(argv + optind, argv + argc);
}
if (opts.count || opts.lflag) {
opts.before_context = opts.after_context = 0;
}
}