Updating for 1.0.2 release
[awesomized/libmemcached] / clients / memerror.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11 #include "config.h"
12
13 #include <inttypes.h>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <cstring>
17 #include <getopt.h>
18 #include <iostream>
19 #include <unistd.h>
20
21 #include <libmemcached/memcached.h>
22
23 #include "utilities.h"
24
25 #define PROGRAM_NAME "memerror"
26 #define PROGRAM_DESCRIPTION "Translate a memcached errror code into a string."
27
28
29 /* Prototypes */
30 void options_parse(int argc, char *argv[]);
31
32 static int opt_verbose= 0;
33
34 int main(int argc, char *argv[])
35 {
36 options_parse(argc, argv);
37
38 if (argc != 2)
39 {
40 return EXIT_FAILURE;
41 }
42
43 unsigned long value= strtoul(argv[1], (char **) NULL, 10);
44
45 if (value < MEMCACHED_MAXIMUM_RETURN)
46 {
47 std::cout << memcached_strerror(NULL, (memcached_return_t)value) << std::endl;
48 }
49 else
50 {
51 std::cerr << memcached_strerror(NULL, MEMCACHED_MAXIMUM_RETURN) << std::endl;
52 return EXIT_FAILURE;
53 }
54
55 return EXIT_SUCCESS;
56 }
57
58
59 void options_parse(int argc, char *argv[])
60 {
61 static struct option long_options[]=
62 {
63 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
64 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
65 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
66 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
67 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
68 {0, 0, 0, 0},
69 };
70
71 bool opt_version= false;
72 bool opt_help= false;
73 int option_index= 0;
74 while (1)
75 {
76 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
77 if (option_rv == -1)
78 {
79 break;
80 }
81
82 switch (option_rv)
83 {
84 case 0:
85 break;
86
87 case OPT_VERBOSE: /* --verbose or -v */
88 opt_verbose = OPT_VERBOSE;
89 break;
90
91 case OPT_DEBUG: /* --debug or -d */
92 opt_verbose = OPT_DEBUG;
93 break;
94
95 case OPT_VERSION: /* --version or -V */
96 opt_version= true;
97 break;
98
99 case OPT_HELP: /* --help or -h */
100 opt_help= true;
101 break;
102
103 case OPT_QUIET:
104 close_stdio();
105 break;
106
107 case '?':
108 /* getopt_long already printed an error message. */
109 exit(EXIT_FAILURE);
110
111 default:
112 abort();
113 }
114 }
115
116 if (opt_version)
117 {
118 version_command(PROGRAM_NAME);
119 exit(EXIT_SUCCESS);
120 }
121
122 if (opt_help)
123 {
124 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
125 exit(EXIT_SUCCESS);
126 }
127 }