From patchwork Fri Jun 17 14:44:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Denis V. Lunev" X-Patchwork-Id: 637136 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rWPGp2kqKz9t0c for ; Sat, 18 Jun 2016 01:26:10 +1000 (AEST) Received: from localhost ([::1]:58348 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDveq-00025O-7X for incoming@patchwork.ozlabs.org; Fri, 17 Jun 2016 11:26:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54630) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDv0h-0008FM-Sn for qemu-devel@nongnu.org; Fri, 17 Jun 2016 10:44:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bDv0f-0007Mh-SL for qemu-devel@nongnu.org; Fri, 17 Jun 2016 10:44:38 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:21175 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDv0W-0007KT-41; Fri, 17 Jun 2016 10:44:28 -0400 Received: from irbis.sw.ru ([10.30.2.139]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id u5HEiEko004617; Fri, 17 Jun 2016 17:44:26 +0300 (MSK) From: "Denis V. Lunev" To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Fri, 17 Jun 2016 17:44:13 +0300 Message-Id: <1466174654-30130-7-git-send-email-den@openvz.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1466174654-30130-1-git-send-email-den@openvz.org> References: <1466174654-30130-1-git-send-email-den@openvz.org> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 6/7] qemu-img: move common options parsing before commands processing X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , "Denis V. Lunev" , stefanha@redhat.com, Paolo Bonzini Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 CC: Paolo Bonzini CC: Kevin Wolf --- qemu-img.c | 39 ++++++++++++++++++++++++++------------- qemu-img.texi | 10 +++++++++- 2 files changed, 35 insertions(+), 14 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,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); 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