diff mbox

[PULL,6/8] qemu-img: move common options parsing before commands processing

Message ID 1467149250-31165-7-git-send-email-stefanha@redhat.com
State New
Headers show

Commit Message

Stefan Hajnoczi June 28, 2016, 9:27 p.m. UTC
From: "Denis V. Lunev" <den@openvz.org>

This is necessary to enable creation of common qemu-img options which will
be specified before command.

The patch also enables '-V' alias to '--version' (exactly like in other
block utilities) and documents this change.

Signed-off-by: Denis V. Lunev <den@openvz.org>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 1466174654-30130-7-git-send-email-den@openvz.org
CC: Paolo Bonzini <pbonzini@redhat.com>
CC: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 qemu-img.c    | 41 +++++++++++++++++++++++++++--------------
 qemu-img.texi | 10 +++++++++-
 2 files changed, 36 insertions(+), 15 deletions(-)

Comments

Denis V. Lunev June 29, 2016, 8:22 a.m. UTC | #1
On 06/29/2016 12:27 AM, Stefan Hajnoczi wrote:
> From: "Denis V. Lunev" <den@openvz.org>
>
> This is necessary to enable creation of common qemu-img options which will
> be specified before command.
>
> The patch also enables '-V' alias to '--version' (exactly like in other
> block utilities) and documents this change.
>
> Signed-off-by: Denis V. Lunev <den@openvz.org>
> Reviewed-by: Eric Blake <eblake@redhat.com>
> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
> Message-id: 1466174654-30130-7-git-send-email-den@openvz.org
> CC: Paolo Bonzini <pbonzini@redhat.com>
> CC: Kevin Wolf <kwolf@redhat.com>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>   qemu-img.c    | 41 +++++++++++++++++++++++++++--------------
>   qemu-img.texi | 10 +++++++++-
>   2 files changed, 36 insertions(+), 15 deletions(-)
>
> diff --git a/qemu-img.c b/qemu-img.c
> index 14e2661..2194c2d 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -91,9 +91,12 @@ static void QEMU_NORETURN help(void)
>   {
>       const char *help_msg =
>              QEMU_IMG_VERSION
> -           "usage: qemu-img command [command options]\n"
> +           "usage: qemu-img [standard options] command [command options]\n"
>              "QEMU disk image utility\n"
>              "\n"
> +           "    '-h', '--help'       display this help and exit\n"
> +           "    '-V', '--version'    output version information and exit\n"
> +           "\n"
>              "Command syntax:\n"
>   #define DEF(option, callback, arg_string)        \
>              "  " arg_string "\n"
> @@ -3806,7 +3809,7 @@ int main(int argc, char **argv)
>       int c;
>       static const struct option long_options[] = {
>           {"help", no_argument, 0, 'h'},
> -        {"version", no_argument, 0, 'v'},
> +        {"version", no_argument, 0, 'V'},
>           {0, 0, 0, 0}
>       };
>   
> @@ -3829,28 +3832,38 @@ 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);
>   
> +    while ((c = getopt_long(argc, argv, "+hV", long_options, NULL)) != -1) {
> +        switch (c) {
> +        case 'h':
> +            help();
> +            return 0;
> +        case 'V':
> +            printf(QEMU_IMG_VERSION);
> +            return 0;
> +        }
> +    }
> +
> +    cmdname = argv[optind];
> +
> +    /* reset getopt_long scanning */
> +    argc -= optind;
> +    if (argc < 1) {
> +        return 0;
> +    }
> +    argv += optind;
> +    optind = 1;
this patch breaks check-block.sh
we should have here
   'optind = 0'

Den
Eric Blake July 1, 2016, 4:39 p.m. UTC | #2
On 06/29/2016 02:22 AM, Denis V. Lunev wrote:
> On 06/29/2016 12:27 AM, Stefan Hajnoczi wrote:
>> From: "Denis V. Lunev" <den@openvz.org>
>>
>> This is necessary to enable creation of common qemu-img options which
>> will
>> be specified before command.

>> +    cmdname = argv[optind];
>> +
>> +    /* reset getopt_long scanning */
>> +    argc -= optind;
>> +    if (argc < 1) {
>> +        return 0;
>> +    }
>> +    argv += optind;
>> +    optind = 1;
> this patch breaks check-block.sh
> we should have here
>   'optind = 0'

Uggh. You've landed in one of the portability traps of getopt().  On
glibc, you can reset option parsing by assigning optind to 1 (but it
also works by assigning it to 0); on BSD, you instead set the variable
'optreset'; on Solaris, there is no way to reset option parsing (but
then again, Solaris doesn't maintain any invisible state like glibc or
BSD where you need to go out of your way to do a full reset).

But regardless of how you look at it, the patch has now been committed
to master without your suggested fix, so you'll have to propose a formal
followup patch as a new thread.
diff mbox

Patch

diff --git a/qemu-img.c b/qemu-img.c
index 14e2661..2194c2d 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -91,9 +91,12 @@  static void QEMU_NORETURN help(void)
 {
     const char *help_msg =
            QEMU_IMG_VERSION
-           "usage: qemu-img command [command options]\n"
+           "usage: qemu-img [standard options] command [command options]\n"
            "QEMU disk image utility\n"
            "\n"
+           "    '-h', '--help'       display this help and exit\n"
+           "    '-V', '--version'    output version information and exit\n"
+           "\n"
            "Command syntax:\n"
 #define DEF(option, callback, arg_string)        \
            "  " arg_string "\n"
@@ -3806,7 +3809,7 @@  int main(int argc, char **argv)
     int c;
     static const struct option long_options[] = {
         {"help", no_argument, 0, 'h'},
-        {"version", no_argument, 0, 'v'},
+        {"version", no_argument, 0, 'V'},
         {0, 0, 0, 0}
     };
 
@@ -3829,28 +3832,38 @@  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);
 
+    while ((c = getopt_long(argc, argv, "+hV", long_options, NULL)) != -1) {
+        switch (c) {
+        case 'h':
+            help();
+            return 0;
+        case 'V':
+            printf(QEMU_IMG_VERSION);
+            return 0;
+        }
+    }
+
+    cmdname = argv[optind];
+
+    /* reset getopt_long scanning */
+    argc -= optind;
+    if (argc < 1) {
+        return 0;
+    }
+    argv += optind;
+    optind = 1;
+
     /* find the command */
     for (cmd = img_cmds; cmd->name != NULL; cmd++) {
         if (!strcmp(cmdname, cmd->name)) {
-            return cmd->handler(argc - 1, argv + 1);
+            return cmd->handler(argc, argv);
         }
     }
 
-    c = getopt_long(argc, argv, "h", long_options, NULL);
-
-    if (c == 'h') {
-        help();
-    }
-    if (c == 'v') {
-        printf(QEMU_IMG_VERSION);
-        return 0;
-    }
-
     /* not found */
     error_exit("Command not found: %s", cmdname);
 }
diff --git a/qemu-img.texi b/qemu-img.texi
index cbe50e9..f1b874d 100644
--- a/qemu-img.texi
+++ b/qemu-img.texi
@@ -1,6 +1,6 @@ 
 @example
 @c man begin SYNOPSIS
-@command{qemu-img} @var{command} [@var{command} @var{options}]
+@command{qemu-img} [@var{standard} @var{options}] @var{command} [@var{command} @var{options}]
 @c man end
 @end example
 
@@ -16,6 +16,14 @@  inconsistent state.
 
 @c man begin OPTIONS
 
+Standard options:
+@table @option
+@item -h, --help
+Display this help and exit
+@item -V, --version
+Display version information and exit
+@end table
+
 The following commands are supported:
 
 @include qemu-img-cmds.texi