70be59039d877b713c88e3a7ff11de29b70b812b
[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 return false;
102 }
103 }
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 }
114 return true;
115 };
116
117 def("binary", 'b', no_argument, "Use the binary memcached protocol.")
118 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
119 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, ext.set)) {
120 if(!opt.isset("quiet")) {
121 std::cerr << memcached_last_error_message(memc);
122 }
123 return false;
124 }
125 return true;
126 };
127 def("buffer", 'B', no_argument, "Buffer requests.")
128 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
129 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, ext.set)) {
130 if(!opt.isset("quiet")) {
131 std::cerr << memcached_last_error_message(memc);
132 }
133 return false;
134 }
135 return true;
136 };
137 def("non-blocking", 'n', no_argument, "Use non-blocking connections.")
138 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
139 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, ext.set)) {
140 if(!opt.isset("quiet")) {
141 std::cerr << memcached_last_error_message(memc);
142 }
143 return false;
144 }
145 return true;
146 };
147 def("tcp-nodelay", 'N', no_argument, "Disable Nagle's algorithm.")
148 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
149 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, ext.set)) {
150 if(!opt.isset("quiet")) {
151 std::cerr << memcached_last_error_message(memc);
152 }
153 return false;
154 }
155 return true;
156 };
157 def("servers", 's', required_argument, "List of servers to connect to.")
158 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
159 auto servers = ext.arg;
160 if (!servers) {
161 if (opt.isset("verbose")) {
162 std::cerr << "Checking environment for a server list in MEMCACHED_SERVERS.\n";
163 }
164 servers = getenv("MEMCACHED_SERVERS");
165 if (!servers || !*servers) {
166 if (!opt.isset("quiet")) {
167 std::cerr << "No servers provided.\n";
168 }
169 return false;
170 }
171 }
172
173 auto server_list = memcached_servers_parse(servers);
174 if (!server_list || !memcached_server_list_count(server_list)) {
175 if (!opt.isset("quiet")) {
176 std::cerr << "Invalid server list provided: '" << servers << "'\n";
177 }
178 if (server_list) {
179 memcached_server_list_free(server_list);
180 }
181 return false;
182 }
183
184 if (MEMCACHED_SUCCESS != memcached_server_push(memc, server_list)) {
185 if (!opt.isset("quiet")) {
186 std::cerr << memcached_last_error_message(memc);
187 }
188 memcached_server_list_free(server_list);
189 return false;
190 }
191 memcached_server_list_free(server_list);
192 return true;
193 };
194 def("hash", 'H', required_argument, "Key hashing method.")
195 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
196 if (ext.set) {
197 std::string hash_wanted{ext.arg};
198 memcached_hash_t hash = MEMCACHED_HASH_DEFAULT;
199
200 std::transform(hash_wanted.begin(), hash_wanted.end(), hash_wanted.begin(), ::toupper);
201
202 if (opt.isset("verbose")) {
203 std::cerr << "Checking for hash '" << hash_wanted << "'.\n";
204 }
205 for (int h = MEMCACHED_HASH_DEFAULT; h < MEMCACHED_HASH_MAX; ++h) {
206 auto hash_type = static_cast<memcached_hash_t>(h);
207 std::string hash_string{libmemcached_string_hash(hash_type)};
208
209 if (hash_wanted.length() == hash_string.length()) {
210 auto ci = std::equal(hash_string.begin(), hash_string.end(), hash_wanted.begin(),
211 [](int a, int b) { return ::toupper(a) == b; });
212 if (ci) {
213 hash = hash_type;
214 break;
215 }
216 }
217 }
218 if (hash == MEMCACHED_HASH_DEFAULT) {
219 if (!opt.isset("quiet")) {
220 std::cerr << "Could not find hash '" << hash_wanted << "'.\n";
221 }
222 }
223 if (MEMCACHED_SUCCESS != memcached_behavior_set_key_hash(memc, hash)) {
224 if (!opt.isset("quiet")) {
225 std::cerr << memcached_last_error_message(memc);
226 }
227 return false;
228 }
229 }
230 return true;
231 };
232 }
233
234 extended_option &def(option opt, std::string help) {
235 defaults.emplace_back(extended_option{opt, std::move(help), {}, {}, nullptr, false});
236 return defaults.back();
237 }
238
239 extended_option &def(const char *name, char flag, int has_arg, const char *help) {
240 return def(option{name, has_arg, nullptr, flag}, help);
241 }
242
243 extended_option &add(extended_option ext) {
244 options.emplace_back(std::move(ext));
245 return options.back();
246 }
247
248 extended_option &add(option opt, std::string help) {
249 options.emplace_back(extended_option{opt, std::move(help), nullptr, nullptr, nullptr, false});
250 return options.back();
251 }
252
253 extended_option &add(const char *name, char flag, int has_arg, const char *help) {
254 return add(option{name, has_arg, nullptr, flag}, help);
255 }
256
257 extended_option &get(const std::string &name) {
258 // UB if not found
259 return *find(name);
260 }
261 extended_option &get(int c) {
262 // UB if not found
263 return *find(c);
264 }
265
266 const extended_option &get(const std::string &name) const {
267 // UB if not found
268 return *find(name);
269 }
270 const extended_option &get(int c) const {
271 // UB if not found
272 return *find(c);
273 }
274
275 bool has(const std::string &name) const {
276 auto found = find(name);
277 return found != options.cend();
278 }
279 bool has(int c) const {
280 auto found = find(c);
281 return found != options.cend();
282 }
283
284 bool isset(const std::string &name) const {
285 return has(name) && get(name).set;
286 }
287 bool isset(int c) const {
288 return has(c) && get(c).set;
289 }
290
291 void unset(const std::string &name) {
292 set(name, false);
293 }
294 void unset(int c) {
295 set(c, false);
296 }
297
298 void set(const std::string &name, bool set_ = true, char *optarg_ = nullptr) {
299 if (has(name)) {
300 auto &opt = get(name);
301 opt.set = set_;
302 opt.arg = optarg_;
303 }
304 }
305 void set(int c, bool set_ = true, char *optarg_ = nullptr) {
306 if (has(c)) {
307 auto &opt = get(c);
308 opt.set = set_;
309 opt.arg = optarg_;
310 }
311 }
312
313 char *argof(const std::string &name) const {
314 if (has(name)) {
315 return get(name).arg;
316 }
317 return nullptr;
318 }
319 char *argof(int c) const {
320 if (has(c)) {
321 return get(c).arg;
322 }
323 return nullptr;
324 }
325
326 const extended_option &operator[](const std::string &name) const {
327 return get(name);
328 }
329 const extended_option &operator[](int c) const {
330 return get(c);
331 }
332
333 void print_version() const;
334 void print_help() const;
335
336 bool parse(int argc, char *argv[], char ***argp = nullptr);
337 bool apply(memcached_st *memc);
338
339 private:
340 using iterator = std::vector<extended_option>::iterator;
341 using const_iterator = std::vector<extended_option>::const_iterator;
342 using predicate = std::function<bool(const extended_option &ext)>;
343
344 const_iterator find(const predicate &pred) const {
345 return std::find_if(options.cbegin(), options.cend(), pred);
346 }
347 const_iterator find(const std::string &name) const {
348 return find([&name](const extended_option &ext) {
349 return ext.opt.name && ext.opt.name == name;
350 });
351 }
352 const_iterator find(int c) const {
353 return find([c](const extended_option &ext) {
354 return ext.opt.val == c || (c == 1 && ext.opt.val == '-');
355 });
356 }
357
358 iterator find(const predicate &pred) {
359 return std::find_if(options.begin(), options.end(), pred);
360 }
361 iterator find(const std::string &name) {
362 return find([&name](const extended_option &ext) {
363 return ext.opt.name && ext.opt.name == name;
364 });
365 }
366 iterator find(int c) {
367 return find([c](const extended_option &ext) {
368 return ext.opt.val == c || (c == 1 && ext.opt.val == '-');
369 });
370 }
371 };