add single letter command line options
[awesomized/libmemcached] / src / memcat.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <memcached.h>
5
6 #include "client_options.h"
7 #include "utilities.h"
8
9
10 /* Prototypes */
11 void options_parse(int argc, char *argv[]);
12
13 static int opt_verbose;
14 static int opt_displayflag;
15 static char *opt_servers;
16
17 int main(int argc, char *argv[])
18 {
19 memcached_st *memc;
20 char *string;
21 size_t string_length;
22 uint16_t flags;
23 memcached_return rc;
24
25 memc= memcached_init(NULL);
26
27 options_parse(argc, argv);
28
29 parse_opt_servers(memc, opt_servers);
30
31 while (optind <= argc)
32 {
33 string= memcached_get(memc, argv[argc], strlen(argv[argc]),
34 &string_length, &flags, &rc);
35 if (rc == MEMCACHED_SUCCESS)
36 {
37 if (opt_displayflag)
38 printf("%d\n", flags);
39 else
40 {
41 printf("%.*s\n", string_length, string);
42 free(string);
43 }
44 }
45 optind++;
46 }
47
48 memcached_deinit(memc);
49
50 return 0;
51 };
52
53
54 void options_parse(int argc, char *argv[])
55 {
56 int option_index = 0;
57 int option_rv;
58
59 static struct option long_options[] =
60 {
61 {"version", no_argument, NULL, OPT_VERSION},
62 {"help", no_argument, NULL, OPT_HELP},
63 {"verbose", no_argument, &opt_verbose, 1},
64 {"debug", no_argument, &opt_verbose, 2},
65 {"servers", required_argument, NULL, OPT_SERVERS},
66 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
67 {0, 0, 0, 0},
68 };
69
70 while (1)
71 {
72 option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
73 if (option_rv == -1) break;
74 switch (option_rv) {
75 case 0:
76 break;
77 case OPT_VERSION: /* --version or -V */
78 printf("memcache tools, memcat, v1.0\n");
79 exit(0);
80 break;
81 case OPT_HELP: /* --help or -h */
82 printf("useful help messages go here\n");
83 exit(0);
84 break;
85 case OPT_SERVERS: /* --servers or -s */
86 opt_servers= optarg;
87 break;
88 case '?':
89 /* getopt_long already printed an error message. */
90 exit(1);
91 default:
92 abort();
93 }
94 }
95 }