Received: from eggs.gnu.org ([140.186.70.92]:34989) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFjcH-0002Ya-KJ for qemu-devel@nongnu.org; Fri, 29 Apr 2011 05:04:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QFjcG-0003eK-LS for qemu-devel@nongnu.org; Fri, 29 Apr 2011 05:04:01 -0400 Received: from mx1.redhat.com ([209.132.183.28]:8335) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFjcG-0003e2-Cy for qemu-devel@nongnu.org; Fri, 29 Apr 2011 05:04:00 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p3T93xCN011508 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 29 Apr 2011 05:03:59 -0400 From: Kevin Wolf Date: Fri, 29 Apr 2011 11:06:32 +0200 Message-Id: <1304067992-7088-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH] qemu-img resize: Fix option parsing List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com For shrinking images, you're supposed to use a negative size. However, the leading minus makes getopt think that it's an option and so you get the help text if you don't use -- like in 'qemu-img resize test.img -- -1G'. This patch handles the size first and removes it from the argument list so that getopt won't even try to interpret it and you don't need -- any more. Signed-off-by: Kevin Wolf --- qemu-img.c | 13 +++++++++++-- 1 files changed, 11 insertions(+), 2 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index ed5ba91..e825123 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1442,6 +1442,16 @@ static int img_resize(int argc, char **argv) { NULL } }; + /* Remove size from argv manually so that negative numbers are not treated + * as options by getopt. */ + if (argc < 3) { + help(); + return 1; + } + + size = argv[--argc]; + + /* Parse getopt arguments */ fmt = NULL; for(;;) { c = getopt(argc, argv, "f:h"); @@ -1458,11 +1468,10 @@ static int img_resize(int argc, char **argv) break; } } - if (optind + 1 >= argc) { + if (optind >= argc) { help(); } filename = argv[optind++]; - size = argv[optind++]; /* Choose grow, shrink, or absolute resize mode */ switch (size[0]) { -- 1.7.2.3