diff mbox

[4/5] qemu-img: move common options parsing before commands processing

Message ID 1464892545-26544-5-git-send-email-den@openvz.org (mailing list archive)
State New, archived
Headers show

Commit Message

Denis V. Lunev June 2, 2016, 6:35 p.m. UTC
This is necessary to enable creation of common qemu-img options which will
be specified before command.

Signed-off-by: Denis V. Lunev <den@openvz.org>
CC: Eric Blake <eblake@redhat.com>
CC: Paolo Bonzini <pbonzini@redhat.com>
CC: Stefan Hajnoczi <stefanha@redhat.com>
CC: Kevin Wolf <kwolf@redhat.com>
---
 qemu-img.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

Comments

Eric Blake June 3, 2016, 2:50 p.m. UTC | #1
On 06/02/2016 12:35 PM, Denis V. Lunev wrote:
> This is necessary to enable creation of common qemu-img options which will
> be specified before command.
> 
> Signed-off-by: Denis V. Lunev <den@openvz.org>
> CC: Eric Blake <eblake@redhat.com>
> CC: Paolo Bonzini <pbonzini@redhat.com>
> CC: Stefan Hajnoczi <stefanha@redhat.com>
> CC: Kevin Wolf <kwolf@redhat.com>
> ---
>  qemu-img.c | 31 +++++++++++++++++++------------
>  1 file changed, 19 insertions(+), 12 deletions(-)
> 

> -    /* find the command */
> -    for (cmd = img_cmds; cmd->name != NULL; cmd++) {
> -        if (!strcmp(cmdname, cmd->name)) {
> -            return cmd->handler(argc - 1, argv + 1);
> +    while ((c = getopt_long(argc, argv, "+h", long_options, NULL)) != -1) {
> +        switch (c) {
> +        case 'h':
> +            help();
> +            return 0;
> +        case 'v':

Umm, how is 'v' supposed to be hit if it is not passed in the short
option string summary?  That is, 'qemu-img --version' will work, but
'qemu-img -v' won't; while both 'qemu-img --help' and 'qemu-img -h' work.

> +            printf(QEMU_IMG_VERSION);
> +            return 0;
>          }
>      }
>  
> -    c = getopt_long(argc, argv, "h", long_options, NULL);
> +    cmdname = argv[optind];
>  
> -    if (c == 'h') {
> -        help();
> -    }
> -    if (c == 'v') {

On the other hand, it seems to be a pre-existing bug.  Still, it's worth
fixing while you're touching this.

In addition to moving the option processing, you should also update the
documentation (both --help and man page).
diff mbox

Patch

diff --git a/qemu-img.c b/qemu-img.c
index 4b56ad3..aa85b6c 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -3499,26 +3499,33 @@  int main(int argc, char **argv)
     if (argc < 2) {
         error_exit("Not enough arguments");
     }
-    cmdname = argv[1];
 
     qemu_add_opts(&qemu_object_opts);
     qemu_add_opts(&qemu_source_opts);
 
-    /* find the command */
-    for (cmd = img_cmds; cmd->name != NULL; cmd++) {
-        if (!strcmp(cmdname, cmd->name)) {
-            return cmd->handler(argc - 1, argv + 1);
+    while ((c = getopt_long(argc, argv, "+h", long_options, NULL)) != -1) {
+        switch (c) {
+        case 'h':
+            help();
+            return 0;
+        case 'v':
+            printf(QEMU_IMG_VERSION);
+            return 0;
         }
     }
 
-    c = getopt_long(argc, argv, "h", long_options, NULL);
+    cmdname = argv[optind];
 
-    if (c == 'h') {
-        help();
-    }
-    if (c == 'v') {
-        printf(QEMU_IMG_VERSION);
-        return 0;
+    /* reset getopt_long scanning */
+    argc -= optind;
+    argv += optind;
+    optind = 1;
+
+    /* find the command */
+    for (cmd = img_cmds; cmd->name != NULL; cmd++) {
+        if (!strcmp(cmdname, cmd->name)) {
+            return cmd->handler(argc, argv);
+        }
     }
 
     /* not found */