3948ff3e79d074db5d7c35941b13c2506a7f73a4
[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 #include "common/checks.hpp"
24
25 int main(int argc, char *argv[]) {
26 client_options opt{PROGRAM_NAME, PROGRAM_VERSION, PROGRAM_DESCRIPTION, "code [code ...]"};
27
28 for (const auto &def : opt.defaults) {
29 switch (def.opt.val) {
30 case 'h': // --help
31 case 'V': // --version
32 case 'v': // --verbose
33 case 'd': // --debug
34 case 'q': // --quiet
35 opt.add(def);
36 break;
37 default:
38 break;
39 }
40 }
41
42 char **argp = nullptr;
43 if (!opt.parse(argc, argv, &argp)) {
44 exit(EXIT_FAILURE);
45 }
46
47 if (!opt.apply(nullptr)) {
48 exit(EXIT_FAILURE);
49 }
50
51 if (!check_argp(opt, argp, "No error code(s) provided.")) {
52 exit(EXIT_FAILURE);
53 }
54
55 auto exit_code = EXIT_SUCCESS;
56 for (auto arg = argp; *arg; ++arg) {
57 auto code = std::stoul(*arg);
58 auto rc = static_cast<memcached_return_t>(code);
59
60 if (opt.isset("verbose")) {
61 std::cout << "code: " << code << "\n";
62 std::cout << "name: ";
63 }
64 if (rc >= MEMCACHED_MAXIMUM_RETURN) {
65 exit_code = EXIT_FAILURE;
66 std::cerr << memcached_strerror(nullptr, rc) << std::endl;
67 } else {
68 std::cout << memcached_strerror(nullptr, rc) << std::endl;
69 }
70 }
71
72 exit(exit_code);
73 }