In order to be able to give correct --help output, pass current cmd
information (img_cmd_t structure) to command handlers and to common
error reporting functions. After the change, in case of command-line
error, qemu-img will now print:
Try 'qemu-img create --help' for more information.
Current cmd info will be useful in --help output as well.
Signed-off-by: Michael Tokarev
---
qemu-img.c | 150 ++---
1 file changed, 75 insertions(+), 75 deletions(-)
diff --git a/qemu-img.c b/qemu-img.c
index 7668f86769..05f80b6e5b 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -60,7 +60,7 @@
typedef struct img_cmd_t {
const char *name;
-int (*handler)(int argc, char **argv);
+int (*handler)(const struct img_cmd_t *ccmd, int argc, char **argv);
} img_cmd_t;
enum {
@@ -101,8 +101,8 @@ static void format_print(void *opaque, const char *name)
printf(" %s", name);
}
-static G_NORETURN G_GNUC_PRINTF(1, 2)
-void error_exit(const char *fmt, ...)
+static G_NORETURN G_GNUC_PRINTF(2, 3)
+void error_exit(const img_cmd_t *ccmd, const char *fmt, ...)
{
va_list ap;
@@ -110,20 +110,20 @@ void error_exit(const char *fmt, ...)
error_vreport(fmt, ap);
va_end(ap);
-error_printf("Try 'qemu-img --help' for more information\n");
+error_printf("Try 'qemu-img %s --help' for more information\n", ccmd ?
ccmd->name : "");
exit(EXIT_FAILURE);
}
static G_NORETURN
-void missing_argument(const char *option)
+void missing_argument(const img_cmd_t *ccmd, const char *option)
{
-error_exit("missing argument for option '%s'", option);
+error_exit(ccmd, "missing argument for option '%s'", option);
}
static G_NORETURN
-void unrecognized_option(const char *option)
+void unrecognized_option(const img_cmd_t *ccmd, const char *option)
{
-error_exit("unrecognized option '%s'", option);
+error_exit(ccmd, "unrecognized option '%s'", option);
}
/* Please keep in synch with docs/tools/qemu-img.rst */
@@ -508,7 +508,7 @@ static int64_t cvtnum(const char *name, const char *value)
return cvtnum_full(name, value, 0, INT64_MAX);
}
-static int img_create(int argc, char **argv)
+static int img_create(const img_cmd_t *ccmd, int argc, char **argv)
{
int c;
uint64_t img_size = -1;
@@ -534,10 +534,10 @@ static int img_create(int argc, char **argv)
}
switch(c) {
case ':':
-missing_argument(argv[optind - 1]);
+missing_argument(ccmd, argv[optind - 1]);
break;
case '?':
-unrecognized_option(argv[optind - 1]);
+unrecognized_option(ccmd, argv[optind - 1]);
break;
case 'h':
help();
@@ -576,7 +576,7 @@ static int img_create(int argc, char **argv)
}
if (optind >= argc) {
-error_exit("Expecting image file name");
+error_exit(ccmd, "Expecting image file name");
}
optind++;
@@ -591,7 +591,7 @@ static int img_create(int argc, char **argv)
img_size = (uint64_t)sval;
}
if (optind != argc) {
-error_exit("Unexpected argument: %s", argv[optind]);
+error_exit(ccmd, "Unexpected argument: %s", argv[optind]);
}
bdrv_img_create(filename, fmt, base_filename, base_fmt,
@@ -716,7 +716,7 @@ static int collect_image_check(BlockDriverState *bs,
* 3 - Check completed, image has leaked clusters, but is good otherwise
* 63 - Checks are not supported by the image format
*/
-static int img_check(int argc, char **argv)
+static int img_check(const img_cmd_t *ccmd, int argc, char **argv)
{
int c, ret;
OutputFormat output_format = OFORMAT_HUMAN;
@@ -754,10 +754,10 @@ static int img_check(int argc, char **argv)
}
switch(c) {
case ':':
-missing_argument(argv[optind - 1]);
+missing_argument(ccmd, argv[optind - 1]);
break;
case '?':
-unrecognized_option(argv[optind - 1]);
+unrecognized_option(ccmd, argv[optind - 1]);
break;
case 'h':
help();
@@ -773,7 +773,7 @@ static int img_check(int argc, char **argv)
} else if (!strcmp(optarg, "all")) {
fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
} else {
-error_exit("Unknown option value for -r "
+error_exit(ccmd, "Unknown option value for -r "
"(expecting 'leaks' or 'all'): %s", optarg);
}
break;
@@ -798,7 +798,7 @@ static int img_check(int argc, char **argv)
}
}
if (optind != argc - 1) {
-error_exit("Expecting one image file name");
+error_exit(ccmd, "Expecting one image file name");
}
filename = argv[optind++];
@@ -948,7 +948,7 @@ static void run_block_job(BlockJob *job, Error **errp)
}
}
-static int img_commit(int argc, char **argv)
+static int img_commit(const img_cmd_t *ccmd,