bin: consolidate clients
[awesomized/libmemcached] / src / bin / memerror.cc
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 #include "mem_config.h"
17
18 #define PROGRAM_NAME "memerror"
19 #define PROGRAM_DESCRIPTION "Translate a memcached error code into a string."
20 #define PROGRAM_VERSION "1.1"
21
22 #include "common/options.hpp"
23
24 int main(int argc, char *argv[]) {
25 client_options opt{PROGRAM_NAME, PROGRAM_VERSION, PROGRAM_DESCRIPTION, "code [code ...]"};
26
27 for (const auto &def : opt.defaults) {
28 switch (def.opt.val) {
29 case 'h': // --help
30 case 'V': // --version
31 case 'v': // --verbose
32 case 'd': // --debug
33 opt.add(def);
34 break;
35 default:
36 break;
37 }
38 }
39
40 char **argp = nullptr;
41 if (!opt.parse(argc, argv, &argp)) {
42 exit(EXIT_FAILURE);
43 }
44
45 opt.apply(nullptr);
46
47 if (!*argp) {
48 std::cerr << "No error codes provided.\n";
49 exit(EXIT_FAILURE);
50 }
51
52 for (auto arg = argp; *arg; ++arg) {
53 auto code = std::stoul(*arg);
54 auto rc = static_cast<memcached_return_t>(code);
55
56 if (opt.isset("verbose")) {
57 std::cout << "code: " << code << "\n";
58 std::cout << "name: " << memcached_strerror(nullptr, rc) << "\n";
59 } else {
60 std::cout << memcached_strerror(nullptr, rc) << "\n";
61 }
62 }
63
64 exit(EXIT_SUCCESS);
65 }