From 93a7546144c0f58efa7b8e25674a2cb09131cbdb Mon Sep 17 00:00:00 2001 From: Guillaume Lelarge Date: Thu, 20 May 2010 16:40:26 -0400 Subject: [PATCH] Allow a user to use long options, instead of short ones. --help and --version are quite standard. If you only use short options, you never know which short option you should use (-v or -V). --- src/main.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index a0d9f88..141e6cf 100644 --- a/src/main.c +++ b/src/main.c @@ -34,13 +34,13 @@ static bool set_defer_accept(ConfElem *elem, const char *val, PgSocket *console) static const char usage_str[] = "Usage: %s [OPTION]... config.ini\n" -" -d Run in background (as a daemon)\n" -" -R Do a online restart\n" -" -q Run quietly\n" -" -v Increase verbosity\n" -" -u Assume identity of \n" -" -V Show version\n" -" -h Show this help screen and exit\n"; +" -d, --daemon Run in background (as a daemon)\n" +" -R, --restart Do a online restart\n" +" -q, --quiet Run quietly\n" +" -v, --verbose Increase verbosity\n" +" -u, --user= Assume identity of \n" +" -V, --version Show version\n" +" -h, --help Show this help screen and exit\n"; static void usage(int err, char *exe) { @@ -647,9 +647,21 @@ int main(int argc, char *argv[]) int c; bool did_takeover = false; char *arg_username = NULL; + int long_idx; + + static const struct option long_options[] = { + {"quiet", no_argument, NULL, 'q'}, + {"verbose", no_argument, NULL, 'v'}, + {"help", no_argument, NULL, 'h'}, + {"daemon", no_argument, NULL, 'd'}, + {"version", no_argument, NULL, 'V'}, + {"reboot", no_argument, NULL, 'R'}, + {"user", required_argument, NULL, 'u'}, + {NULL, 0, NULL, 0} + }; /* parse cmdline */ - while ((c = getopt(argc, argv, "qvhdVRu:")) != EOF) { + while ((c = getopt_long(argc, argv, "qvhdVRu:", long_options, &long_idx)) != -1) { switch (c) { case 'R': cf_reboot = 1; -- 2.40.0