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.
12 #include "libmemcached/common.h"
16 #include <libmemcached/memcached.h>
18 #include "client_options.h"
19 #include "utilities.h"
21 static int opt_binary
= 0;
22 static int opt_verbose
= 0;
23 static time_t opt_expire
= 0;
24 static char *opt_servers
= NULL
;
25 static char *opt_hash
= NULL
;
27 #define PROGRAM_NAME "memrm"
28 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
31 static void options_parse(int argc
, char *argv
[]);
33 int main(int argc
, char *argv
[])
36 memcached_return_t rc
;
37 memcached_server_st
*servers
;
41 options_parse(argc
, argv
);
47 if ((temp
= getenv("MEMCACHED_SERVERS")))
48 opt_servers
= strdup(temp
);
51 fprintf(stderr
, "No Servers provided\n");
56 memc
= memcached_create(NULL
);
57 process_hash_option(memc
, opt_hash
);
59 servers
= memcached_servers_parse(opt_servers
);
60 memcached_server_push(memc
, servers
);
61 memcached_server_list_free(servers
);
62 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
63 (uint64_t) opt_binary
);
68 printf("key: %s\nexpires: %llu\n", argv
[optind
], (unsigned long long)opt_expire
);
69 rc
= memcached_delete(memc
, argv
[optind
], strlen(argv
[optind
]), opt_expire
);
71 if (rc
!= MEMCACHED_SUCCESS
)
73 fprintf(stderr
, "memrm: %s: memcache error %s",
74 argv
[optind
], memcached_strerror(memc
, rc
));
75 if (memc
->cached_errno
)
76 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
77 fprintf(stderr
, "\n");
96 static void options_parse(int argc
, char *argv
[])
98 memcached_programs_help_st help_options
[]=
103 static struct option long_options
[]=
105 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
106 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
107 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
108 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
109 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
110 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
111 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
112 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
120 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
121 if (option_rv
== -1) break;
129 case OPT_VERBOSE
: /* --verbose or -v */
130 opt_verbose
= OPT_VERBOSE
;
132 case OPT_DEBUG
: /* --debug or -d */
133 opt_verbose
= OPT_DEBUG
;
135 case OPT_VERSION
: /* --version or -V */
136 version_command(PROGRAM_NAME
);
138 case OPT_HELP
: /* --help or -h */
139 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
141 case OPT_SERVERS
: /* --servers or -s */
142 opt_servers
= strdup(optarg
);
144 case OPT_EXPIRE
: /* --expire */
145 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
148 opt_hash
= strdup(optarg
);
151 /* getopt_long already printed an error message. */