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 +--------------------------------------------------------------------+
28 #elif defined _MSC_VER
29 # include "win32/getopt.h"
32 #include "libmemcached/common.h"
34 class client_options {
37 struct extended_option {
40 std::function<bool(client_options &, extended_option &)> parse;
41 std::function<bool(const client_options &, const extended_option &, memcached_st *)> apply;
46 std::vector<extended_option> options;
47 std::vector<extended_option> defaults;
49 const char *prog_name;
50 const char *prog_vers;
51 const char *prog_desc;
52 const char *prog_argp;
54 client_options(const char *prg, const char *ver, const char *dsc, const char *arg = nullptr)
63 def("help", 'h', no_argument, "Print this help.")
64 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *) {
71 def("version", 'V', no_argument, "Print program version.")
72 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *) {
80 def("verbose", 'v', no_argument, "Print more informational output.")
81 .parse = [](client_options &opt, extended_option &) {
85 def("debug", 'd', no_argument, "Print output useful only for debugging.")
86 .parse = [](client_options &opt, extended_option &) {
91 def("quiet", 'q', no_argument, "Print no output, not even errors.")
92 .parse = [](client_options &opt, extended_option &) {
98 def("password", 'p', required_argument, "SASL password.");
99 def("username", 'u', required_argument, "SASL username.")
100 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
101 if (auto username = ext.arg) {
102 #if !LIBMEMCACHED_WITH_SASL_SUPPORT
103 if (!opt.isset("quiet")) {
105 << "SASL username was supplied, but binary was not built with SASL support.\n";
110 if (MEMCACHED_SUCCESS
111 != memcached_set_sasl_auth_data(memc, username, opt.argof("password"))) {
112 if (!opt.isset("quiet")) {
113 std::cerr << memcached_last_error_message(memc);
123 def("binary", 'b', no_argument, "Use the binary memcached protocol.")
124 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
125 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, ext.set)) {
126 if(!opt.isset("quiet")) {
127 std::cerr << memcached_last_error_message(memc);
133 def("buffer", 'B', no_argument, "Buffer requests.")
134 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
135 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, ext.set)) {
136 if(!opt.isset("quiet")) {
137 std::cerr << memcached_last_error_message(memc);
143 def("non-blocking", 'n', no_argument, "Use non-blocking connections.")
144 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
145 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, ext.set)) {
146 if(!opt.isset("quiet")) {
147 std::cerr << memcached_last_error_message(memc);
153 def("tcp-nodelay", 'N', no_argument, "Disable Nagle's algorithm.")
154 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
155 if (MEMCACHED_SUCCESS != memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, ext.set)) {
156 if(!opt.isset("quiet")) {
157 std::cerr << memcached_last_error_message(memc);
163 def("servers", 's', required_argument, "List of servers to connect to.")
164 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
165 auto servers = ext.arg;
167 if (opt.isset("verbose")) {
168 std::cerr << "Checking environment for a server list in MEMCACHED_SERVERS.\n";
170 servers = getenv("MEMCACHED_SERVERS");
171 if (!servers || !*servers) {
172 if (!opt.isset("quiet")) {
173 std::cerr << "No servers provided.\n";
179 auto server_list = memcached_servers_parse(servers);
180 if (!server_list || !memcached_server_list_count(server_list)) {
181 if (!opt.isset("quiet")) {
182 std::cerr << "Invalid server list provided: '" << servers << "'\n";
185 memcached_server_list_free(server_list);
190 if (MEMCACHED_SUCCESS != memcached_server_push(memc, server_list)) {
191 if (!opt.isset("quiet")) {
192 std::cerr << memcached_last_error_message(memc);
194 memcached_server_list_free(server_list);
197 memcached_server_list_free(server_list);
200 def("hash", 'H', required_argument, "Key hashing method.")
201 .apply = [](const client_options &opt, const extended_option &ext, memcached_st *memc) {
203 std::string hash_wanted{ext.arg};
204 memcached_hash_t hash = MEMCACHED_HASH_DEFAULT;
206 std::transform(hash_wanted.begin(), hash_wanted.end(), hash_wanted.begin(), ::toupper);
208 if (opt.isset("verbose")) {
209 std::cerr << "Checking for hash '" << hash_wanted << "'.\n";
211 for (int h = MEMCACHED_HASH_DEFAULT; h < MEMCACHED_HASH_MAX; ++h) {
212 auto hash_type = static_cast<memcached_hash_t>(h);
213 std::string hash_string{libmemcached_string_hash(hash_type)};
215 if (hash_wanted.length() == hash_string.length()) {
216 auto ci = std::equal(hash_string.begin(), hash_string.end(), hash_wanted.begin(),
217 [](int a, int b) { return ::toupper(a) == b; });
224 if (hash == MEMCACHED_HASH_DEFAULT) {
225 if (!opt.isset("quiet")) {
226 std::cerr << "Could not find hash '" << hash_wanted << "'.\n";
229 if (MEMCACHED_SUCCESS != memcached_behavior_set_key_hash(memc, hash)) {
230 if (!opt.isset("quiet")) {
231 std::cerr << memcached_last_error_message(memc);
240 extended_option &def(option opt, std::string help) {
241 defaults.emplace_back(extended_option{opt, std::move(help), {}, {}, nullptr, false});
242 return defaults.back();
245 extended_option &def(const char *name, char flag, int has_arg, const char *help) {
246 return def(option{name, has_arg, nullptr, flag}, help);
249 extended_option &add(extended_option ext) {
250 options.emplace_back(std::move(ext));
251 return options.back();
254 extended_option &add(option opt, std::string help) {
255 options.emplace_back(extended_option{opt, std::move(help), nullptr, nullptr, nullptr, false});
256 return options.back();
259 extended_option &add(const char *name, char flag, int has_arg, const char *help) {
260 return add(option{name, has_arg, nullptr, flag}, help);
263 extended_option &get(const std::string &name) {
267 extended_option &get(int c) {
272 const extended_option &get(const std::string &name) const {
276 const extended_option &get(int c) const {
281 bool has(const std::string &name) const {
282 auto found = find(name);
283 return found != options.cend();
285 bool has(int c) const {
286 auto found = find(c);
287 return found != options.cend();
290 bool isset(const std::string &name) const {
291 return has(name) && get(name).set;
293 bool isset(int c) const {
294 return has(c) && get(c).set;
297 void unset(const std::string &name) {
304 void set(const std::string &name, bool set_ = true, char *optarg_ = nullptr) {
306 auto &opt = get(name);
311 void set(int c, bool set_ = true, char *optarg_ = nullptr) {
319 char *argof(const std::string &name) const {
321 return get(name).arg;
325 char *argof(int c) const {
332 const extended_option &operator[](const std::string &name) const {
335 const extended_option &operator[](int c) const {
339 void print_version() const;
340 void print_help() const;
342 bool parse(int argc, char *argv[], char ***argp = nullptr);
343 bool apply(memcached_st *memc);
346 using iterator = std::vector<extended_option>::iterator;
347 using const_iterator = std::vector<extended_option>::const_iterator;
348 using predicate = std::function<bool(const extended_option &ext)>;
350 const_iterator find(const predicate &pred) const {
351 return std::find_if(options.cbegin(), options.cend(), pred);
353 const_iterator find(const std::string &name) const {
354 return find([&name](const extended_option &ext) {
355 return ext.opt.name && ext.opt.name == name;
358 const_iterator find(int c) const {
359 return find([c](const extended_option &ext) {
360 return ext.opt.val == c || (c == 1 && ext.opt.val == '-');
364 iterator find(const predicate &pred) {
365 return std::find_if(options.begin(), options.end(), pred);
367 iterator find(const std::string &name) {
368 return find([&name](const extended_option &ext) {
369 return ext.opt.name && ext.opt.name == name;
372 iterator find(int c) {
373 return find([c](const extended_option &ext) {
374 return ext.opt.val == c || (c == 1 && ext.opt.val == '-');