From: Karel Zak Date: Mon, 3 Nov 2025 12:41:31 +0000 (+0100) Subject: include/optutils: improve err_exclusive_options() output X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=abfce923bb66aa2a95264867e34ca5051031452b;p=thirdparty%2Futil-linux.git include/optutils: improve err_exclusive_options() output OLD: $ losetup --remove --detach loop0 losetup: mutually exclusive arguments: --detach-all --all --set-capacity --detach --find --associated --remove NEW: $ losetup --remove --detach loop0 losetup: options --detach and --remove cannot be combined. Suggested-by: Benno Schulenberg Signed-off-by: Karel Zak --- diff --git a/include/optutils.h b/include/optutils.h index 6fe4aeb7a..87d20d0d5 100644 --- a/include/optutils.h +++ b/include/optutils.h @@ -11,7 +11,11 @@ #include "nls.h" #include "cctype.h" -static inline const char *option_to_longopt(int c, const struct option *opts) +/* + * Converts the short option @c to the corresponding long option from @opts, or + * returns NULL. + */ +static inline const char *ul_get_longopt(const struct option *opts, int c) { const struct option *o; @@ -22,6 +26,19 @@ static inline const char *option_to_longopt(int c, const struct option *opts) return NULL; } +/* + * Converts the short options @c to "%c" or "0x" if not printable. + */ +static inline const char *ul_get_shortopt(char *buf, size_t bufsz, int c) +{ + if (c_isgraph(c)) + snprintf(buf, bufsz, "%c", c); + else + snprintf(buf, bufsz, "<0x%02x>", c); /* should not happen */ + + return buf; +} + #ifndef OPTUTILS_EXIT_CODE # define OPTUTILS_EXIT_CODE EXIT_FAILURE #endif @@ -84,23 +101,16 @@ static inline void err_exclusive_options( if (status[e] == 0) status[e] = c; else if (status[e] != c) { - size_t ct = 0; + const char *a = ul_get_longopt(opts, status[e]); + const char *b = ul_get_longopt(opts, c); + char buf[16]; /* short option in hex */ - fprintf(stderr, _("%s: mutually exclusive " - "arguments:"), - program_invocation_short_name); - - for (op = excl[e]; - ct + 1 < ARRAY_SIZE(excl[0]) && *op; - op++, ct++) { - const char *n = option_to_longopt(*op, opts); - if (n) - fprintf(stderr, " --%s", n); - else if (c_isgraph(*op)) - fprintf(stderr, " -%c", *op); - } - fputc('\n', stderr); - exit(OPTUTILS_EXIT_CODE); + errx(OPTUTILS_EXIT_CODE, + _("options %s%s and %s%s cannot be combined"), + a ? "--" : "-", + a ? a : ul_get_shortopt(buf, sizeof(buf), status[e]), + b ? "--" : "-", + b ? b : ul_get_shortopt(buf, sizeof(buf), c)); } break; } @@ -108,4 +118,3 @@ static inline void err_exclusive_options( } #endif -