From e1211d38a41940ca13581578fb4ef62990b9c25b Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Sat, 27 Apr 2024 19:26:32 +0200 Subject: [PATCH] command: refactor test for relative opt Use _update_relative_opt to check for relative option and update val_enum accordingly. --- tools/command.c | 47 +++++++++++++++++++++++++++---------------- tools/man-generator.c | 18 ++--------------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/tools/command.c b/tools/command.c index 146bf5b0b..c39295a54 100644 --- a/tools/command.c +++ b/tools/command.c @@ -1625,25 +1625,38 @@ static void _print_usage_description(struct command *cmd) } } +static void _update_relative_opt(const char *name, int opt_enum, int *val_enum) +{ + /* check for relative sign */ + switch (opt_enum) { + case extents_ARG: + case mirrors_ARG: + case poolmetadatasize_ARG: + case size_ARG: + /* + * Suppress the [+] prefix for lvcreate which we have to + * accept for backwards compat, but don't want to advertise. + */ + if (!strcmp(name, "lvcreate")) { + switch (*val_enum) { + case psizemb_VAL: + *val_enum = sizemb_VAL; + break; + case pextents_VAL: + *val_enum = extents_VAL; + break; + case pnumber_VAL: + if (opt_enum == mirrors_ARG) + *val_enum = number_VAL; + break; + } + } + } +} + static void _print_val_usage(struct command *cmd, int opt_enum, int val_enum) { - int is_relative_opt = (opt_enum == size_ARG) || - (opt_enum == extents_ARG) || - (opt_enum == poolmetadatasize_ARG) || - (opt_enum == mirrors_ARG); - - /* - * Suppress the [+] prefix for lvcreate which we have to - * accept for backwards compat, but don't want to advertise. - */ - if (!strcmp(cmd->name, "lvcreate") && is_relative_opt) { - if (val_enum == psizemb_VAL) - val_enum = sizemb_VAL; - else if (val_enum == pextents_VAL) - val_enum = extents_VAL; - else if ((val_enum == pnumber_VAL) && (opt_enum == mirrors_ARG)) - val_enum = number_VAL; - } + _update_relative_opt(cmd->name, opt_enum, &val_enum); if (!val_names[val_enum].usage) printf("%s", val_names[val_enum].name); diff --git a/tools/man-generator.c b/tools/man-generator.c index 50047da3c..03322dc40 100644 --- a/tools/man-generator.c +++ b/tools/man-generator.c @@ -191,24 +191,10 @@ static void _print_val_man(struct command_name *cname, int opt_enum, int val_enu char *line_argv[MAX_LINE_ARGC]; int line_argc; int i; - int is_relative_opt = (opt_enum == size_ARG) || - (opt_enum == extents_ARG) || - (opt_enum == poolmetadatasize_ARG) || - (opt_enum == mirrors_ARG); _was_hyphen = 0; - /* - * Suppress the [+] prefix for lvcreate which we have to - * accept for backwards compat, but don't want to advertise. - */ - if (!strcmp(cname->name, "lvcreate") && is_relative_opt) { - if (val_enum == psizemb_VAL) - val_enum = sizemb_VAL; - else if (val_enum == pextents_VAL) - val_enum = extents_VAL; - else if ((val_enum == pnumber_VAL) && (opt_enum == mirrors_ARG)) - val_enum = number_VAL; - } + + _update_relative_opt(cname->name, opt_enum, &val_enum); switch (val_enum) { case sizemb_VAL: