5682c78610ec32b0c75dfa2752297d0e0943bd1b
[m6w6/libmemcached] / src / bin / common / options.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include <algorithm>
19 #include <cstdint>
20 #include <climits>
21 #include <functional>
22 #include <getopt.h>
23 #include <iostream>
24 #include <string>
25 #include <vector>
26
27 #include "libmemcached/common.h"
28
29 class client_options {
30 public:
31
32 struct extended_option {
33 option opt;
34 std::string help;
35 std::function<bool(client_options &, extended_option &)> parse;
36 std::function<bool(const client_options &, const extended_option &, memcached_st *)> apply;
37 char *arg;
38 bool set;
39 };
40
41 std::vector<extended_option> options;
42 std::vector<extended_option> defaults;
43
44 const char *prog_name;
45 const char *prog_vers;
46 const char *prog_desc;
47 const char *prog_argp;
48
49 client_options(const char *prg, const char *ver, const char *dsc, const char *arg = nullptr)
50 : options{}
51 , defaults{}
52 , prog_name{prg}
53 , prog_vers{ver}
54 , prog_desc{dsc}
55 , prog_argp{arg}
56 {
57
58 def("help", 'h', no_argument, "Print this help.")
59 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *) {
60 if (ext.set) {
61 opt.print_help();
62 exit(EXIT_SUCCESS);
63 }
64 return true;
65 };
66 def("version", 'V', no_argument, "Print program version.")
67 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *) {
68 if (ext.set) {
69 opt.print_version();
70 exit(EXIT_SUCCESS);
71 }
72 return true;
73 };
74
75 def("verbose", 'v', no_argument, "Print more informational output.")
76 .parse = [](client_options &opt, extended_option &) {
77 opt.unset("quiet");
78 return true;
79 };
80 def("debug", 'd', no_argument, "Print output useful only for debugging.")
81 .parse = [](client_options &opt, extended_option &) {
82 opt.set("verbose");
83 opt.unset("quiet");
84 return true;
85 };
86 def("quiet", 'q', no_argument, "Print no output, not even errors.")
87 .parse = [](client_options &opt, extended_option &) {
88 opt.unset("verbose");
89 opt.unset("debug");
90 return true;
91 };
92
93 def("password", 'p', required_argument, "SASL password.");
94 def("username", 'u', required_argument, "SASL username.")
95 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
96 if (auto username = ext.arg) {
97 #if !LIBMEMCACHED_WITH_SASL_SUPPORT
98 if (!opt.isset("quiet")) {
99 std::cerr
100 << "SASL username was supplied, but binary was not built with SASL support.\n";
101 }
102 return false;
103 #else
104 if (memc) {
105 if (MEMCACHED_SUCCESS
106 != memcached_set_sasl_auth_data(memc, username, opt.argof("password"))) {
107 if (!opt.isset("quiet")) {
108 std::cerr << memcached_last_error_message(memc);
109 }
110 return false;
111 }
112 }
113 #endif
114 }
115 return true;
116 };
117
118 def("binary", 'b', no_argument, "Use the binary memcached protocol.")
119 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
120 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, ext.set)) {
121 if(!opt.isset("quiet")) {
122 std::cerr << memcached_last_error_message(memc);
123 }
124 return false;
125 }
126 return true;
127 };
128 def("buffer", 'B', no_argument, "Buffer requests.")
129 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
130 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, ext.set)) {
131 if(!opt.isset("quiet")) {
132 std::cerr << memcached_last_error_message(memc);
133 }
134 return false;
135 }
136 return true;
137 };
138 def("non-blocking", 'n', no_argument, "Use non-blocking connections.")
139 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
140 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, ext.set)) {
141 if(!opt.isset("quiet")) {
142 std::cerr << memcached_last_error_message(memc);
143 }
144 return false;
145 }
146 return true;
147 };
148 def("tcp-nodelay", 'N', no_argument, "Disable Nagle's algorithm.")
149 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
150 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, ext.set)) {
151 if(!opt.isset("quiet")) {
152 std::cerr << memcached_last_error_message(memc);
153 }
154 return false;
155 }
156 return true;
157 };
158 def("servers", 's', required_argument, "List of servers to connect to.")
159 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
160 auto servers = ext.arg;
161 if (!servers) {
162 if (opt.isset("verbose")) {
163 std::cerr << "Checking environment for a server list in MEMCACHED_SERVERS.\n";
164 }
165 servers = getenv("MEMCACHED_SERVERS");
166 if (!servers || !*servers) {
167 if (!opt.isset("quiet")) {
168 std::cerr << "No servers provided.\n";
169 }
170 return false;
171 }
172 }
173
174 auto server_list = memcached_servers_parse(servers);
175 if (!server_list || !memcached_server_list_count(server_list)) {
176 if (!opt.isset("quiet")) {
177 std::cerr << "Invalid server list provided: '" << servers << "'\n";
178 }
179 if (server_list) {
180 memcached_server_list_free(server_list);
181 }
182 return false;
183 }
184
185 if (MEMCACHED_SUCCESS != memcached_server_push(memc, server_list)) {
186 if (!opt.isset("quiet")) {
187 std::cerr << memcached_last_error_message(memc);
188 }
189 memcached_server_list_free(server_list);
190 return false;
191 }
192 memcached_server_list_free(server_list);
193 return true;
194 };
195 def("hash", 'H', required_argument, "Key hashing method.")
196 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
197 if (ext.set) {
198 std::string hash_wanted{ext.arg};
199 memcached_hash_t hash = MEMCACHED_HASH_DEFAULT;
200
201 std::transform(hash_wanted.begin(), hash_wanted.end(), hash_wanted.begin(), ::toupper);
202
203 if (opt.isset("verbose")) {
204 std::cerr << "Checking for hash '" << hash_wanted << "'.\n";
205 }
206 for (int h = MEMCACHED_HASH_DEFAULT; h < MEMCACHED_HASH_MAX; ++h) {
207 auto hash_type = static_cast<memcached_hash_t>(h);
208 std::string hash_string{libmemcached_string_hash(hash_type)};
209
210 if (hash_wanted.length() == hash_string.length()) {
211 auto ci = std::equal(hash_string.begin(), hash_string.end(), hash_wanted.begin(),
212 [](int a, int b) { return ::toupper(a) == b; });
213 if (ci) {
214 hash = hash_type;
215 break;
216 }
217 }
218 }
219 if (hash == MEMCACHED_HASH_DEFAULT) {
220 if (!opt.isset("quiet")) {
221 std::cerr << "Could not find hash '" << hash_wanted << "'.\n";
222 }
223 }
224 if (MEMCACHED_SUCCESS != memcached_behavior_set_key_hash(memc, hash)) {
225 if (!opt.isset("quiet")) {
226 std::cerr << memcached_last_error_message(memc);
227 }
228 return false;
229 }
230 }
231 return true;
232 };
233 }
234
235 extended_option &def(option opt, std::string help) {
236 defaults.emplace_back(extended_option{opt, std::move(help), {}, {}, nullptr, false});
237 return defaults.back();
238 }
239
240 extended_option &def(const char *name, char flag, int has_arg, const char *help) {
241 return def(option{name, has_arg, nullptr, flag}, help);
242 }
243
244 extended_option &add(extended_option ext) {
245 options.emplace_back(std::move(ext));
246 return options.back();
247 }
248
249 extended_option &add(option opt, std::string help) {
250 options.emplace_back(extended_option{opt, std::move(help), nullptr, nullptr, nullptr, false});
251 return options.back();
252 }
253
254 extended_option &add(const char *name, char flag, int has_arg, const char *help) {
255 return add(option{name, has_arg, nullptr, flag}, help);
256 }
257
258 extended_option &get(const std::string &name) {
259 // UB if not found
260 return *find(name);
261 }
262 extended_option &get(int c) {
263 // UB if not found
264 return *find(c);
265 }
266
267 const extended_option &get(const std::string &name) const {
268 // UB if not found
269 return *find(name);
270 }
271 const extended_option &get(int c) const {
272 // UB if not found
273 return *find(c);
274 }
275
276 bool has(const std::string &name) const {
277 auto found = find(name);
278 return found != options.cend();
279 }
280 bool has(int c) const {
281 auto found = find(c);
282 return found != options.cend();
283 }
284
285 bool isset(const std::string &name) const {
286 return has(name) && get(name).set;
287 }
288 bool isset(int c) const {
289 return has(c) && get(c).set;
290 }
291
292 void unset(const std::string &name) {
293 set(name, false);
294 }
295 void unset(int c) {
296 set(c, false);
297 }
298
299 void set(const std::string &name, bool set_ = true, char *optarg_ = nullptr) {
300 if (has(name)) {
301 auto &opt = get(name);
302 opt.set = set_;
303 opt.arg = optarg_;
304 }
305 }
306 void set(int c, bool set_ = true, char *optarg_ = nullptr) {
307 if (has(c)) {
308 auto &opt = get(c);
309 opt.set = set_;
310 opt.arg = optarg_;
311 }
312 }
313
314 char *argof(const std::string &name) const {
315 if (has(name)) {
316 return get(name).arg;
317 }
318 return nullptr;
319 }
320 char *argof(int c) const {
321 if (has(c)) {
322 return get(c).arg;
323 }
324 return nullptr;
325 }
326
327 const extended_option &operator[](const std::string &name) const {
328 return get(name);
329 }
330 const extended_option &operator[](int c) const {
331 return get(c);
332 }
333
334 void print_version() const;
335 void print_help() const;
336
337 bool parse(int argc, char *argv[], char ***argp = nullptr);
338 bool apply(memcached_st *memc);
339
340 private:
341 using iterator = std::vector<extended_option>::iterator;
342 using const_iterator = std::vector<extended_option>::const_iterator;
343 using predicate = std::function<bool(const extended_option &ext)>;
344
345 const_iterator find(const predicate &pred) const {
346 return std::find_if(options.cbegin(), options.cend(), pred);
347 }
348 const_iterator find(const std::string &name) const {
349 return find([&name](const extended_option &ext) {
350 return ext.opt.name && ext.opt.name == name;
351 });
352 }
353 const_iterator find(int c) const {
354 return find([c](const extended_option &ext) {
355 return ext.opt.val == c || (c == 1 && ext.opt.val == '-');
356 });
357 }
358
359 iterator find(const predicate &pred) {
360 return std::find_if(options.begin(), options.end(), pred);
361 }
362 iterator find(const std::string &name) {
363 return find([&name](const extended_option &ext) {
364 return ext.opt.name && ext.opt.name == name;
365 });
366 }
367 iterator find(int c) {
368 return find([c](const extended_option &ext) {
369 return ext.opt.val == c || (c == 1 && ext.opt.val == '-');
370 });
371 }
372 };