vshCmddefHelp: Refactor printing of help (list of arguments)

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 <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 465091d2b8
commit e879e63f5b

View File

@ -630,47 +630,63 @@ vshCmddefHelp(const vshCmdDef *def)
if (def->opts) { if (def->opts) {
const vshCmdOptDef *opt; const vshCmdOptDef *opt;
for (opt = def->opts; opt->name; opt++) { for (opt = def->opts; opt->name; opt++) {
const char *fmt = "%s"; bool required_option = opt->flags & VSH_OFLAG_REQ;
switch (opt->type) { switch (opt->type) {
case VSH_OT_BOOL: case VSH_OT_BOOL:
fmt = "[--%s]"; fprintf(stdout, " [--%s]", opt->name);
break; break;
case VSH_OT_INT: case VSH_OT_INT:
/* xgettext:c-format */ if (required_option) {
fmt = ((opt->flags & VSH_OFLAG_REQ) ? "<%s>" fprintf(stdout, " <%s>", opt->name);
: _("[--%1$s <number>]"));
if (!(opt->flags & VSH_OFLAG_REQ_OPT))
shortopt = true;
break;
case VSH_OT_STRING:
/* xgettext:c-format */
fmt = _("[--%1$s <string>]");
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] <string>}...")
: _("[[--%1$s] <string>]...");
} else { } else {
fmt = (opt->flags & VSH_OFLAG_REQ) ? _("<%1$s>...") fprintf(stdout, _(" [--%1$s <number>]"), opt->name);
: _("[<%1$s>]..."); }
if (!(opt->flags & VSH_OFLAG_REQ_OPT))
shortopt = true;
break;
case VSH_OT_STRING:
fprintf(stdout, _(" [--%1$s <string>]"), 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] <string>}..."), opt->name);
} else {
fprintf(stdout, _(" [[--%1$s] <string>]..."), opt->name);
}
} else {
if (required_option) {
fprintf(stdout, " <%s>...", opt->name);
} else {
fprintf(stdout, " [<%s>]...", opt->name);
}
} }
break; break;
case VSH_OT_ALIAS: case VSH_OT_ALIAS:
case VSH_OT_NONE: case VSH_OT_NONE:
/* aliases are intentionally undocumented */ /* aliases are intentionally undocumented */
continue; continue;
} }
fputc(' ', stdout);
fprintf(stdout, fmt, opt->name);
} }
} }
fputc('\n', stdout); fputc('\n', stdout);