8 #define PROGRAM_NAME "memcat"
9 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
13 void options_parse(int argc
, char *argv
[]);
15 static int opt_verbose
= 0;
16 static int opt_displayflag
= 0;
17 static char *opt_servers
;
19 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
);
40 memc
= memcached_create(NULL
);
42 servers
= memcached_servers_parse(opt_servers
);
44 memcached_server_push(memc
, servers
);
45 memcached_server_list_free(servers
);
49 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
50 &string_length
, &flags
, &rc
);
51 if (rc
== MEMCACHED_SUCCESS
)
56 printf("key: %s\nflags: ", argv
[optind
]);
57 printf("%x\n", flags
);
62 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
63 argv
[optind
], flags
, string_length
);
64 printf("%.*s\n", (int)string_length
, string
);
68 else if (rc
!= MEMCACHED_NOTFOUND
)
70 fprintf(stderr
, "memcat: %s: memcache error %s\n",
71 argv
[optind
], memcached_strerror(memc
, rc
));
84 void options_parse(int argc
, char *argv
[])
89 memcached_programs_help_st help_options
[]=
94 static struct option long_options
[]=
96 {"version", no_argument
, NULL
, OPT_VERSION
},
97 {"help", no_argument
, NULL
, OPT_HELP
},
98 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
99 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
100 {"servers", required_argument
, NULL
, OPT_SERVERS
},
101 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
107 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
108 if (option_rv
== -1) break;
113 case OPT_VERBOSE
: /* --verbose or -v */
114 opt_verbose
= OPT_VERBOSE
;
116 case OPT_DEBUG
: /* --debug or -d */
117 opt_verbose
= OPT_DEBUG
;
119 case OPT_VERSION
: /* --version or -V */
120 version_command(PROGRAM_NAME
);
122 case OPT_HELP
: /* --help or -h */
123 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
125 case OPT_SERVERS
: /* --servers or -s */
126 opt_servers
= strdup(optarg
);
129 /* getopt_long already printed an error message. */