Browse Source

Convert command line to accept long options that are also self documenting with -h

master
Con Kolivas 11 years ago
parent
commit
1e81ac7887
  1. 2
      configure.ac
  2. 37
      src/ckpool.c

2
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(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(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(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) AC_CHECK_HEADERS(libpq-fe.h postgresql/libpq-fe.h grp.h)
PTHREAD_LIBS="-lpthread" PTHREAD_LIBS="-lpthread"

37
src/ckpool.c

@ -15,7 +15,9 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <ctype.h>
#include <fenv.h> #include <fenv.h>
#include <getopt.h>
#include <grp.h> #include <grp.h>
#include <jansson.h> #include <jansson.h>
#include <signal.h> #include <signal.h>
@ -945,11 +947,25 @@ static void *watchdog(void *arg)
return NULL; 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) int main(int argc, char **argv)
{ {
struct sigaction handler; struct sigaction handler;
int c, ret, i = 0, j;
char buf[512] = {}; char buf[512] = {};
int c, ret, i;
ckpool_t ckp; ckpool_t ckp;
/* Make significant floating point errors fatal to avoid subtle bugs being missed */ /* 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)); memset(&ckp, 0, sizeof(ckp));
ckp.loglevel = LOG_NOTICE; 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) { switch (c) {
case 'c': case 'c':
ckp.config = optarg; ckp.config = optarg;
@ -970,6 +986,23 @@ int main(int argc, char **argv)
case 'g': case 'g':
ckp.grpnam = optarg; ckp.grpnam = optarg;
break; 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': case 'k':
ckp.killold = true; ckp.killold = true;
break; break;

Loading…
Cancel
Save