Merge from Mark's code. Modified memcat to use enum for options.
[m6w6/libmemcached] / src / memcat.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <memcached.h>
5 #include "client_options.h"
6
7 static int opt_verbose;
8 static int opt_displayflag;
9 static char *opt_servers;
10
11 int main(int argc, char *argv[])
12 {
13 memcached_st *memc;
14 char *string;
15 size_t string_length;
16 uint16_t flags;
17 memcached_return rc;
18 unsigned int x;
19
20 static struct option long_options[] =
21 {
22 {"version", no_argument, NULL, OPT_VERSION},
23 {"help", no_argument, NULL, OPT_HELP},
24 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
25 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
26 {"servers", required_argument, NULL, OPT_SERVERS},
27 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
28 {0, 0, 0, 0},
29 };
30 int option_index = 0;
31 int option_rv;
32
33 while (1)
34 {
35 option_rv = getopt_long(argc, argv, "", long_options, &option_index);
36 if (option_rv == -1) break;
37 switch (option_rv) {
38 case 0:
39 break;
40 case OPT_VERSION: /* --version */
41 printf("memcache tools, memcat, v1.0\n");
42 exit(0);
43 break;
44 case OPT_HELP: /* --help */
45 printf("useful help messages go here\n");
46 exit(0);
47 break;
48 case OPT_SERVERS: /* --servers */
49 opt_servers = strdup(optarg);
50 break;
51 case '?':
52 /* getopt_long already printed an error message. */
53 exit(1);
54 default:
55 abort();
56 }
57 }
58
59 /* todo, turn opt_servers into something to pass to memcached_init */
60 memc= memcached_init(NULL);
61
62 for (x= 1; x < argc; x++)
63 {
64 string= memcached_get(memc, argv[x], strlen(argv[x]),
65 &string_length, &flags, &rc);
66 if (string)
67 {
68 printf("%.*s\n", string_length, string);
69 free(string);
70 }
71 }
72
73 memcached_deinit(memc);
74
75 return 0;
76 };