1 #include "libmemcached/common.h"
5 #include <libmemcached/memcached.h>
7 #include "client_options.h"
10 static int opt_binary
= 0;
11 static int opt_verbose
= 0;
12 static time_t opt_expire
= 0;
13 static char *opt_servers
= NULL
;
14 static char *opt_hash
= NULL
;
16 #define PROGRAM_NAME "memrm"
17 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
20 void options_parse(int argc
, char *argv
[]);
22 int main(int argc
, char *argv
[])
26 memcached_server_st
*servers
;
28 options_parse(argc
, argv
);
34 if ((temp
= getenv("MEMCACHED_SERVERS")))
35 opt_servers
= strdup(temp
);
38 fprintf(stderr
, "No Servers provided\n");
43 memc
= memcached_create(NULL
);
44 process_hash_option(memc
, opt_hash
);
46 servers
= memcached_servers_parse(opt_servers
);
47 memcached_server_push(memc
, servers
);
48 memcached_server_list_free(servers
);
49 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
50 (uint64_t) opt_binary
);
55 printf("key: %s\nexpires: %llu\n", argv
[optind
], (unsigned long long)opt_expire
);
56 rc
= memcached_delete(memc
, argv
[optind
], strlen(argv
[optind
]), opt_expire
);
58 if (rc
!= MEMCACHED_SUCCESS
)
60 fprintf(stderr
, "memrm: %s: memcache error %s",
61 argv
[optind
], memcached_strerror(memc
, rc
));
62 if (memc
->cached_errno
)
63 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
64 fprintf(stderr
, "\n");
81 void options_parse(int argc
, char *argv
[])
83 memcached_programs_help_st help_options
[]=
88 static struct option long_options
[]=
90 {"version", no_argument
, NULL
, OPT_VERSION
},
91 {"help", no_argument
, NULL
, OPT_HELP
},
92 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
93 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
94 {"servers", required_argument
, NULL
, OPT_SERVERS
},
95 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
96 {"hash", required_argument
, NULL
, OPT_HASH
},
97 {"binary", no_argument
, NULL
, OPT_BINARY
},
105 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
106 if (option_rv
== -1) break;
114 case OPT_VERBOSE
: /* --verbose or -v */
115 opt_verbose
= OPT_VERBOSE
;
117 case OPT_DEBUG
: /* --debug or -d */
118 opt_verbose
= OPT_DEBUG
;
120 case OPT_VERSION
: /* --version or -V */
121 version_command(PROGRAM_NAME
);
123 case OPT_HELP
: /* --help or -h */
124 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
126 case OPT_SERVERS
: /* --servers or -s */
127 opt_servers
= strdup(optarg
);
129 case OPT_EXPIRE
: /* --expire */
130 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
133 opt_hash
= strdup(optarg
);
136 /* getopt_long already printed an error message. */