#include <unistd.h>\r
#include <getopt.h>\r
#include <memcached.h>\r
+#include "client_options.h"\r
\r
static int opt_verbose;\r
static int opt_displayflag;\r
static char *opt_servers;\r
\r
+struct memcached_st *parse_opt_servers (struct memcached_st *m,\r
+ char *opt_servers)\r
+{\r
+ char *s, *hostname;\r
+ unsigned int portnum;\r
+ while (s = strsep(&opt_servers, ",")) {\r
+ hostname = strsep(&s, ":");\r
+ portnum = atoi(s);\r
+ memcached_server_add(m, hostname, portnum);\r
+ }\r
+ return m;\r
+}\r
+\r
int main(int argc, char *argv[])\r
{\r
memcached_st *memc;\r
\r
static struct option long_options[] =\r
{\r
- {"version", no_argument, NULL, 257},\r
- {"help", no_argument, NULL, 258},\r
+ {"version", no_argument, NULL, OPT_VERSION},\r
+ {"help", no_argument, NULL, OPT_HELP},\r
{"verbose", no_argument, &opt_verbose, 1},\r
{"debug", no_argument, &opt_verbose, 2},\r
- {"servers", required_argument, NULL, 259},\r
- {"flag", no_argument, &opt_displayflag, 1},\r
+ {"servers", required_argument, NULL, OPT_SERVERS},\r
+ {"flag", no_argument, &opt_displayflag, OPT_FLAG},\r
{0, 0, 0, 0},\r
};\r
int option_index = 0;\r
int option_rv;\r
\r
- while (1) {\r
+ while (1) \r
+ {\r
option_rv = getopt_long(argc, argv, "", long_options, &option_index);\r
if (option_rv == -1) break;\r
switch (option_rv) {\r
case 0:\r
break;\r
- case 257: /* --version */\r
+ case OPT_VERSION: /* --version */\r
printf("memcache tools, memcat, v1.0\n");\r
exit(0);\r
break;\r
- case 258: /* --help */\r
+ case OPT_HELP: /* --help */\r
printf("useful help messages go here\n");\r
exit(0);\r
break;\r
- case 259: /* --servers */\r
+ case OPT_SERVERS: /* --servers */\r
opt_servers = strdup(optarg);\r
break;\r
case '?':\r
}\r
}\r
\r
- /* todo, turn opt_servers into something to pass to memcached_init */\r
memc= memcached_init(NULL);\r
-\r
+ memc= parse_opt_servers(memc, opt_servers);\r
+ \r
while (optind < argc) {\r
string= memcached_get(memc, argv[optind], strlen(argv[optind]),\r
&string_length, &flags, &rc);\r
- if (string) {\r
- printf("%.*s\n", string_length, string);\r
- free(string);\r
+ if (rc == MEMCACHED_SUCCESS) {\r
+ if (opt_displayflag) {\r
+ printf("%d\n", flags);\r
+ } else {\r
+ if (string) \r
+ {\r
+ printf("%.*s\n", string_length, string);\r
+ free(string);\r
+ }\r
+ }\r
}\r
+\r
optind++;\r
}\r
\r