From patchwork Tue Jun 28 21:27:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 9204025 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 604556074E for ; Tue, 28 Jun 2016 21:32:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 55940285FE for ; Tue, 28 Jun 2016 21:32:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 495122861F; Tue, 28 Jun 2016 21:32:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 397D7285FE for ; Tue, 28 Jun 2016 21:32:46 +0000 (UTC) Received: from localhost ([::1]:40026 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bI0cf-0004Ej-9h for patchwork-qemu-devel@patchwork.kernel.org; Tue, 28 Jun 2016 17:32:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50669) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bI0Xu-0008A0-7o for qemu-devel@nongnu.org; Tue, 28 Jun 2016 17:27:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bI0Xr-0005S8-FB for qemu-devel@nongnu.org; Tue, 28 Jun 2016 17:27:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54925) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bI0Xr-0005S3-6a for qemu-devel@nongnu.org; Tue, 28 Jun 2016 17:27:47 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C1C6D78222; Tue, 28 Jun 2016 21:27:46 +0000 (UTC) Received: from localhost (ovpn-112-80.ams2.redhat.com [10.36.112.80]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5SLRjPm015358; Tue, 28 Jun 2016 17:27:46 -0400 From: Stefan Hajnoczi To: Date: Tue, 28 Jun 2016 22:27:28 +0100 Message-Id: <1467149250-31165-7-git-send-email-stefanha@redhat.com> In-Reply-To: <1467149250-31165-1-git-send-email-stefanha@redhat.com> References: <1467149250-31165-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 28 Jun 2016 21:27:46 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 6/8] 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 , Peter Maydell , Paolo Bonzini , Stefan Hajnoczi , "Denis V. Lunev" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: "Denis V. Lunev" 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 | 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; + /* 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