[PATCH 12/23] vshCmddefHelp: Refactor printing of help (argument description)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Extract flag check to a separate variable and replace ternary operators
by normal conditions and use allocated buffer instead of a static one
to improve readability.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 tools/vsh.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/tools/vsh.c b/tools/vsh.c
index add50fe670..af5a576932 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -619,7 +619,6 @@ vshCmdGrpHelp(vshControl *ctl, const vshCmdGrp *grp)
 static bool
 vshCmddefHelp(const vshCmdDef *def)
 {
-    char buf[256];
     bool shortopt = false; /* true if 'arg' works instead of '--opt arg' */

     fputs(_("  NAME\n"), stdout);
@@ -701,33 +700,44 @@ vshCmddefHelp(const vshCmdDef *def)
         const vshCmdOptDef *opt;
         fputs(_("\n  OPTIONS\n"), stdout);
         for (opt = def->opts; opt->name; opt++) {
+            bool required_option = opt->flags & VSH_OFLAG_REQ;
+            g_autofree char *optstr = NULL;
+
             switch (opt->type) {
             case VSH_OT_BOOL:
-                g_snprintf(buf, sizeof(buf), "--%s", opt->name);
+                optstr = g_strdup_printf("--%s", opt->name);
                 break;
+
             case VSH_OT_INT:
-                g_snprintf(buf, sizeof(buf),
-                           (opt->flags & VSH_OFLAG_REQ) ? _("[--%1$s] <number>")
-                           : _("--%1$s <number>"), opt->name);
+                if (required_option) {
+                    optstr = g_strdup_printf(_("[--%1$s] <number>"), opt->name);
+                } else {
+                    optstr = g_strdup_printf(_("--%1$s <number>"), opt->name);
+                }
                 break;
+
             case VSH_OT_STRING:
-                g_snprintf(buf, sizeof(buf), _("--%1$s <string>"), opt->name);
+                optstr = g_strdup_printf(_("--%1$s <string>"), opt->name);
                 break;
+
             case VSH_OT_DATA:
-                g_snprintf(buf, sizeof(buf), _("[--%1$s] <string>"),
-                           opt->name);
+                optstr = g_strdup_printf(_("[--%1$s] <string>"), opt->name);
                 break;
+
             case VSH_OT_ARGV:
-                g_snprintf(buf, sizeof(buf),
-                           shortopt ? _("[--%1$s] <string>") : _("<%1$s>"),
-                           opt->name);
+                if (shortopt) {
+                    optstr = g_strdup_printf(_("[--%1$s] <string>"), opt->name);
+                } else {
+                    optstr = g_strdup_printf("<%s>", opt->name);
+                }
                 break;
+
             case VSH_OT_ALIAS:
             case VSH_OT_NONE:
                 continue;
             }

-            fprintf(stdout, "    %-15s  %s\n", buf, _(opt->help));
+            fprintf(stdout, "    %-15s  %s\n", optstr, _(opt->help));
         }
     }
     fputc('\n', stdout);
-- 
2.44.0
_______________________________________________
Devel mailing list -- devel@xxxxxxxxxxxxxxxxx
To unsubscribe send an email to devel-leave@xxxxxxxxxxxxxxxxx




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux