0a122b
From e8f877d31ff4ffbd7c09744f7244b21c728281be Mon Sep 17 00:00:00 2001
0a122b
From: Kevin Wolf <kwolf@redhat.com>
0a122b
Date: Tue, 25 Feb 2014 15:00:04 +0100
0a122b
Subject: [PATCH 6/7] qemu-img: Allow -o help with incomplete argument list
0a122b
0a122b
RH-Author: Kevin Wolf <kwolf@redhat.com>
0a122b
Message-id: <1393340405-9936-6-git-send-email-kwolf@redhat.com>
0a122b
Patchwork-id: 57796
0a122b
O-Subject: [RHEL-7.0 qemu-kvm PATCH 5/6] qemu-img: Allow -o help with incomplete argument list
0a122b
Bugzilla: 1065873
0a122b
RH-Acked-by: Juan Quintela <quintela@redhat.com>
0a122b
RH-Acked-by: Fam Zheng <famz@redhat.com>
0a122b
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
0a122b
0a122b
This patch allows using 'qemu-img $subcmd -o help' for the create,
0a122b
convert and amend subcommands, without specifying the previously
0a122b
required filename arguments.
0a122b
0a122b
Note that it's still allowed and meaningful to specify a filename: An
0a122b
invocation like 'qemu-img create -o help sheepdog:foo' will also display
0a122b
options that are provided by the Sheepdog driver.
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
Reviewed-by: Jeff Cody <jcody@redhat.com>
0a122b
Reviewed-by: Eric Blake <eblake@redhat.com>
0a122b
(cherry picked from commit a283cb6e58fca846c658360971d23fdd1129db65)
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
---
0a122b
 qemu-img.c | 58 +++++++++++++++++++++++++++++++++++-----------------------
0a122b
 1 file changed, 35 insertions(+), 23 deletions(-)
0a122b
0a122b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
0a122b
---
0a122b
 qemu-img.c |   58 +++++++++++++++++++++++++++++++++++-----------------------
0a122b
 1 files changed, 35 insertions(+), 23 deletions(-)
0a122b
0a122b
diff --git a/qemu-img.c b/qemu-img.c
0a122b
index 5002a56..3dc325e 100644
0a122b
--- a/qemu-img.c
0a122b
+++ b/qemu-img.c
0a122b
@@ -244,16 +244,19 @@ static int print_block_option_help(const char *filename, const char *fmt)
0a122b
         return 1;
0a122b
     }
0a122b
 
0a122b
-    proto_drv = bdrv_find_protocol(filename, true);
0a122b
-    if (!proto_drv) {
0a122b
-        error_report("Unknown protocol '%s'", filename);
0a122b
-        return 1;
0a122b
-    }
0a122b
-
0a122b
     create_options = append_option_parameters(create_options,
0a122b
                                               drv->create_options);
0a122b
-    create_options = append_option_parameters(create_options,
0a122b
-                                              proto_drv->create_options);
0a122b
+
0a122b
+    if (filename) {
0a122b
+        proto_drv = bdrv_find_protocol(filename, true);
0a122b
+        if (!proto_drv) {
0a122b
+            error_report("Unknown protocol '%s'", filename);
0a122b
+            return 1;
0a122b
+        }
0a122b
+        create_options = append_option_parameters(create_options,
0a122b
+                                                  proto_drv->create_options);
0a122b
+    }
0a122b
+
0a122b
     print_option_help(create_options);
0a122b
     free_option_parameters(create_options);
0a122b
     return 0;
0a122b
@@ -390,10 +393,16 @@ static int img_create(int argc, char **argv)
0a122b
     }
0a122b
 
0a122b
     /* Get the filename */
0a122b
+    filename = (optind < argc) ? argv[optind] : NULL;
0a122b
+    if (options && has_help_option(options)) {
0a122b
+        g_free(options);
0a122b
+        return print_block_option_help(filename, fmt);
0a122b
+    }
0a122b
+
0a122b
     if (optind >= argc) {
0a122b
         help();
0a122b
     }
0a122b
-    filename = argv[optind++];
0a122b
+    optind++;
0a122b
 
0a122b
     /* Get image size, if specified */
0a122b
     if (optind < argc) {
0a122b
@@ -416,11 +425,6 @@ static int img_create(int argc, char **argv)
0a122b
         help();
0a122b
     }
0a122b
 
0a122b
-    if (options && has_help_option(options)) {
0a122b
-        g_free(options);
0a122b
-        return print_block_option_help(filename, fmt);
0a122b
-    }
0a122b
-
0a122b
     bdrv_img_create(filename, fmt, base_filename, base_fmt,
0a122b
                     options, img_size, BDRV_O_FLAGS, &local_err, quiet);
0a122b
     if (error_is_set(&local_err)) {
0a122b
@@ -1251,17 +1255,18 @@ static int img_convert(int argc, char **argv)
0a122b
     }
0a122b
 
0a122b
     bs_n = argc - optind - 1;
0a122b
-    if (bs_n < 1) {
0a122b
-        help();
0a122b
-    }
0a122b
-
0a122b
-    out_filename = argv[argc - 1];
0a122b
+    out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
0a122b
 
0a122b
     if (options && has_help_option(options)) {
0a122b
         ret = print_block_option_help(out_filename, out_fmt);
0a122b
         goto out;
0a122b
     }
0a122b
 
0a122b
+    if (bs_n < 1) {
0a122b
+        help();
0a122b
+    }
0a122b
+
0a122b
+
0a122b
     if (bs_n > 1 && out_baseimg) {
0a122b
         error_report("-B makes no sense when concatenating multiple input "
0a122b
                      "images");
0a122b
@@ -2639,15 +2644,21 @@ static int img_amend(int argc, char **argv)
0a122b
         }
0a122b
     }
0a122b
 
0a122b
-    if (optind != argc - 1) {
0a122b
+    if (!options) {
0a122b
         help();
0a122b
     }
0a122b
 
0a122b
-    if (!options) {
0a122b
-        help();
0a122b
+    filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
0a122b
+    if (fmt && has_help_option(options)) {
0a122b
+        /* If a format is explicitly specified (and possibly no filename is
0a122b
+         * given), print option help here */
0a122b
+        ret = print_block_option_help(filename, fmt);
0a122b
+        goto out;
0a122b
     }
0a122b
 
0a122b
-    filename = argv[argc - 1];
0a122b
+    if (optind != argc - 1) {
0a122b
+        help();
0a122b
+    }
0a122b
 
0a122b
     bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
0a122b
     if (!bs) {
0a122b
@@ -2659,6 +2670,7 @@ static int img_amend(int argc, char **argv)
0a122b
     fmt = bs->drv->format_name;
0a122b
 
0a122b
     if (has_help_option(options)) {
0a122b
+        /* If the format was auto-detected, print option help here */
0a122b
         ret = print_block_option_help(filename, fmt);
0a122b
         goto out;
0a122b
     }
0a122b
-- 
0a122b
1.7.1
0a122b