From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, stefanha@redhat.com, qemu-devel@nongnu.org
Subject: [PULL 56/57] qemu-img: implement short --help, remove global help() function
Date: Tue, 15 Jul 2025 21:03:29 +0200 [thread overview]
Message-ID: <20250715190330.378764-57-kwolf@redhat.com> (raw)
In-Reply-To: <20250715190330.378764-1-kwolf@redhat.com>
From: Michael Tokarev <mjt@tls.msk.ru>
now once all individual subcommands has --help support, remove
the large unreadable help() thing and replace it with small
global --help, which refers to individual command --help for
more info.
While at it, also line-wrap list of formats after 75 chars.
Since missing_argument() and unrecognized_option() are now unused,
remove them.
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Message-ID: <20250531171609.197078-27-mjt@tls.msk.ru>
[kwolf: Fixed up style and formatting]
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
qemu-img.c | 214 ++++++++++++++++++-----------------------------------
1 file changed, 73 insertions(+), 141 deletions(-)
diff --git a/qemu-img.c b/qemu-img.c
index a7d9d50250..b34b1390bb 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -61,6 +61,7 @@
typedef struct img_cmd_t {
const char *name;
int (*handler)(const struct img_cmd_t *ccmd, int argc, char **argv);
+ const char *description;
} img_cmd_t;
enum {
@@ -95,11 +96,6 @@ typedef enum OutputFormat {
/* Default to cache=writeback as data integrity is not important for qemu-img */
#define BDRV_DEFAULT_CACHE "writeback"
-static void format_print(void *opaque, const char *name)
-{
- printf(" %s", name);
-}
-
static G_NORETURN
void tryhelp(const char *argv0)
{
@@ -119,18 +115,6 @@ void error_exit(const char *argv0, const char *fmt, ...)
tryhelp(argv0);
}
-static G_NORETURN
-void missing_argument(const char *option)
-{
- error_exit("qemu-img", "missing argument for option '%s'", option);
-}
-
-static G_NORETURN
-void unrecognized_option(const char *option)
-{
- error_exit("qemu-img", "unrecognized option '%s'", option);
-}
-
/*
* Print --help output for a command and exit.
* @syntax and @description are multi-line with trailing EOL
@@ -145,15 +129,14 @@ void cmd_help(const img_cmd_t *ccmd,
{
printf(
"Usage:\n"
-"\n"
-" %s %s %s"
+" %s %s %s\n"
+"%s.\n"
"\n"
"Arguments:\n"
" -h, --help\n"
" print this help and exit\n"
"%s\n",
- "qemu-img", ccmd->name,
- syntax, arguments);
+ "qemu-img", ccmd->name, syntax, ccmd->description, arguments);
exit(EXIT_SUCCESS);
}
@@ -168,114 +151,6 @@ static OutputFormat parse_output_format(const char *argv0, const char *arg)
}
}
-/* Please keep in synch with docs/tools/qemu-img.rst */
-static G_NORETURN
-void help(void)
-{
- const char *help_msg =
- QEMU_IMG_VERSION
- "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"
- " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
- " specify tracing options\n"
- "\n"
- "Command syntax:\n"
-#define DEF(option, callback, arg_string) \
- " " arg_string "\n"
-#include "qemu-img-cmds.h"
-#undef DEF
- "\n"
- "Command parameters:\n"
- " 'filename' is a disk image filename\n"
- " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
- " manual page for a description of the object properties. The most common\n"
- " object type is a 'secret', which is used to supply passwords and/or\n"
- " encryption keys.\n"
- " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
- " 'cache' is the cache mode used to write the output disk image, the valid\n"
- " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
- " 'directsync' and 'unsafe' (default for convert)\n"
- " 'src_cache' is the cache mode used to read input disk images, the valid\n"
- " options are the same as for the 'cache' option\n"
- " 'size' is the disk image size in bytes. Optional suffixes\n"
- " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
- " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
- " supported. 'b' is ignored.\n"
- " 'output_filename' is the destination disk image filename\n"
- " 'output_fmt' is the destination format\n"
- " 'options' is a comma separated list of format specific options in a\n"
- " name=value format. Use -o help for an overview of the options supported by\n"
- " the used format\n"
- " 'snapshot_param' is param used for internal snapshot, format\n"
- " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
- " '[ID_OR_NAME]'\n"
- " '-c' indicates that target image must be compressed (qcow format only)\n"
- " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
- " new backing file match exactly. The image doesn't need a working\n"
- " backing file before rebasing in this case (useful for renaming the\n"
- " backing file). For image creation, allow creating without attempting\n"
- " to open the backing file.\n"
- " '-h' with or without a command shows this help and lists the supported formats\n"
- " '-p' show progress of command (only certain commands)\n"
- " '-q' use Quiet mode - do not print any output (except errors)\n"
- " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
- " contain only zeros for qemu-img to create a sparse image during\n"
- " conversion. If the number of bytes is 0, the source will not be scanned for\n"
- " unallocated or zero sectors, and the destination image will always be\n"
- " fully allocated\n"
- " '--output' takes the format in which the output must be done (human or json)\n"
- " '-n' skips the target volume creation (useful if the volume is created\n"
- " prior to running qemu-img)\n"
- "\n"
- "Parameters to bitmap subcommand:\n"
- " 'bitmap' is the name of the bitmap to manipulate, through one or more\n"
- " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n"
- " or '--merge source'\n"
- " '-g granularity' sets the granularity for '--add' actions\n"
- " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n"
- " bitmaps from an alternative file\n"
- "\n"
- "Parameters to check subcommand:\n"
- " '-r' tries to repair any inconsistencies that are found during the check.\n"
- " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
- " kinds of errors, with a higher risk of choosing the wrong fix or\n"
- " hiding corruption that has already occurred.\n"
- "\n"
- "Parameters to convert subcommand:\n"
- " '--bitmaps' copies all top-level persistent bitmaps to destination\n"
- " '-m' specifies how many coroutines work in parallel during the convert\n"
- " process (defaults to 8)\n"
- " '-W' allow to write to the target out of order rather than sequential\n"
- "\n"
- "Parameters to snapshot subcommand:\n"
- " 'snapshot' is the name of the snapshot to create, apply or delete\n"
- " '-a' applies a snapshot (revert disk to saved state)\n"
- " '-c' creates a snapshot\n"
- " '-d' deletes a snapshot\n"
- " '-l' lists all snapshots in the given image\n"
- "\n"
- "Parameters to compare subcommand:\n"
- " '-f' first image format\n"
- " '-F' second image format\n"
- " '-s' run in Strict mode - fail on different image size or sector allocation\n"
- "\n"
- "Parameters to dd subcommand:\n"
- " 'bs=BYTES' read and write up to BYTES bytes at a time "
- "(default: 512)\n"
- " 'count=N' copy only N input blocks\n"
- " 'if=FILE' read from FILE\n"
- " 'of=FILE' write to FILE\n"
- " 'skip=N' skip N bs-sized blocks at the start of input\n";
-
- printf("%s\nSupported formats:", help_msg);
- bdrv_iterate_format(format_print, NULL, false);
- printf("\n\n" QEMU_HELP_BOTTOM "\n");
- exit(EXIT_SUCCESS);
-}
-
/*
* Is @list safe for accumulate_options()?
* It is when multiple of them can be joined together separated by ','.
@@ -5999,13 +5874,49 @@ out:
}
static const img_cmd_t img_cmds[] = {
-#define DEF(option, callback, arg_string) \
- { option, callback },
-#include "qemu-img-cmds.h"
-#undef DEF
+ { "amend", img_amend,
+ "Update format-specific options of the image" },
+ { "bench", img_bench,
+ "Run a simple image benchmark" },
+ { "bitmap", img_bitmap,
+ "Perform modifications of the persistent bitmap in the image" },
+ { "check", img_check,
+ "Check basic image integrity" },
+ { "commit", img_commit,
+ "Commit image to its backing file" },
+ { "compare", img_compare,
+ "Check if two images have the same contents" },
+ { "convert", img_convert,
+ "Copy one or more images to another with optional format conversion" },
+ { "create", img_create,
+ "Create and format a new image file" },
+ { "dd", img_dd,
+ "Copy input to output with optional format conversion" },
+ { "info", img_info,
+ "Display information about the image" },
+ { "map", img_map,
+ "Dump image metadata" },
+ { "measure", img_measure,
+ "Calculate the file size required for a new image" },
+ { "rebase", img_rebase,
+ "Change the backing file of the image" },
+ { "resize", img_resize,
+ "Resize the image" },
+ { "snapshot", img_snapshot,
+ "List or manipulate snapshots in the image" },
{ NULL, NULL, },
};
+static void format_print(void *opaque, const char *name)
+{
+ int *np = opaque;
+ if (*np + strlen(name) > 75) {
+ printf("\n ");
+ *np = 1;
+ }
+ *np += printf(" %s", name);
+}
+
int main(int argc, char **argv)
{
const img_cmd_t *cmd;
@@ -6037,16 +5948,35 @@ int main(int argc, char **argv)
qemu_add_opts(&qemu_source_opts);
qemu_add_opts(&qemu_trace_opts);
- while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
+ while ((c = getopt_long(argc, argv, "+hVT:", long_options, NULL)) != -1) {
switch (c) {
- case ':':
- missing_argument(argv[optind - 1]);
- return 0;
- case '?':
- unrecognized_option(argv[optind - 1]);
- return 0;
case 'h':
- help();
+ printf(
+QEMU_IMG_VERSION
+"QEMU disk image utility. Usage:\n"
+"\n"
+" qemu-img [standard options] COMMAND [--help | command options]\n"
+"\n"
+"Standard options:\n"
+" -h, --help\n"
+" display this help and exit\n"
+" -V, --version\n"
+" display version info and exit\n"
+" -T,--trace TRACE\n"
+" specify tracing options:\n"
+" [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
+"\n"
+"Recognized commands (run qemu-img COMMAND --help for command-specific help):\n\n");
+ for (cmd = img_cmds; cmd->name != NULL; cmd++) {
+ printf(" %s - %s\n", cmd->name, cmd->description);
+ }
+ printf("\nSupported image formats:\n");
+ c = 99; /* force a newline */
+ bdrv_iterate_format(format_print, &c, false);
+ if (c) {
+ printf("\n");
+ }
+ printf("\n" QEMU_HELP_BOTTOM "\n");
return 0;
case 'V':
printf(QEMU_IMG_VERSION);
@@ -6054,6 +5984,8 @@ int main(int argc, char **argv)
case 'T':
trace_opt_parse(optarg);
break;
+ default:
+ tryhelp(argv[0]);
}
}
--
2.50.1
next prev parent reply other threads:[~2025-07-15 19:43 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-15 19:02 [PULL 00/57] Block layer patches Kevin Wolf
2025-07-15 19:02 ` [PULL 01/57] block: never use atomics to access bs->quiesce_counter Kevin Wolf
2025-07-15 19:02 ` [PULL 02/57] block: add bdrv_graph_wrlock_drained() convenience wrapper Kevin Wolf
2025-07-15 19:02 ` [PULL 03/57] block/mirror: switch to bdrv_set_backing_hd_drained() variant Kevin Wolf
2025-07-15 19:02 ` [PULL 04/57] block/commit: " Kevin Wolf
2025-07-15 19:02 ` [PULL 05/57] block: call bdrv_set_backing_hd() while unlocked in bdrv_open_backing_file() Kevin Wolf
2025-07-15 19:02 ` [PULL 06/57] block: mark bdrv_set_backing_hd() as GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:02 ` [PULL 07/57] blockdev: avoid locking and draining multiple times in external_snapshot_abort() Kevin Wolf
2025-07-15 19:02 ` [PULL 08/57] block: drop wrapper for bdrv_set_backing_hd_drained() Kevin Wolf
2025-07-15 19:02 ` [PULL 09/57] block-backend: mark blk_drain_all() as GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:02 ` [PULL 10/57] block/snapshot: mark bdrv_all_delete_snapshot() " Kevin Wolf
2025-07-15 19:02 ` [PULL 11/57] block/stream: mark stream_prepare() " Kevin Wolf
2025-07-15 19:02 ` [PULL 12/57] block: mark bdrv_reopen_queue() and bdrv_reopen_multiple() " Kevin Wolf
2025-07-15 19:02 ` [PULL 13/57] block: mark bdrv_inactivate() as GRAPH_RDLOCK and move drain to callers Kevin Wolf
2025-07-15 19:02 ` [PULL 14/57] block: mark bdrv_inactivate_all() as GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:02 ` [PULL 15/57] block: mark blk_remove_bs() " Kevin Wolf
2025-07-15 19:02 ` [PULL 16/57] block: mark blk_drain() " Kevin Wolf
2025-07-15 19:02 ` [PULL 17/57] block-backend: mark blk_io_limits_disable() " Kevin Wolf
2025-07-15 19:02 ` [PULL 18/57] block/commit: mark commit_abort() " Kevin Wolf
2025-07-15 19:02 ` [PULL 19/57] block: Allow bdrv_new() with and without graph lock Kevin Wolf
2025-07-15 19:02 ` [PULL 20/57] block: mark bdrv_replace_child_bs() as GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:02 ` [PULL 21/57] block: mark bdrv_insert_node() " Kevin Wolf
2025-07-15 19:02 ` [PULL 22/57] block: mark bdrv_drop_intermediate() " Kevin Wolf
2025-07-15 19:02 ` [PULL 23/57] block: mark bdrv_close_all() " Kevin Wolf
2025-07-15 19:02 ` [PULL 24/57] block: mark bdrv_close() " Kevin Wolf
2025-07-15 19:02 ` [PULL 25/57] block: mark bdrv_open_child_common() and its callers GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:02 ` [PULL 26/57] blockjob: mark block_job_remove_all_bdrv() as GRAPH_UNLOCKED Kevin Wolf
2025-07-15 19:03 ` [PULL 27/57] block/qapi: include child references in block device info Kevin Wolf
2025-07-15 19:03 ` [PULL 28/57] block/qapi: make @node-name in @BlockDeviceInfo non-optional Kevin Wolf
2025-07-15 19:03 ` [PULL 29/57] file-posix: Fix aio=threads performance regression after enablign FUA Kevin Wolf
2025-07-15 19:03 ` [PULL 30/57] iotests: add test for changing the 'drive' property via 'qom-set' Kevin Wolf
2025-07-15 19:03 ` [PULL 31/57] qemu-img: measure: convert img_size to signed, simplify handling Kevin Wolf
2025-07-15 19:03 ` [PULL 32/57] qemu-img: create: " Kevin Wolf
2025-07-15 19:03 ` [PULL 33/57] qemu-img: global option processing and error printing Kevin Wolf
2025-07-15 19:03 ` [PULL 34/57] qemu-img: pass current cmd info into command handlers Kevin Wolf
2025-07-15 19:03 ` [PULL 35/57] qemu-img: create: refresh options/--help (short option change) Kevin Wolf
2025-07-15 19:03 ` [PULL 36/57] qemu-img: factor out parse_output_format() and use it in the code Kevin Wolf
2025-07-15 19:03 ` [PULL 37/57] qemu-img: check: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 38/57] qemu-img: simplify --repair error message Kevin Wolf
2025-07-15 19:03 ` [PULL 39/57] qemu-img: commit: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 40/57] qemu-img: compare: use helper function for --object Kevin Wolf
2025-07-15 19:03 ` [PULL 41/57] qemu-img: compare: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 42/57] qemu-img: convert: refresh options/--help (short option change) Kevin Wolf
2025-07-15 19:03 ` [PULL 43/57] qemu-img: info: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 44/57] qemu-img: map: " Kevin Wolf
2025-07-15 19:03 ` [PULL 45/57] qemu-img: snapshot: allow specifying -f fmt Kevin Wolf
2025-07-15 19:03 ` [PULL 46/57] qemu-img: snapshot: make -l (list) the default, simplify option handling Kevin Wolf
2025-07-15 19:03 ` [PULL 47/57] qemu-img: snapshot: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 48/57] qemu-img: rebase: refresh options/--help (short option change) Kevin Wolf
2025-07-15 19:03 ` [PULL 49/57] qemu-img: resize: do not always eat last argument Kevin Wolf
2025-07-15 19:03 ` [PULL 50/57] qemu-img: resize: refresh options/--help Kevin Wolf
2025-07-15 19:03 ` [PULL 51/57] qemu-img: amend: " Kevin Wolf
2025-07-15 19:03 ` [PULL 52/57] qemu-img: bench: " Kevin Wolf
2025-07-15 19:03 ` [PULL 53/57] qemu-img: bitmap: " Kevin Wolf
2025-07-15 19:03 ` [PULL 54/57] qemu-img: dd: " Kevin Wolf
2025-07-15 19:03 ` [PULL 55/57] qemu-img: measure: " Kevin Wolf
2025-07-15 19:03 ` Kevin Wolf [this message]
2025-07-15 19:03 ` [PULL 57/57] qemu-img: extend cvtnum() and use it in more places Kevin Wolf
2025-07-16 12:41 ` [PULL 00/57] Block layer patches Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250715190330.378764-57-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.