2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
19 #include <libmemcached/memcached.h>
20 #include "client_options.h"
21 #include "utilities.h"
23 static int opt_binary
= 0;
24 static int opt_verbose
= 0;
25 static time_t opt_expire
= 0;
26 static char *opt_servers
= NULL
;
27 static char *opt_hash
= NULL
;
28 static char *opt_username
;
29 static char *opt_passwd
;
31 #define PROGRAM_NAME "memrm"
32 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
35 static void options_parse(int argc
, char *argv
[]);
37 int main(int argc
, char *argv
[])
40 memcached_return_t rc
;
41 memcached_server_st
*servers
;
45 options_parse(argc
, argv
);
52 if ((temp
= getenv("MEMCACHED_SERVERS")))
53 opt_servers
= strdup(temp
);
56 std::cerr
<< "No Servers provided" << std::endl
;
61 memc
= memcached_create(NULL
);
62 process_hash_option(memc
, opt_hash
);
64 servers
= memcached_servers_parse(opt_servers
);
65 memcached_server_push(memc
, servers
);
66 memcached_server_list_free(servers
);
67 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
68 (uint64_t) opt_binary
);
70 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
73 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
79 memcached_return_t ret
;
80 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
82 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
92 std::cout
<< "key: " << argv
[optind
] << std::endl
;
93 std::cout
<< "expires: " << opt_expire
<< std::endl
;
95 rc
= memcached_delete(memc
, argv
[optind
], strlen(argv
[optind
]), opt_expire
);
97 if (memcached_failed(rc
))
99 std::cerr
<< PROGRAM_NAME
<< ": " << argv
[optind
] << ": error " << memcached_strerror(memc
, rc
) << std::endl
;
101 if (memcached_last_error_errno(memc
))
103 std::cerr
<< " system error " << strerror(memcached_last_error_errno(memc
));
105 std::cerr
<< std::endl
;
113 memcached_free(memc
);
125 static void options_parse(int argc
, char *argv
[])
127 memcached_programs_help_st help_options
[]=
132 static struct option long_options
[]=
134 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
135 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
136 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
137 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
138 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
139 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
140 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
141 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
142 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
143 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
151 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
152 if (option_rv
== -1) break;
160 case OPT_VERBOSE
: /* --verbose or -v */
161 opt_verbose
= OPT_VERBOSE
;
163 case OPT_DEBUG
: /* --debug or -d */
164 opt_verbose
= OPT_DEBUG
;
166 case OPT_VERSION
: /* --version or -V */
167 version_command(PROGRAM_NAME
);
169 case OPT_HELP
: /* --help or -h */
170 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
172 case OPT_SERVERS
: /* --servers or -s */
173 opt_servers
= strdup(optarg
);
175 case OPT_EXPIRE
: /* --expire */
176 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
179 opt_hash
= strdup(optarg
);
182 opt_username
= optarg
;
188 /* getopt_long already printed an error message. */