From patchwork Mon Apr 28 18:37:18 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Cody X-Patchwork-Id: 343506 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 ozlabs.org (Postfix) with ESMTPS id E24151400A1 for ; Tue, 29 Apr 2014 04:38:04 +1000 (EST) Received: from localhost ([::1]:45437 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WeqRG-0007OT-8x for incoming@patchwork.ozlabs.org; Mon, 28 Apr 2014 14:38:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50246) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WeqQq-00075t-Da for qemu-devel@nongnu.org; Mon, 28 Apr 2014 14:37:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WeqQd-0007gC-IN for qemu-devel@nongnu.org; Mon, 28 Apr 2014 14:37:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50134) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WeqQd-0007dD-Ac for qemu-devel@nongnu.org; Mon, 28 Apr 2014 14:37:23 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s3SIbLaV027073 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 28 Apr 2014 14:37:21 -0400 Received: from localhost (ovpn-112-43.phx2.redhat.com [10.3.112.43]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s3SIbJln016563 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NO); Mon, 28 Apr 2014 14:37:20 -0400 From: Jeff Cody To: qemu-devel@nongnu.org Date: Mon, 28 Apr 2014 14:37:18 -0400 Message-Id: <8fb8959efcbf66f8fe6eacafdd86c44b369406bf.1398710046.git.jcody@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH] block: Add '--version' option to qemu-img X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This allows qemu-img to print out version information, without needing to print the long help wall of text. While there, perform some minor whitespace cleanup, and remove the unused option_index variable in the call to getopt_long(). Reported-by: Eric Blake Signed-off-by: Jeff Cody Reviewed-by: Eric Blake --- qemu-img.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index d884324..96f4463 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -33,6 +33,9 @@ #include "block/qapi.h" #include +#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION \ + ", Copyright (c) 2004-2008 Fabrice Bellard\n" + typedef struct img_cmd_t { const char *name; int (*handler)(int argc, char **argv); @@ -75,7 +78,7 @@ static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) static void QEMU_NORETURN help(void) { const char *help_msg = - "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n" + QEMU_IMG_VERSION "usage: qemu-img command [command options]\n" "QEMU disk image utility\n" "\n" @@ -2790,9 +2793,9 @@ int main(int argc, char **argv) const img_cmd_t *cmd; const char *cmdname; int c; - int option_index = 0; static const struct option long_options[] = { {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; @@ -2811,17 +2814,21 @@ int main(int argc, char **argv) cmdname = argv[1]; /* find the command */ - for(cmd = img_cmds; cmd->name != NULL; cmd++) { + for (cmd = img_cmds; cmd->name != NULL; cmd++) { if (!strcmp(cmdname, cmd->name)) { return cmd->handler(argc - 1, argv + 1); } } - c = getopt_long(argc, argv, "h", long_options, &option_index); + 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);