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