[Libguestfs] [nbdkit PATCH v2 2/5] main: Saner newline printing during --help

Eric Blake eblake at redhat.com
Thu Nov 8 21:29:49 UTC 2018


We are inconsistent on whether our plugins and filters end in
a newline; as a result, things like
  nbdkit --help --filter=log --filter=nozero --filter=delay memory
have inconsistent spacing between sections.  Rather than documenting
whether the user must have/avoid a trailing newline, it's nicer to
just supply one ourselves only when it is missing.

Signed-off-by: Eric Blake <eblake at redhat.com>
---

I'm not sure if it's worth factoring out a helper function that
either prints newline or returns ""/"\n" based on whether the
passed in parameter ends in newline; in this patch,  it is
open-coded 4 times.

 src/filters.c | 13 ++++++++-----
 src/plugins.c | 13 ++++++++-----
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/filters.c b/src/filters.c
index 3626742..773f4df 100644
--- a/src/filters.c
+++ b/src/filters.c
@@ -134,19 +134,22 @@ static void
 filter_usage (struct backend *b)
 {
   struct backend_filter *f = container_of (b, struct backend_filter, backend);
+  const char *p;

   printf ("filter: %s", f->name);
   if (f->filter.longname)
     printf (" (%s)", f->filter.longname);
   printf ("\n");
-  printf ("(%s)", f->filename);
+  printf ("(%s)\n", f->filename);
   if (f->filter.description) {
-    printf ("\n");
-    printf ("%s\n", f->filter.description);
+    printf ("%s", f->filter.description);
+    if ((p = strrchr (f->filter.description, '\n')) == NULL || p[1])
+      printf ("\n");
   }
   if (f->filter.config_help) {
-    printf ("\n");
-    printf ("%s\n", f->filter.config_help);
+    printf ("%s", f->filter.config_help);
+    if ((p = strrchr (f->filter.config_help, '\n')) == NULL || p[1])
+      printf ("\n");
   }
 }

diff --git a/src/plugins.c b/src/plugins.c
index 2bea6ac..912c226 100644
--- a/src/plugins.c
+++ b/src/plugins.c
@@ -103,19 +103,22 @@ static void
 plugin_usage (struct backend *b)
 {
   struct backend_plugin *p = container_of (b, struct backend_plugin, backend);
+  const char *t;

   printf ("plugin: %s", p->name);
   if (p->plugin.longname)
     printf (" (%s)", p->plugin.longname);
   printf ("\n");
-  printf ("(%s)", p->filename);
+  printf ("(%s)\n", p->filename);
   if (p->plugin.description) {
-    printf ("\n");
-    printf ("%s\n", p->plugin.description);
+    printf ("%s", p->plugin.description);
+    if ((t = strrchr (p->plugin.description, '\n')) == NULL || t[1])
+      printf ("\n");
   }
   if (p->plugin.config_help) {
-    printf ("\n");
-    printf ("%s\n", p->plugin.config_help);
+    printf ("%s", p->plugin.config_help);
+    if ((t = strrchr (p->plugin.config_help, '\n')) == NULL || t[1])
+      printf ("\n");
   }
 }

-- 
2.17.2




More information about the Libguestfs mailing list