10 #define PROGRAM_NAME "memcat"
11 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
15 void options_parse(int argc
, char *argv
[]);
17 static int opt_verbose
= 0;
18 static int opt_displayflag
= 0;
19 static char *opt_servers
;
21 int main(int argc
, char *argv
[])
28 memcached_server_st
*servers
;
30 options_parse(argc
, argv
);
36 if ((temp
= getenv("MEMCACHED_SERVERS")))
37 opt_servers
= strdup(temp
);
42 memc
= memcached_create(NULL
);
44 servers
= memcached_servers_parse(opt_servers
);
46 memcached_server_push(memc
, servers
);
47 memcached_server_list_free(servers
);
51 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
52 &string_length
, &flags
, &rc
);
53 if (rc
== MEMCACHED_SUCCESS
)
58 printf("key: %s\nflags: ", argv
[optind
]);
59 printf("%x\n", flags
);
64 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
65 argv
[optind
], flags
, string_length
);
66 printf("%.*s\n", (int)string_length
, string
);
70 else if (rc
!= MEMCACHED_NOTFOUND
)
72 fprintf(stderr
, "memcat: %s: memcache error %s",
73 argv
[optind
], memcached_strerror(memc
, rc
));
74 if (memc
->cached_errno
)
75 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
76 fprintf(stderr
, "\n");
89 void options_parse(int argc
, char *argv
[])
94 memcached_programs_help_st help_options
[]=
99 static struct option long_options
[]=
101 {"version", no_argument
, NULL
, OPT_VERSION
},
102 {"help", no_argument
, NULL
, OPT_HELP
},
103 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
104 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
105 {"servers", required_argument
, NULL
, OPT_SERVERS
},
106 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
112 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
113 if (option_rv
== -1) break;
118 case OPT_VERBOSE
: /* --verbose or -v */
119 opt_verbose
= OPT_VERBOSE
;
121 case OPT_DEBUG
: /* --debug or -d */
122 opt_verbose
= OPT_DEBUG
;
124 case OPT_VERSION
: /* --version or -V */
125 version_command(PROGRAM_NAME
);
127 case OPT_HELP
: /* --help or -h */
128 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
130 case OPT_SERVERS
: /* --servers or -s */
131 opt_servers
= strdup(optarg
);
134 /* getopt_long already printed an error message. */