[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 18/27] qemu-img: resize: do not always eat last argument
From: |
Michael Tokarev |
Subject: |
[PATCH 18/27] qemu-img: resize: do not always eat last argument |
Date: |
Wed, 24 Apr 2024 11:50:26 +0300 |
'qemu-img resize --help' does not work, since it wants more
arguments. Also -size is only recognized as a very last
argument, but it is common for tools to handle other options
after positional arguments too.
Tell getopt_long() to return non-options together with options,
and process filename and size in the loop, and check if there's
an argument right after filename which looks like -N (number),
and treat it as size (decrement). This way we can handle --help,
and we can also have options after filename and size, and `--'
will be handled fine too.
The only case which is not handled right is when there's an option
between filename and size, and size is given as decrement, - in
this case -size will be treated as option, not as size.
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
---
qemu-img.c | 41 +++++++++++++++++++++++++++--------------
1 file changed, 27 insertions(+), 14 deletions(-)
diff --git a/qemu-img.c b/qemu-img.c
index 47dfa137c1..da10fafffc 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -4297,7 +4297,7 @@ static int img_resize(const img_cmd_t *ccmd, int argc,
char **argv)
{
Error *err = NULL;
int c, ret, relative;
- const char *filename, *fmt, *size;
+ const char *filename = NULL, *fmt = NULL, *size = NULL;
int64_t n, total_size, current_size;
bool quiet = false;
BlockBackend *blk = NULL;
@@ -4320,17 +4320,7 @@ static int img_resize(const img_cmd_t *ccmd, int argc,
char **argv)
bool image_opts = false;
bool shrink = false;
- /* Remove size from argv manually so that negative numbers are not treated
- * as options by getopt. */
- if (argc < 3) {
- error_exit(argv[0], "Not enough arguments");
- return 1;
- }
-
- size = argv[--argc];
-
/* Parse getopt arguments */
- fmt = NULL;
for(;;) {
static const struct option long_options[] = {
{"help", no_argument, 0, 'h'},
@@ -4340,7 +4330,7 @@ static int img_resize(const img_cmd_t *ccmd, int argc,
char **argv)
{"shrink", no_argument, 0, OPTION_SHRINK},
{0, 0, 0, 0}
};
- c = getopt_long(argc, argv, ":f:hq",
+ c = getopt_long(argc, argv, "-:f:hq",
long_options, NULL);
if (c == -1) {
break;
@@ -4378,12 +4368,35 @@ static int img_resize(const img_cmd_t *ccmd, int argc,
char **argv)
case OPTION_SHRINK:
shrink = true;
break;
+ case 1: /* a non-optional argument */
+ if (!filename) {
+ filename = optarg;
+ /* see if we have -size (number) next to filename */
+ if (optind < argc) {
+ size = argv[optind];
+ if (size[0] == '-' && size[1] >= '0' && size[1] <= '9') {
+ ++optind;
+ } else {
+ size = NULL;
+ }
+ }
+ } else if (!size) {
+ size = optarg;
+ } else {
+ error_exit(argv[0], "Extra argument(s) in command line");
+ }
+ break;
}
}
- if (optind != argc - 1) {
+ if (!filename && optind < argc) {
+ filename = argv[optind++];
+ }
+ if (!size && optind < argc) {
+ size = argv[optind++];
+ }
+ if (!filename || !size || optind < argc) {
error_exit(argv[0], "Expecting image file name and size");
}
- filename = argv[optind++];
/* Choose grow, shrink, or absolute resize mode */
switch (size[0]) {
--
2.39.2
- [PATCH 11/27] qemu-img: convert: refresh options/--help, (continued)
- [PATCH 11/27] qemu-img: convert: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 07/27] qemu-img: check: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 08/27] qemu-img: simplify --repair error message, Michael Tokarev, 2024/04/24
- [PATCH 09/27] qemu-img: commit: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 13/27] qemu-img: map: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 14/27] qemu-img: snapshot: allow specifying -f fmt, Michael Tokarev, 2024/04/24
- [PATCH 12/27] qemu-img: info: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 15/27] qemu-img: snapshot: make -l (list) the default, simplify option handling, Michael Tokarev, 2024/04/24
- [PATCH 16/27] qemu-img: snapshot: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 17/27] qemu-img: rebase: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 18/27] qemu-img: resize: do not always eat last argument,
Michael Tokarev <=
- [PATCH 20/27] qemu-img: amend: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 19/27] qemu-img: resize: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 22/27] qemu-img: bitmap: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 21/27] qemu-img: bench: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 23/27] qemu-img: dd: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 24/27] qemu-img: measure: refresh options/--help, Michael Tokarev, 2024/04/24
- [PATCH 25/27] qemu-img: implement short --help, remove global help() function, Michael Tokarev, 2024/04/24
- [PATCH 27/27] qemu-img: extend cvtnum() and use it in more places, Michael Tokarev, 2024/04/24
- [PATCH 26/27] qemu-img: inline list of supported commands, remove qemu-img-cmds.h include, Michael Tokarev, 2024/04/24