From 10985131e337a0c52c5bd1e191fd7867a6ff8d02 Mon Sep 17 00:00:00 2001 From: "Denis V. Lunev" Date: Fri, 17 Jun 2016 17:44:13 +0300 Subject: qemu-img: move common options parsing before commands processing 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 Reviewed-by: Eric Blake Reviewed-by: Stefan Hajnoczi Message-id: 1466174654-30130-7-git-send-email-den@openvz.org CC: Paolo Bonzini CC: Kevin Wolf Signed-off-by: Stefan Hajnoczi --- qemu-img.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 14e2661a5c..2194c2d8e6 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,27 +3832,37 @@ 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, "+hV", 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); + /* 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, argv); + } + } /* not found */ error_exit("Command not found: %s", cmdname); -- cgit v1.2.1