[1/4] vl: Split off user_creatable_print_help()
diff mbox series

Message ID 20191011205551.32149-2-kwolf@redhat.com
State New
Headers show
Series
  • qemu-img/io/nbd: Support help options for --object
Related show

Commit Message

Kevin Wolf Oct. 11, 2019, 8:55 p.m. UTC
Printing help for --object is something that we don't only want in the
system emulator, but also in tools that support --object. Move it into a
separate function in qom/object_interfaces.c to make the code accessible
for tools.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 include/qom/object_interfaces.h | 12 +++++++
 qom/object_interfaces.c         | 61 +++++++++++++++++++++++++++++++++
 vl.c                            | 52 +---------------------------
 3 files changed, 74 insertions(+), 51 deletions(-)

Comments

Eric Blake Oct. 11, 2019, 9:35 p.m. UTC | #1
On 10/11/19 3:55 PM, Kevin Wolf wrote:
> Printing help for --object is something that we don't only want in the

s/don't/not/

> system emulator, but also in tools that support --object. Move it into a
> separate function in qom/object_interfaces.c to make the code accessible
> for tools.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>   include/qom/object_interfaces.h | 12 +++++++
>   qom/object_interfaces.c         | 61 +++++++++++++++++++++++++++++++++
>   vl.c                            | 52 +---------------------------
>   3 files changed, 74 insertions(+), 51 deletions(-)
> 
> diff --git a/include/qom/object_interfaces.h b/include/qom/object_interfaces.h
> index 682ba1d9b0..3e4e1d928b 100644
> --- a/include/qom/object_interfaces.h

Reviewed-by: Eric Blake <eblake@redhat.com>
Kevin Wolf Oct. 14, 2019, 9:55 a.m. UTC | #2
Am 11.10.2019 um 23:35 hat Eric Blake geschrieben:
> On 10/11/19 3:55 PM, Kevin Wolf wrote:
> > Printing help for --object is something that we don't only want in the
> 
> s/don't/not/

Can someone send a fix for the English grammar? It's obviously broken
and doesn't know what it wants. Actually, maybe do-support was a bad
idea and we should just revert it and restore consistent use of proper
verb-second word order?

(Hm, actually, since this seems to negate "only" rather than the verb,
does "...that we want not only in..." work without patching the
grammar?)

(Thanks for the correction.)

Kevin

> > system emulator, but also in tools that support --object. Move it into a
> > separate function in qom/object_interfaces.c to make the code accessible
> > for tools.
> > 
> > Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Eric Blake Oct. 14, 2019, 2:18 p.m. UTC | #3
On 10/14/19 4:55 AM, Kevin Wolf wrote:
> Am 11.10.2019 um 23:35 hat Eric Blake geschrieben:
>> On 10/11/19 3:55 PM, Kevin Wolf wrote:
>>> Printing help for --object is something that we don't only want in the
>>
>> s/don't/not/
> 
> Can someone send a fix for the English grammar? It's obviously broken
> and doesn't know what it wants. Actually, maybe do-support was a bad
> idea and we should just revert it and restore consistent use of proper
> verb-second word order?

Lol

> 
> (Hm, actually, since this seems to negate "only" rather than the verb,
> does "...that we want not only in..." work without patching the
> grammar?)

Yes, that formulation also works.

> 
> (Thanks for the correction.)
> 
> Kevin
> 
>>> system emulator, but also in tools that support --object. Move it into a
>>> separate function in qom/object_interfaces.c to make the code accessible
>>> for tools.
>>>
>>> Signed-off-by: Kevin Wolf <kwolf@redhat.com>

Patch
diff mbox series

diff --git a/include/qom/object_interfaces.h b/include/qom/object_interfaces.h
index 682ba1d9b0..3e4e1d928b 100644
--- a/include/qom/object_interfaces.h
+++ b/include/qom/object_interfaces.h
@@ -132,6 +132,18 @@  typedef bool (*user_creatable_add_opts_predicate)(const char *type);
 int user_creatable_add_opts_foreach(void *opaque,
                                     QemuOpts *opts, Error **errp);
 
+/**
+ * user_creatable_print_help:
+ * @type: the QOM type to be added
+ * @opts: options to create
+ *
+ * Prints help if requested in @opts.
+ *
+ * Returns: true if @opts contained a help option and help was printed, false
+ * if no help option was found.
+ */
+bool user_creatable_print_help(const char *type, QemuOpts *opts);
+
 /**
  * user_creatable_del:
  * @id: the unique ID for the object
diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
index cb5809934a..46cd6eab5c 100644
--- a/qom/object_interfaces.c
+++ b/qom/object_interfaces.c
@@ -1,8 +1,11 @@ 
 #include "qemu/osdep.h"
+
+#include "qemu/cutils.h"
 #include "qapi/error.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qerror.h"
 #include "qom/object_interfaces.h"
+#include "qemu/help_option.h"
 #include "qemu/module.h"
 #include "qemu/option.h"
 #include "qapi/opts-visitor.h"
@@ -155,6 +158,64 @@  int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
     return 0;
 }
 
+bool user_creatable_print_help(const char *type, QemuOpts *opts)
+{
+    ObjectClass *klass;
+
+    if (is_help_option(type)) {
+        GSList *l, *list;
+
+        printf("List of user creatable objects:\n");
+        list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
+        for (l = list; l != NULL; l = l->next) {
+            ObjectClass *oc = OBJECT_CLASS(l->data);
+            printf("  %s\n", object_class_get_name(oc));
+        }
+        g_slist_free(list);
+        return true;
+    }
+
+    klass = object_class_by_name(type);
+    if (klass && qemu_opt_has_help_opt(opts)) {
+        ObjectPropertyIterator iter;
+        ObjectProperty *prop;
+        GPtrArray *array = g_ptr_array_new();
+        int i;
+
+        object_class_property_iter_init(&iter, klass);
+        while ((prop = object_property_iter_next(&iter))) {
+            GString *str;
+
+            if (!prop->set) {
+                continue;
+            }
+
+            str = g_string_new(NULL);
+            g_string_append_printf(str, "  %s=<%s>", prop->name, prop->type);
+            if (prop->description) {
+                if (str->len < 24) {
+                    g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
+                }
+                g_string_append_printf(str, " - %s", prop->description);
+            }
+            g_ptr_array_add(array, g_string_free(str, false));
+        }
+        g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
+        if (array->len > 0) {
+            printf("%s options:\n", type);
+        } else {
+            printf("There are no options for %s.\n", type);
+        }
+        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);
+        return true;
+    }
+
+    return false;
+}
 
 void user_creatable_del(const char *id, Error **errp)
 {
diff --git a/vl.c b/vl.c
index 002bf4919e..b86d4e502d 100644
--- a/vl.c
+++ b/vl.c
@@ -2649,57 +2649,7 @@  static int machine_set_property(void *opaque,
  */
 static bool object_create_initial(const char *type, QemuOpts *opts)
 {
-    ObjectClass *klass;
-
-    if (is_help_option(type)) {
-        GSList *l, *list;
-
-        printf("List of user creatable objects:\n");
-        list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
-        for (l = list; l != NULL; l = l->next) {
-            ObjectClass *oc = OBJECT_CLASS(l->data);
-            printf("  %s\n", object_class_get_name(oc));
-        }
-        g_slist_free(list);
-        exit(0);
-    }
-
-    klass = object_class_by_name(type);
-    if (klass && qemu_opt_has_help_opt(opts)) {
-        ObjectPropertyIterator iter;
-        ObjectProperty *prop;
-        GPtrArray *array = g_ptr_array_new();
-        int i;
-
-        object_class_property_iter_init(&iter, klass);
-        while ((prop = object_property_iter_next(&iter))) {
-            GString *str;
-
-            if (!prop->set) {
-                continue;
-            }
-
-            str = g_string_new(NULL);
-            g_string_append_printf(str, "  %s=<%s>", prop->name, prop->type);
-            if (prop->description) {
-                if (str->len < 24) {
-                    g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
-                }
-                g_string_append_printf(str, " - %s", prop->description);
-            }
-            g_ptr_array_add(array, g_string_free(str, false));
-        }
-        g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
-        if (array->len > 0) {
-            printf("%s options:\n", type);
-        } else {
-            printf("There are no options for %s.\n", type);
-        }
-        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);
+    if (user_creatable_print_help(type, opts)) {
         exit(0);
     }