[PATCH 24/26] qom: introduce object_property_help()

Marc-André Lureau posted 26 patches 6 years, 1 month ago
[PATCH 24/26] qom: introduce object_property_help()
Posted by Marc-André Lureau 6 years, 1 month ago
Let's factor out the code to format a help string for a property. We
are going to reuse it in qdev next, which will bring some consistency.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/qom/object.h    | 13 +++++++++++++
 qom/object_interfaces.c | 39 +++++++++++++++++++++++----------------
 2 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/include/qom/object.h b/include/qom/object.h
index 933e5c6cb9..a28e37a648 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -1854,6 +1854,19 @@ Object *container_get(Object *root, const char *path);
  */
 size_t object_type_get_instance_size(const char *typename);
 
+/**
+ * object_property_help:
+ * @name: the name of the property
+ * @type: the type of the property
+ * @defval: the default value
+ * @description: description of the property
+ *
+ * Returns: a user-friendly formatted string describing the property
+ * for help purposes.
+ */
+char *object_property_help(const char *name, const char *type,
+                           const char *defval, const char *description);
+
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref)
 
 #endif
diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
index edb4cc4a3d..b7157d6ab0 100644
--- a/qom/object_interfaces.c
+++ b/qom/object_interfaces.c
@@ -158,6 +158,25 @@ int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
     return 0;
 }
 
+char *object_property_help(const char *name, const char *type,
+                           const char *defval, const char *description)
+{
+    GString *str = g_string_new(NULL);
+
+    g_string_append_printf(str, "  %s=<%s>", name, type);
+    if (defval) {
+        g_string_append_printf(str, " (default: %s)", defval);
+    }
+    if (description) {
+        if (str->len < 24) {
+            g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
+        }
+        g_string_append_printf(str, " - %s", description);
+    }
+
+    return g_string_free(str, false);
+}
+
 bool user_creatable_print_help(const char *type, QemuOpts *opts)
 {
     ObjectClass *klass;
@@ -184,27 +203,15 @@ bool user_creatable_print_help(const char *type, QemuOpts *opts)
 
         object_class_property_iter_init(&iter, klass);
         while ((prop = object_property_iter_next(&iter))) {
-            GString *str;
-            char *defval;
+            g_autofree char *defval = object_property_get_default(prop);
 
             if (!prop->set) {
                 continue;
             }
 
-            str = g_string_new(NULL);
-            g_string_append_printf(str, "  %s=<%s>", prop->name, prop->type);
-            defval = object_property_get_default(prop);
-            if (defval) {
-                g_string_append_printf(str, " (default: %s)", defval);
-                g_free(defval);
-            }
-            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_add(array,
+                            object_property_help(prop->name, prop->type,
+                                                 defval, prop->description));
         }
         g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
         if (array->len > 0) {
-- 
2.25.0.rc1.20.g2443f3f80d.dirty


Re: [PATCH 24/26] qom: introduce object_property_help()
Posted by Thomas Huth 4 years, 11 months ago
On 10/01/2020 16.30, Marc-André Lureau wrote:
> Let's factor out the code to format a help string for a property. We
> are going to reuse it in qdev next, which will bring some consistency.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>   include/qom/object.h    | 13 +++++++++++++
>   qom/object_interfaces.c | 39 +++++++++++++++++++++++----------------
>   2 files changed, 36 insertions(+), 16 deletions(-)
> 
> diff --git a/include/qom/object.h b/include/qom/object.h
> index 933e5c6cb9..a28e37a648 100644
> --- a/include/qom/object.h
> +++ b/include/qom/object.h
> @@ -1854,6 +1854,19 @@ Object *container_get(Object *root, const char *path);
>    */
>   size_t object_type_get_instance_size(const char *typename);
>   
> +/**
> + * object_property_help:
> + * @name: the name of the property
> + * @type: the type of the property
> + * @defval: the default value
> + * @description: description of the property
> + *
> + * Returns: a user-friendly formatted string describing the property
> + * for help purposes.
> + */
> +char *object_property_help(const char *name, const char *type,
> +                           const char *defval, const char *description);
> +
>   G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref)
>   
>   #endif
> diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
> index edb4cc4a3d..b7157d6ab0 100644
> --- a/qom/object_interfaces.c
> +++ b/qom/object_interfaces.c
> @@ -158,6 +158,25 @@ int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
>       return 0;
>   }
>   
> +char *object_property_help(const char *name, const char *type,
> +                           const char *defval, const char *description)
> +{
> +    GString *str = g_string_new(NULL);
> +
> +    g_string_append_printf(str, "  %s=<%s>", name, type);
> +    if (defval) {
> +        g_string_append_printf(str, " (default: %s)", defval);
> +    }

  Hi!

There is something weird going on wit the default value text...

When I run:

$ qemu-system-x86_64 -device isa-fdc,help

I get:

isa-fdc options:
   bootindexA=<int32>
   bootindexB=<int32>
   dma=<uint32>           -  (default: (null))
   driveA=<str>           - Node name or ID of a block device to use as a backend
   driveB=<str>           - Node name or ID of a block device to use as a backend
   fallback=<FdcDriveType> - FDC drive type, 144/288/120/none/auto (default: (null))
   fdtypeA=<FdcDriveType> - FDC drive type, 144/288/120/none/auto (default: (null))
   fdtypeB=<FdcDriveType> - FDC drive type, 144/288/120/none/auto (default: (null))
   iobase=<uint32>        -  (default: (null))
   irq=<uint32>           -  (default: (null))

Why is it showing "(null)" as default values?

  Thomas


Re: [PATCH 24/26] qom: introduce object_property_help()
Posted by Marc-André Lureau 4 years, 11 months ago
Hi

On Fri, Mar 12, 2021 at 4:54 PM Thomas Huth <thuth@redhat.com> wrote:

> On 10/01/2020 16.30, Marc-André Lureau wrote:
> > Let's factor out the code to format a help string for a property. We
> > are going to reuse it in qdev next, which will bring some consistency.
> >
> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > ---
> >   include/qom/object.h    | 13 +++++++++++++
> >   qom/object_interfaces.c | 39 +++++++++++++++++++++++----------------
> >   2 files changed, 36 insertions(+), 16 deletions(-)
> >
> > diff --git a/include/qom/object.h b/include/qom/object.h
> > index 933e5c6cb9..a28e37a648 100644
> > --- a/include/qom/object.h
> > +++ b/include/qom/object.h
> > @@ -1854,6 +1854,19 @@ Object *container_get(Object *root, const char
> *path);
> >    */
> >   size_t object_type_get_instance_size(const char *typename);
> >
> > +/**
> > + * object_property_help:
> > + * @name: the name of the property
> > + * @type: the type of the property
> > + * @defval: the default value
> > + * @description: description of the property
> > + *
> > + * Returns: a user-friendly formatted string describing the property
> > + * for help purposes.
> > + */
> > +char *object_property_help(const char *name, const char *type,
> > +                           const char *defval, const char *description);
> > +
> >   G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref)
> >
> >   #endif
> > diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
> > index edb4cc4a3d..b7157d6ab0 100644
> > --- a/qom/object_interfaces.c
> > +++ b/qom/object_interfaces.c
> > @@ -158,6 +158,25 @@ int user_creatable_add_opts_foreach(void *opaque,
> QemuOpts *opts, Error **errp)
> >       return 0;
> >   }
> >
> > +char *object_property_help(const char *name, const char *type,
> > +                           const char *defval, const char *description)
> > +{
> > +    GString *str = g_string_new(NULL);
> > +
> > +    g_string_append_printf(str, "  %s=<%s>", name, type);
> > +    if (defval) {
> > +        g_string_append_printf(str, " (default: %s)", defval);
> > +    }
>
>   Hi!
>
> There is something weird going on wit the default value text...
>
> When I run:
>
> $ qemu-system-x86_64 -device isa-fdc,help
>
> I get:
>
> isa-fdc options:
>    bootindexA=<int32>
>    bootindexB=<int32>
>    dma=<uint32>           -  (default: (null))
>    driveA=<str>           - Node name or ID of a block device to use as a
> backend
>    driveB=<str>           - Node name or ID of a block device to use as a
> backend
>    fallback=<FdcDriveType> - FDC drive type, 144/288/120/none/auto
> (default: (null))
>    fdtypeA=<FdcDriveType> - FDC drive type, 144/288/120/none/auto
> (default: (null))
>    fdtypeB=<FdcDriveType> - FDC drive type, 144/288/120/none/auto
> (default: (null))
>    iobase=<uint32>        -  (default: (null))
>    irq=<uint32>           -  (default: (null))
>
> Why is it showing "(null)" as default values?
>

Congratulations, you found a regression! :)

QEMU emulator version 5.2.0 (qemu-5.2.0-6.fc33.1)
qemu-system-x86_64 -device isa-fdc,help
isa-fdc options:
  bootindexA=<int32>
  bootindexB=<int32>
  check_media_rate=<bool> - on/off (default: true)
  dma=<uint32>           -  (default: 2)
  driveA=<str>           - Node name or ID of a block device to use as a
backend
  driveB=<str>           - Node name or ID of a block device to use as a
backend
  fallback=<FdcDriveType> - FDC drive type, 144/288/120/none/auto (default:
"288")
  fdtypeA=<FdcDriveType> - FDC drive type, 144/288/120/none/auto (default:
"auto")
  fdtypeB=<FdcDriveType> - FDC drive type, 144/288/120/none/auto (default:
"auto")
  iobase=<uint32>        -  (default: 1008)
  irq=<uint32>           -  (default: 6)