From 1e81ac7887bcc914a0a2ce7930f5214f5b3ce9ca Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 18 Jul 2014 22:06:02 +1000 Subject: [PATCH] Convert command line to accept long options that are also self documenting with -h --- configure.ac | 2 +- src/ckpool.c | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 1059c64d..ad5d186b 100644 --- a/configure.ac +++ b/configure.ac @@ -36,7 +36,7 @@ AC_CHECK_HEADERS(ctype.h errno.h byteswap.h string.h time.h fenv.h) AC_CHECK_HEADERS(endian.h sys/endian.h arpa/inet.h sys/poll.h syslog.h) AC_CHECK_HEADERS(alloca.h pthread.h stdio.h math.h signal.h sys/prctl.h) AC_CHECK_HEADERS(sys/types.h sys/socket.h sys/stat.h linux/un.h netdb.h) -AC_CHECK_HEADERS(stdint.h netinet/in.h netinet/tcp.h sys/ioctl.h) +AC_CHECK_HEADERS(stdint.h netinet/in.h netinet/tcp.h sys/ioctl.h getopt.h) AC_CHECK_HEADERS(libpq-fe.h postgresql/libpq-fe.h grp.h) PTHREAD_LIBS="-lpthread" diff --git a/src/ckpool.c b/src/ckpool.c index 56fe3a08..5572cb6f 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -15,7 +15,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -945,11 +947,25 @@ static void *watchdog(void *arg) return NULL; } +static struct option long_options[] = { + {"config", required_argument, 0, 'c'}, + {"ckdb-name", required_argument, 0, 'd'}, + {"group", required_argument, 0, 'g'}, + {"help", no_argument, 0, 'h'}, + {"killold", no_argument, 0, 'k'}, + {"loglevel", required_argument, 0, 'l'}, + {"name", required_argument, 0, 'n'}, + {"proxy", no_argument, 0, 'p'}, + {"ckdb-sockdir",required_argument, 0, 'S'}, + {"sockdir", required_argument, 0, 's'}, + {0, 0, 0, 0} +}; + int main(int argc, char **argv) { struct sigaction handler; + int c, ret, i = 0, j; char buf[512] = {}; - int c, ret, i; ckpool_t ckp; /* Make significant floating point errors fatal to avoid subtle bugs being missed */ @@ -959,7 +975,7 @@ int main(int argc, char **argv) memset(&ckp, 0, sizeof(ckp)); ckp.loglevel = LOG_NOTICE; - while ((c = getopt(argc, argv, "c:d:g:kl:n:pS:s:")) != -1) { + while ((c = getopt_long(argc, argv, "c:d:g:hkl:n:pS:s:", long_options, &i)) != -1) { switch (c) { case 'c': ckp.config = optarg; @@ -970,6 +986,23 @@ int main(int argc, char **argv) case 'g': ckp.grpnam = optarg; break; + case 'h': + for (j = 0; long_options[j].val; j++) { + struct option *jopt = &long_options[j]; + + if (jopt->has_arg) { + char *upper = alloca(strlen(jopt->name) + 1); + int offset = 0; + + do { + upper[offset] = toupper(jopt->name[offset]); + } while (upper[offset++] != '\0'); + printf("-%c %s | --%s %s\n", jopt->val, + upper, jopt->name, upper); + } else + printf("-%c | %s\n", jopt->val, jopt->name); + } + exit(0); case 'k': ckp.killold = true; break;