qemu-option: add help fallback to print the list of options
PATCH 1/2 qemu-option: add help fallback to print the list of options
QDev options accept '?' or 'help' in the list of parameters, which is really handy to list the available options.
Unfortunately, this isn't built in QemuOpts. qemu_opts_parse_noisily() seems to be the common path for command line options, so place a fallback to check for '?' and print help listing available options.
This is very handy, for example with qemu "-spice ?".
--- util/qemu-option.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
util/qemu-option.c +6 -1
@@ -889,7 +889,12 @@ QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, opts =opts_parse (list, params, permit_abbrev, false, &err);if (err) { -error_report_err (err); +if (has_help_option (params)) { +qemu_opts_print_help (list); +error_free (err); + }else { +error_report_err (err); + } }return opts; }
Signature · 1 line
2.18.0.547.g1d89318c48
PATCH 2/2 qemu-option: improve qemu_opts_print_help() output
Modify qemu_opts_print_help():
- to print expected argument type
- skip description if not available
- sort lines
- prefix with the list name (like qdev, to avoid confusion)
- drop 16-chars alignment, use a '-' as seperator for option name and description
For ex, "-spice ?" output is changed from:
port No description available tls-port No description available addr No description available
[...]
gl No description available
rendernode No description available
to:
spice.addr=str
spice.agent-mouse=bool (on/off)
spice.disable-agent-file-xfer=bool (on/off)
[...]
spice.x509-key-password=str
spice.zlib-glz-wan-compression=str
"qemu-img create -f qcow2 -o ?", changed from:
size Virtual disk size compat Compatibility level (0.10 or 1.1) backing_file File name of a base image
[...]lazy_refcounts Postpone refcount updatesrefcount_bits Width of a reference count entry in bits
to:
backing_file=str - File name of a base image backing_fmt=str - Image format of the base image cluster_size=size - qcow2 cluster size
[...]
refcount_bits=num - Width of a reference count entry in bits size=size - Virtual disk size
--- util/qemu-option.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-)
util/qemu-option.c +42 -2
@@ -208,17 +208,57 @@ out:return result; } +static const char *opt_type_to_string (enum QemuOptType type) +{ +switch (type) { +case QEMU_OPT_STRING : +return "str" ; +case QEMU_OPT_BOOL : +return "bool (on/off)" ; +case QEMU_OPT_NUMBER : +return "num" ; +case QEMU_OPT_SIZE : +return "size" ; + } + +g_assert_not_reached (); +} + +static gint +pstrcmp (const char **a,const char **b) +{ +return g_strcmp0 (*a, *b); +} +void qemu_opts_print_help (QemuOptsList *list) {QemuOptDesc *desc; +int i; +GPtrArray *array =g_ptr_array_new ();assert (list); desc = list->desc;while (desc && desc->name) { -printf ("%-16s %s\n" , desc->name, - desc->help ? desc->help :"No description available" ); +GString *str =g_string_new (NULL ); +if (list->name) { +g_string_append_printf (str,"%s." , list->name); + } +g_string_append_printf (str,"%s=%s" , desc->name, +opt_type_to_string (desc->type)); +if (desc->help) { +g_string_append_printf (str," - %s" , desc->help); + } +g_ptr_array_add (array,g_string_free (str, false)); desc++; } + +g_ptr_array_sort (array, (GCompareFunc )pstrcmp); +for (i =0 ; i < array->len; i++) { +printf ("%s\n" , (char *)array->pdata[i]); + } +g_ptr_array_set_free_func (array, g_free); +g_ptr_array_free (array, true); + }/* ------------------------------------------------------------------ */
Signature · 1 line
2.18.0.547.g1d89318c48
Quoted 6 lines · Marc-André Lureau
QDev options accept '?' or 'help' in the list of parameters, which is really handy to list the available options.
Unfortunately, this isn't built in QemuOpts.
qemu_opts_parse_noisily()seems to be the common path for command line options, so place a fallback to check for '?' and print help listing available options.
My immediate reaction was "how come this doesn't break -device help"? It doesn't, because...
Quoted 15 lines
This is very handy, for example with qemu "-spice ?".
Signed-off-by Marc-André Lureau <[email protected]>--- util/qemu-option.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)util/qemu-option.c +0
-0@@ -889,7 +889,12 @@ QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, opts =opts_parse (list, params, permit_abbrev, false, &err);if (err) {
util/qemu-option.c QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, · line 900
Quoted 10 lines
util/qemu-option.c +6
-1-error_report_err (err); +if (has_help_option (params)) { +qemu_opts_print_help (list); +error_free (err); + }else { +error_report_err (err); + } }return opts; }
... it only kicks in when parsing failed, and it doesn't for option argument "help".
However, it *can* break for some option arguments:
$ upstream-qemu -device e1000,id=@
upstream-qemu: -device e1000,id=@: Parameter 'id' expects an identifier
Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.
[Exit 1 ]
$ upstream-qemu -device e1000,id=@,help
[Exit 1 ]In the second test case, the error message gets replaced by (empty) help.
I love patches improving help, but I can't immediately see how to salvage this one.
Hi
Quoted 51 lines · Markus Armbruster
Quoted 6 lines · Marc-André Lureau
QDev options accept '?' or 'help' in the list of parameters, which is really handy to list the available options.
Unfortunately, this isn't built in QemuOpts.
qemu_opts_parse_noisily()seems to be the common path for command line options, so place a fallback to check for '?' and print help listing available options.My immediate reaction was "how come this doesn't break -device help"? It doesn't, because...
Quoted 15 lines
This is very handy, for example with qemu "-spice ?".
Signed-off-by Marc-André Lureau <[email protected]>--- util/qemu-option.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)util/qemu-option.c +0
-0@@ -889,7 +889,12 @@ QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, opts =opts_parse (list, params, permit_abbrev, false, &err);if (err) {util/qemu-option.c QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, · line 900
Quoted 10 lines
util/qemu-option.c +6
-1-error_report_err (err); +if (has_help_option (params)) { +qemu_opts_print_help (list); +error_free (err); + }else { +error_report_err (err); + } }return opts; }... it only kicks in when parsing failed, and it doesn't for option argument "help".
However, it *can* break for some option arguments:
$ upstream-qemu -device e1000,id=@ upstream-qemu: -device e1000,id=@: Parameter 'id' expects an identifier Identifiers consist of letters, digits, '-', '.', '_', starting with a letter. [Exit 1 ] $ upstream-qemu -device e1000,id=@,help [Exit 1 ]
Quoted 2 lines
In the second test case, the error message gets replaced by (empty) help.
Good catch
Quoted 4 lines
I love patches improving help, but I can't immediately see how to salvage this one.
We can pass a with_help option down to opt_set(), and print help there? Alternatively, pass a bool *invalid_parameter, to be set in this case, so qemu_opts_parse_noisly() would only print help when it is set.
Do you see a problem with any of those 2 options?
Signature · 1 line
Marc-André Lureau
Quoted 65 lines · Marc-André Lureau
Hi
Quoted 51 lines · Markus Armbruster
Quoted 6 lines · Marc-André Lureau
QDev options accept '?' or 'help' in the list of parameters, which is really handy to list the available options.
Unfortunately, this isn't built in QemuOpts.
qemu_opts_parse_noisily()seems to be the common path for command line options, so place a fallback to check for '?' and print help listing available options.My immediate reaction was "how come this doesn't break -device help"? It doesn't, because...
Quoted 15 lines
This is very handy, for example with qemu "-spice ?".
Signed-off-by Marc-André Lureau <[email protected]>--- util/qemu-option.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)util/qemu-option.c +0
-0@@ -889,7 +889,12 @@ QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, opts =opts_parse (list, params, permit_abbrev, false, &err);if (err) {util/qemu-option.c QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, · line 900
Quoted 10 lines
util/qemu-option.c +6
-1-error_report_err (err); +if (has_help_option (params)) { +qemu_opts_print_help (list); +error_free (err); + }else { +error_report_err (err); + } }return opts; }... it only kicks in when parsing failed, and it doesn't for option argument "help".
However, it *can* break for some option arguments:
$ upstream-qemu -device e1000,id=@ upstream-qemu: -device e1000,id=@: Parameter 'id' expects an identifier Identifiers consist of letters, digits, '-', '.', '_', starting with a letter. [Exit 1 ] $ upstream-qemu -device e1000,id=@,help [Exit 1 ]Quoted 2 lines
In the second test case, the error message gets replaced by (empty) help.
Good catch
Quoted 4 lines
I love patches improving help, but I can't immediately see how to salvage this one.
Quoted 6 lines
We can pass a
with_helpoption down toopt_set(), and print help there? Alternatively, pass a bool*invalid_parameter, to be set in this case, soqemu_opts_parse_noisly()would only print help when it is set.Do you see a problem with any of those 2 options?
This is the second option, that I can amend and send v2 if you are ok with it:
util/qemu-option.c +1 -1
@@ -526,7 +526,7 @@ int qemu_opt_unset(QemuOpts *opts, const char *name) }static void opt_set (QemuOpts *opts,const char *name,char *value, -bool prepend,Error **errp) +bool prepend,bool *invalidp,Error **errp) {QemuOpt *opt;const QemuOptDesc *desc;
@@ -536,6 +536,9 @@ static void opt_set(QemuOpts *opts, const char
*name, char *value,
if (!desc && !opts_accepts_any(opts)) {
g_free(value);
error_setg(errp, QERR_INVALID_PARAMETER, name);
+ if (invalidp) {
+ *invalidp = true;
+ }
return;
}@@ -559,7 +562,7 @@ static void opt_set(QemuOpts *opts, const char *name, char *value,
void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
Error **errp)
{
- opt_set(opts, name, g_strdup(value), false, errp);
+ opt_set(opts, name, g_strdup(value), false, NULL, errp);
}
void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
@@ -790,7 +793,8 @@ void qemu_opts_print(QemuOpts *opts, const char *separator)
}
static void opts_do_parse(QemuOpts *opts, const char *params,
- const char *firstname, bool prepend, Error **errp)
+ const char *firstname, bool prepend,
+ bool *invalidp, Error **errp)
{
char *option = NULL;
char *value = NULL;
@@ -825,7 +829,7 @@ static void opts_do_parse(QemuOpts *opts, const
char *params,
}
if (strcmp(option, "id") != 0) {
/* store and parse */
- opt_set(opts, option, value, prepend, &local_err);
+ opt_set(opts, option, value, prepend, invalidp, &local_err);
value = NULL;
if (local_err) {
error_propagate(errp, local_err);@@ -854,11 +858,12 @@ static void opts_do_parse(QemuOpts *opts, const char *params,
void qemu_opts_do_parse(QemuOpts *opts, const char *params,
const char *firstname, Error **errp)
{
- opts_do_parse(opts, params, firstname, false, errp);
+ opts_do_parse(opts, params, firstname, false, NULL, errp);
}
static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
- bool permit_abbrev, bool defaults, Error **errp)
+ bool permit_abbrev, bool defaults,
+ bool *invalidp, Error **errp)
{
const char *firstname;
char *id = NULL;
@@ -890,7 +895,7 @@ static QemuOpts *opts_parse(QemuOptsList *list,
const char *params,
return NULL;
}- opts_do_parse(opts, params, firstname, defaults, &local_err);
+ opts_do_parse(opts, params, firstname, defaults, invalidp, &local_err);
if (local_err) {
error_propagate(errp, local_err);
qemu_opts_del(opts);@@ -910,7 +915,7 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
bool permit_abbrev, Error **errp)
{
- return opts_parse(list, params, permit_abbrev, false, errp);
+ return opts_parse(list, params, permit_abbrev, false, NULL, errp);
}
/**
@@ -926,10 +931,11 @@ QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
{
Error *err = NULL;
QemuOpts *opts;+ bool invalidp = false;
- opts = opts_parse(list, params, permit_abbrev, false, &err);
+ opts = opts_parse(list, params, permit_abbrev, false, &invalidp, &err);
if (err) {
- if (has_help_option(params)) {
+ if (invalidp && has_help_option(params)) {
qemu_opts_print_help(list);
error_free(err);
} else {
@@ -944,7 +950,7 @@ void qemu_opts_set_defaults(QemuOptsList *list,
const char *params,
{
QemuOpts *opts;- opts = opts_parse(list, params, permit_abbrev, true, NULL);
+ opts = opts_parse(list, params, permit_abbrev, true, NULL, NULL);
assert(opts);}
Signature · 1 line
Marc-André Lureau