vshCmddefHelp: Refactor printing of help (argument description)

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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2024-03-01 14:51:46 +01:00
parent e879e63f5b
commit a191c5d455

View File

@ -619,7 +619,6 @@ vshCmdGrpHelp(vshControl *ctl, const vshCmdGrp *grp)
static bool static bool
vshCmddefHelp(const vshCmdDef *def) vshCmddefHelp(const vshCmdDef *def)
{ {
char buf[256];
bool shortopt = false; /* true if 'arg' works instead of '--opt arg' */ bool shortopt = false; /* true if 'arg' works instead of '--opt arg' */
fputs(_(" NAME\n"), stdout); fputs(_(" NAME\n"), stdout);
@ -701,33 +700,44 @@ vshCmddefHelp(const vshCmdDef *def)
const vshCmdOptDef *opt; const vshCmdOptDef *opt;
fputs(_("\n OPTIONS\n"), stdout); fputs(_("\n OPTIONS\n"), stdout);
for (opt = def->opts; opt->name; opt++) { for (opt = def->opts; opt->name; opt++) {
bool required_option = opt->flags & VSH_OFLAG_REQ;
g_autofree char *optstr = NULL;
switch (opt->type) { switch (opt->type) {
case VSH_OT_BOOL: case VSH_OT_BOOL:
g_snprintf(buf, sizeof(buf), "--%s", opt->name); optstr = g_strdup_printf("--%s", opt->name);
break; break;
case VSH_OT_INT: case VSH_OT_INT:
g_snprintf(buf, sizeof(buf), if (required_option) {
(opt->flags & VSH_OFLAG_REQ) ? _("[--%1$s] <number>") optstr = g_strdup_printf(_("[--%1$s] <number>"), opt->name);
: _("--%1$s <number>"), opt->name); } else {
optstr = g_strdup_printf(_("--%1$s <number>"), opt->name);
}
break; break;
case VSH_OT_STRING: case VSH_OT_STRING:
g_snprintf(buf, sizeof(buf), _("--%1$s <string>"), opt->name); optstr = g_strdup_printf(_("--%1$s <string>"), opt->name);
break; break;
case VSH_OT_DATA: case VSH_OT_DATA:
g_snprintf(buf, sizeof(buf), _("[--%1$s] <string>"), optstr = g_strdup_printf(_("[--%1$s] <string>"), opt->name);
opt->name);
break; break;
case VSH_OT_ARGV: case VSH_OT_ARGV:
g_snprintf(buf, sizeof(buf), if (shortopt) {
shortopt ? _("[--%1$s] <string>") : _("<%1$s>"), optstr = g_strdup_printf(_("[--%1$s] <string>"), opt->name);
opt->name); } else {
optstr = g_strdup_printf("<%s>", opt->name);
}
break; break;
case VSH_OT_ALIAS: case VSH_OT_ALIAS:
case VSH_OT_NONE: case VSH_OT_NONE:
continue; continue;
} }
fprintf(stdout, " %-15s %s\n", buf, _(opt->help)); fprintf(stdout, " %-15s %s\n", optstr, _(opt->help));
} }
} }
fputc('\n', stdout); fputc('\n', stdout);