void printHelpTable(void) { int i = 0; int longestFlag = 0; char flagBuf[100]; char lastHeaderType = '\0'; printAdditionalHelp(); for (i = 0; i < numFlags; i++) { int flagLen; if (flagList[i].flagDash[0] == '\0') { flagLen = 2 + strlen(flagList[i].flagDashDash); } else { if (flagList[i].flagDashDash[0] == '\0') { flagLen = 1 + strlen(flagList[i].flagDash) + strlen(flagList[i].subArg); } else { flagLen = 1 + strlen(flagList[i].flagDash) + strlen(flagList[i].subArg) + 2 + 2 + strlen(flagList[i].flagDashDash); } } if (longestFlag < flagLen) { longestFlag = flagLen; } } for (i = 0; i < numFlags; i++) { printHeaders(flagList[i].headerType, &lastHeaderType); if (flagList[i].flagDash[0] == '\0') { snprintf(flagBuf, sizeof(flagBuf), "--%s", flagList[i].flagDashDash); } else { if (flagList[i].flagDashDash[0] == '\0') { snprintf(flagBuf, sizeof(flagBuf), "-%s%s", flagList[i].flagDash, flagList[i].subArg); } else { snprintf(flagBuf, sizeof(flagBuf), "-%s%s, --%s", flagList[i].flagDash, flagList[i].subArg, flagList[i].flagDashDash); } } fprintf(stdout, " %-*s : ", longestFlag, flagBuf); { const char* p; const char* pNext; for (p = flagList[i].description; (pNext = strchr(p, '\n')) != NULL; p = pNext + 1) { fprintf(stdout, "%.*s\n%*s", (int) (pNext - p), p, longestFlag + 6, ""); } fprintf(stdout, "%s\n", p); } } fprintf(stdout, "\n"); }
void printHelpTable(void) { typedef struct _flagType { const char* flag; const char* description; const char headerType; } flagType; static flagType flagList[] = { {"-h, --help", "print this message", 'g'}, {"-nl <n>", "run program using n locales", 'g'}, {"", "(equivalent to setting the numLocales config const)", 'g'}, {"-q, --quiet", "run program in quiet mode", 'g'}, {"-v, --verbose", "run program in verbose mode", 'g'}, {"-b, --blockreport", "report location of blocked threads on SIGINT", 'g'}, {"-t, --taskreport", "report list of pending and executing tasks on SIGINT", 'g'}, {"--gdb", "run program in gdb", 'g'}, {"-s, --<cfgVar>=<val>", "set the value of a config var", 'c'}, {"-f<filename>", "read in a file of config var assignments", 'c'}, {NULL, NULL, ' '} }; int i = 0; int longestFlag = 0; char lastHeaderType = '\0'; printAdditionalHelp(); while (flagList[i].flag) { int thisFlag = strlen(flagList[i].flag); if (longestFlag < thisFlag) { longestFlag = thisFlag; } i++; } i = 0; while (flagList[i].flag) { printHeaders(flagList[i].headerType, &lastHeaderType); if (flagList[i].flag[0] == '\0') { fprintf(stdout, " %-*s %s\n", longestFlag, flagList[i].flag, flagList[i].description); } else { fprintf(stdout, " %-*s : %s\n", longestFlag, flagList[i].flag, flagList[i].description); } i++; } fprintf(stdout, "\n"); }