From e879e63f5b37bce80855a2f23cf5a1c29b855375 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 1 Mar 2024 14:51:46 +0100 Subject: [PATCH] vshCmddefHelp: Refactor printing of help (list of arguments) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract flag check to a separate variable and replace ternary operators by normal conditions and directly output the text rather than using extra variable to improve readability. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- tools/vsh.c | 74 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/tools/vsh.c b/tools/vsh.c index ef7e86092b..6abfd3982d 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -630,47 +630,63 @@ vshCmddefHelp(const vshCmdDef *def) if (def->opts) { const vshCmdOptDef *opt; for (opt = def->opts; opt->name; opt++) { - const char *fmt = "%s"; + bool required_option = opt->flags & VSH_OFLAG_REQ; + switch (opt->type) { case VSH_OT_BOOL: - fmt = "[--%s]"; + fprintf(stdout, " [--%s]", opt->name); break; + case VSH_OT_INT: - /* xgettext:c-format */ - fmt = ((opt->flags & VSH_OFLAG_REQ) ? "<%s>" - : _("[--%1$s ]")); - if (!(opt->flags & VSH_OFLAG_REQ_OPT)) - shortopt = true; - break; - case VSH_OT_STRING: - /* xgettext:c-format */ - fmt = _("[--%1$s ]"); - if (!(opt->flags & VSH_OFLAG_REQ_OPT)) - shortopt = true; - break; - case VSH_OT_DATA: - fmt = ((opt->flags & VSH_OFLAG_REQ) ? "<%s>" : "[<%s>]"); - if (!(opt->flags & VSH_OFLAG_REQ_OPT)) - shortopt = true; - break; - case VSH_OT_ARGV: - /* xgettext:c-format */ - if (shortopt) { - fmt = (opt->flags & VSH_OFLAG_REQ) - ? _("{[--%1$s] }...") - : _("[[--%1$s] ]..."); + if (required_option) { + fprintf(stdout, " <%s>", opt->name); } else { - fmt = (opt->flags & VSH_OFLAG_REQ) ? _("<%1$s>...") - : _("[<%1$s>]..."); + fprintf(stdout, _(" [--%1$s ]"), opt->name); + } + + if (!(opt->flags & VSH_OFLAG_REQ_OPT)) + shortopt = true; + break; + + case VSH_OT_STRING: + fprintf(stdout, _(" [--%1$s ]"), opt->name); + + if (!(opt->flags & VSH_OFLAG_REQ_OPT)) + shortopt = true; + break; + + case VSH_OT_DATA: + if (required_option) { + fprintf(stdout, " <%s>", opt->name); + } else { + fprintf(stdout, " [<%s>]", opt->name); + } + + if (!(opt->flags & VSH_OFLAG_REQ_OPT)) + shortopt = true; + break; + + case VSH_OT_ARGV: + if (shortopt) { + if (required_option) { + fprintf(stdout, _(" {[--%1$s] }..."), opt->name); + } else { + fprintf(stdout, _(" [[--%1$s] ]..."), opt->name); + } + } else { + if (required_option) { + fprintf(stdout, " <%s>...", opt->name); + } else { + fprintf(stdout, " [<%s>]...", opt->name); + } } break; + case VSH_OT_ALIAS: case VSH_OT_NONE: /* aliases are intentionally undocumented */ continue; } - fputc(' ', stdout); - fprintf(stdout, fmt, opt->name); } } fputc('\n', stdout);