From 34151214f3191dfda9431fa2bb5395dda7ff0762 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 5 Mar 2024 12:35:51 +0100 Subject: [PATCH] vshCmddefGetOption: Improve readability MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Declare one argument per line, separate disticnt conditions by newline, move some checks earlier. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- tools/vsh.c | 67 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/tools/vsh.c b/tools/vsh.c index c344797cbb..c4099bb725 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -468,9 +468,14 @@ static vshCmdOptDef helpopt = { .type = VSH_OT_BOOL, .help = N_("print help for this function") }; + static const vshCmdOptDef * -vshCmddefGetOption(vshControl *ctl, const vshCmdDef *cmd, const char *name, - uint64_t *opts_seen, size_t *opt_index, char **optstr, +vshCmddefGetOption(vshControl *ctl, + const vshCmdDef *cmd, + const char *name, + uint64_t *opts_seen, + size_t *opt_index, + char **optstr, bool report) { size_t i; @@ -482,39 +487,43 @@ vshCmddefGetOption(vshControl *ctl, const vshCmdDef *cmd, const char *name, for (i = 0; cmd->opts && cmd->opts[i].name; i++) { const vshCmdOptDef *opt = &cmd->opts[i]; - if (STREQ(opt->name, name)) { - if (opt->type == VSH_OT_ALIAS) { - char *value; + if (STRNEQ(opt->name, name)) + continue; - /* Two types of replacements: - opt->help = "string": straight replacement of name - opt->help = "string=value": treat boolean flag as - alias of option and its default value */ - alias = g_strdup(opt->help); - name = alias; - if ((value = strchr(name, '='))) { - *value = '\0'; - if (*optstr) { - if (report) - vshError(ctl, _("invalid '=' after option --%1$s"), - opt->name); - return NULL; - } - *optstr = g_strdup(value + 1); + if (opt->type == VSH_OT_ALIAS) { + char *value; + + /* Two types of replacements: + opt->help = "string": straight replacement of name + opt->help = "string=value": treat boolean flag as + alias of option and its default value */ + alias = g_strdup(opt->help); + name = alias; + if ((value = strchr(name, '='))) { + *value = '\0'; + if (*optstr) { + if (report) + vshError(ctl, _("invalid '=' after option --%1$s"), + opt->name); + return NULL; } - continue; + *optstr = g_strdup(value + 1); } - if ((*opts_seen & (1ULL << i)) && opt->type != VSH_OT_ARGV) { - if (report) - vshError(ctl, _("option --%1$s already seen"), name); - return NULL; - } - *opts_seen |= 1ULL << i; - *opt_index = i; - return opt; + continue; } + + if ((*opts_seen & (1ULL << i)) && opt->type != VSH_OT_ARGV) { + if (report) + vshError(ctl, _("option --%1$s already seen"), name); + return NULL; + } + + *opts_seen |= 1ULL << i; + *opt_index = i; + return opt; } + /* The 'help' command ignores extra options */ if (STRNEQ(cmd->name, "help") && report) { vshError(ctl, _("command '%1$s' doesn't support option --%2$s"), cmd->name, name);