[libvirt] [PATCH] Properly handle -h / -V for --help/--version aliases in virtlockd/libvirtd

Daniel P. Berrange berrange at redhat.com
Tue Aug 13 11:17:06 UTC 2013


From: "Daniel P. Berrange" <berrange at redhat.com>

The virtlockd/libvirtd daemons had listed '?' as the short option
for --help. getopt_long uses '?' for any unknown option. We want
to be able to distinguish unknown options (which use EXIT_FAILURE)
from correct usage of help (which should use EXIT_SUCCESS). Thus
we should use 'h' as a short option for --help. Also add this to
the man page docs

The virtlockd/libvirtd daemons did not list any short option
for the --version arg. Add -V as a valid short option, since
-v is already used for --verbose.

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 daemon/libvirtd.c            | 31 ++++++++++++++-----------------
 daemon/libvirtd.pod.in       |  4 ++++
 src/locking/lock_daemon.c    | 29 +++++++++++++----------------
 src/locking/virtlockd.pod.in |  6 +++++-
 4 files changed, 36 insertions(+), 34 deletions(-)

diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index 402b494..c9cd1a1 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -1038,12 +1038,13 @@ daemonUsage(const char *argv0, bool privileged)
               "  %s [options]\n"
               "\n"
               "Options:\n"
+              "  -h | --help            Display program help:\n"
               "  -v | --verbose         Verbose messages.\n"
               "  -d | --daemon          Run as a daemon & write PID file.\n"
               "  -l | --listen          Listen for TCP/IP connections.\n"
               "  -t | --timeout <secs>  Exit after timeout period.\n"
               "  -f | --config <file>   Configuration file.\n"
-              "     | --version         Display version information.\n"
+              "  -V | --version         Display version information.\n"
               "  -p | --pid-file <file> Change name of PID file.\n"
               "\n"
               "libvirt management daemon:\n"),
@@ -1098,10 +1099,6 @@ daemonUsage(const char *argv0, bool privileged)
     }
 }
 
-enum {
-    OPT_VERSION = 129
-};
-
 #define MAX_LISTEN 5
 int main(int argc, char **argv) {
     virNetServerPtr srv = NULL;
@@ -1123,14 +1120,14 @@ int main(int argc, char **argv) {
     mode_t old_umask;
 
     struct option opts[] = {
-        { "verbose", no_argument, &verbose, 1},
-        { "daemon", no_argument, &godaemon, 1},
-        { "listen", no_argument, &ipsock, 1},
+        { "verbose", no_argument, &verbose, 'v'},
+        { "daemon", no_argument, &godaemon, 'd'},
+        { "listen", no_argument, &ipsock, 'l'},
         { "config", required_argument, NULL, 'f'},
         { "timeout", required_argument, NULL, 't'},
         { "pid-file", required_argument, NULL, 'p'},
-        { "version", no_argument, NULL, OPT_VERSION },
-        { "help", no_argument, NULL, '?' },
+        { "version", no_argument, NULL, 'V' },
+        { "help", no_argument, NULL, 'h' },
         {0, 0, 0, 0}
     };
 
@@ -1173,7 +1170,7 @@ int main(int argc, char **argv) {
         int c;
         char *tmp;
 
-        c = getopt_long(argc, argv, "ldf:p:t:v", opts, &optidx);
+        c = getopt_long(argc, argv, "ldf:p:t:vVh", opts, &optidx);
 
         if (c == -1) {
             break;
@@ -1219,17 +1216,17 @@ int main(int argc, char **argv) {
             }
             break;
 
-        case OPT_VERSION:
+        case 'V':
             daemonVersion(argv[0]);
-            return 0;
+            exit(EXIT_SUCCESS);
 
-        case '?':
+        case 'h':
             daemonUsage(argv[0], privileged);
-            return 2;
+            exit(EXIT_SUCCESS);
 
+        case '?':
         default:
-            VIR_ERROR(_("%s: internal error: unknown flag: %c"),
-                      argv[0], c);
+            daemonUsage(argv[0], privileged);
             exit(EXIT_FAILURE);
         }
     }
diff --git a/daemon/libvirtd.pod.in b/daemon/libvirtd.pod.in
index 930b752..9901ecf 100644
--- a/daemon/libvirtd.pod.in
+++ b/daemon/libvirtd.pod.in
@@ -36,6 +36,10 @@ from the configuration.
 
 =over
 
+=item B<-h, --help>
+
+Display command line help usage then exit.
+
 =item B<-d, --daemon>
 
 Run as a daemon & write PID file.
diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c
index c45f45c..77d6e0d 100644
--- a/src/locking/lock_daemon.c
+++ b/src/locking/lock_daemon.c
@@ -1096,10 +1096,11 @@ virLockDaemonUsage(const char *argv0, bool privileged)
               "  %s [options]\n"
               "\n"
               "Options:\n"
+              "  -h | --help            Display program help:\n"
               "  -v | --verbose         Verbose messages.\n"
               "  -d | --daemon          Run as a daemon & write PID file.\n"
               "  -f | --config <file>   Configuration file.\n"
-              "     | --version         Display version information.\n"
+              "  -V | --version         Display version information.\n"
               "  -p | --pid-file <file> Change name of PID file.\n"
               "\n"
               "libvirt lock management daemon:\n"), argv0);
@@ -1138,10 +1139,6 @@ virLockDaemonUsage(const char *argv0, bool privileged)
     }
 }
 
-enum {
-    OPT_VERSION = 129
-};
-
 #define MAX_LISTEN 5
 int main(int argc, char **argv) {
     virNetServerProgramPtr lockProgram = NULL;
@@ -1161,12 +1158,12 @@ int main(int argc, char **argv) {
     int rv;
 
     struct option opts[] = {
-        { "verbose", no_argument, &verbose, 1},
-        { "daemon", no_argument, &godaemon, 1},
+        { "verbose", no_argument, &verbose, 'v'},
+        { "daemon", no_argument, &godaemon, 'd'},
         { "config", required_argument, NULL, 'f'},
         { "pid-file", required_argument, NULL, 'p'},
-        { "version", no_argument, NULL, OPT_VERSION },
-        { "help", no_argument, NULL, '?' },
+        { "version", no_argument, NULL, 'V' },
+        { "help", no_argument, NULL, 'h' },
         {0, 0, 0, 0}
     };
 
@@ -1185,7 +1182,7 @@ int main(int argc, char **argv) {
         int optidx = 0;
         int c;
 
-        c = getopt_long(argc, argv, "ldf:p:t:v", opts, &optidx);
+        c = getopt_long(argc, argv, "ldf:p:t:vVh", opts, &optidx);
 
         if (c == -1) {
             break;
@@ -1218,17 +1215,17 @@ int main(int argc, char **argv) {
             }
             break;
 
-        case OPT_VERSION:
+        case 'V':
             virLockDaemonVersion(argv[0]);
-            return 0;
+            exit(EXIT_SUCCESS);
 
-        case '?':
+        case 'h':
             virLockDaemonUsage(argv[0], privileged);
-            return 2;
+            exit(EXIT_SUCCESS);
 
+        case '?':
         default:
-            fprintf(stderr, _("%s: internal error: unknown flag: %c\n"),
-                    argv[0], c);
+            virLockDaemonUsage(argv[0], privileged);
             exit(EXIT_FAILURE);
         }
     }
diff --git a/src/locking/virtlockd.pod.in b/src/locking/virtlockd.pod.in
index f5748ca..99612aa 100644
--- a/src/locking/virtlockd.pod.in
+++ b/src/locking/virtlockd.pod.in
@@ -26,6 +26,10 @@ The virtlockd daemon listens for requests on a local Unix domain socket.
 
 =over
 
+=item B<-h, --help>
+
+Display command line help usage then exit.
+
 =item B<-d, --daemon>
 
 Run as a daemon and write PID file.
@@ -42,7 +46,7 @@ Use this name for the PID file, overriding the default value.
 
 Enable output of verbose messages.
 
-=item B<    --version>
+=item B<-V, --version>
 
 Display version information then exit.
 
-- 
1.8.3.1




More information about the libvir-list mailing list