- for (struct opt_item * item = help->opt; item->cls != OPT_CL_END; item++) {
- if (item->flags & OPT_NO_HELP) continue;
- if (item->cls == OPT_CL_SECTION) {
- sections_cnt++;
- continue;
- }
- if (!*(item->help)) {
- lines_cnt++;
- continue;
- }
- FOREACHLINE(item->help)
- lines_cnt++;
- }
-
- struct opt_sectlist {
- int pos;
- struct opt_section * sect;
- } sections[sections_cnt];
- int s = 0;
-
- const char *lines[lines_cnt][3];
- memset(lines, 0, sizeof(lines));
- int line = 0;
-
- int linelengths[3] = { -1, -1, -1 };
-
- for (struct opt_item * item = help->opt; item->cls != OPT_CL_END; item++) {
- if (item->flags & OPT_NO_HELP) continue;
-
- if (item->cls == OPT_CL_HELP) {
- if (!*(item->help)) {
- line++;
- continue;
- }
-#define SPLITLINES(text) do { \
- FOREACHLINE(text) { \
- int cell = 0; \
- for (const char * b = begin, * e = begin; (e < end) && (e = strchrnul(b, '\t')) && (e > end ? (e = end) : end); b = e+1) { \
- lines[line][cell] = b; \
- if (cell >= 2) \
- break; \
- else \
- if (*e == '\t' && linelengths[cell] < (e - b)) \
- linelengths[cell] = e-b; \
- cell++; \
- } \
- line++; \
- } } while (0)
- SPLITLINES(item->help);
- continue;
- }
-
- if (item->cls == OPT_CL_SECTION) {
- sections[s++] = (struct opt_sectlist) { .pos = line, .sect = item->u.section };
- continue;
- }
-
- uns valoff = strchrnul(item->help, '\t') - item->help;
- uns eol = strchrnul(item->help, '\n') - item->help;
- if (valoff > eol)
- valoff = eol;
-#define VAL(it) ((it->flags & OPT_REQUIRED_VALUE) ? stk_printf("=%.*s", valoff, item->help) : ((it->flags & OPT_NO_VALUE) ? "" : stk_printf("(=%.*s)", valoff, item->help)))
- if (item->name) {
- lines[line][1] = stk_printf("--%s%s", item->name, VAL(item));
- if (linelengths[1] < (int) strlen(lines[line][1]))
- linelengths[1] = strlen(lines[line][1]);
- lines[line][0] = "";
- if (linelengths[0] < 0)
- linelengths[0] = 0;
- }
- if (item->letter) {
- lines[line][0] = stk_printf("-%c,", item->letter);
- if (linelengths[0] < (int) strlen(lines[line][0]))
- linelengths[0] = strlen(lines[line][0]);
- }
-#undef VAL