# HG changeset patch # User Matti Hamalainen # Date 1578844907 -7200 # Node ID 446203207cba809fccf851a3b56a7b04e0dc92dd # Parent 1a4359ecc5d522547cfb8a17a9a2db91d8231ef6 Refactor th_args_help() to be more flexible how it works and prints out the option list. diff -r 1a4359ecc5d5 -r 446203207cba th_args.c --- a/th_args.c Sun Jan 12 16:32:02 2020 +0200 +++ b/th_args.c Sun Jan 12 18:01:47 2020 +0200 @@ -245,6 +245,83 @@ } +static inline const char *th_args_get_optarg(const th_optarg *opt) +{ + (void) opt; + return "ARG"; +} + + +static void th_args_help_print_item(FILE *fh, const th_optarg *opt, int *lineWidth, const int maxLineWidth, const BOOL doPrint) +{ + const char *arg = th_args_get_optarg(opt); + char fmtBuf[32]; + int padWidth; + BOOL hasLongOpt = opt->o_long != NULL; + + if (opt->o_short != 0) + { + if (!hasLongOpt && (opt->flags & OPT_ARGREQ)) + { + snprintf(fmtBuf, sizeof(fmtBuf), " -%c <%s>", + opt->o_short, arg); + } + else + { + snprintf(fmtBuf, sizeof(fmtBuf), " -%c,", + opt->o_short); + } + + *lineWidth = strlen(fmtBuf); + if (doPrint) + padWidth = hasLongOpt ? 2 : maxLineWidth - *lineWidth; + else + padWidth = 2; + } + else + { + fmtBuf[0] = 0; + *lineWidth = 0; + padWidth = 4 + 2; + } + + if (doPrint) + { + fputs(fmtBuf, fh); + th_print_pad(fh, padWidth, ' '); + } + *lineWidth += padWidth; + + if (hasLongOpt) + { + if (opt->flags & OPT_ARGREQ) + { + snprintf(fmtBuf, sizeof(fmtBuf), "--%s=<%s>", + opt->o_long, arg); + } + else + { + snprintf(fmtBuf, sizeof(fmtBuf), "--%s", + opt->o_long); + } + + *lineWidth += strlen(fmtBuf); + } + else + fmtBuf[0] = 0; + + if (doPrint) + { + padWidth = hasLongOpt ? maxLineWidth - *lineWidth : 0; + *lineWidth += padWidth; + + fputs(fmtBuf, fh); + th_print_pad(fh, padWidth, ' '); + th_print_wrap(fh, opt->desc, *lineWidth, *lineWidth, th_term_width() - 2); + } +} + + /** * Print help for commandline arguments/options * @param fh stdio file handle to output to @@ -252,42 +329,29 @@ * @param nopts number of elements in options list array * @param flags flags (currently unused) */ -void th_args_help(FILE *fh, - const th_optarg *opts, const int nopts, - const int flags) +void th_args_help(FILE *fh, const th_optarg *opts, + const int nopts, const int flags) { - int index; + int index, maxLineWidth; + (void) flags; - // Print out option list + // Determine width of the options and arguments + maxLineWidth = 0; for (index = 0; index < nopts; index++) { - const th_optarg *opt = &opts[index]; - char tmpStr[128]; - - // Print short option - if (opt->o_short != 0) - { - snprintf(tmpStr, sizeof(tmpStr), - "-%c,", opt->o_short); - } - else - tmpStr[0] = 0; - - fprintf(fh, " %-5s", tmpStr); + int lineWidth = 0; + th_args_help_print_item(NULL, &opts[index], &lineWidth, 0, FALSE); + if (lineWidth > maxLineWidth) + maxLineWidth = lineWidth; + } - // Print long option - if (opt->o_long != NULL) - { - snprintf(tmpStr, sizeof(tmpStr), "--%s%s", - opt->o_long, - (opt->flags & OPT_ARGREQ) ? "=ARG" : ""); - } - else - tmpStr[0] = 0; + maxLineWidth += 2; - fprintf(fh, "%-18s", tmpStr); - - th_print_wrap(fh, opt->desc, 24, 24, th_term_width() - 2); + // Print out the formatted option list + for (index = 0; index < nopts; index++) + { + int lineWidth; + th_args_help_print_item(fh, &opts[index], &lineWidth, maxLineWidth, TRUE); } }