[common] option: Reformat help and support rST tables

Corners of table have '+' added, and adds new command line flag --rst-help,
which adds some extra formatting to the make it an rST compliant table for the
in-line docs.
This commit is contained in:
Jonathan Rubenstein 2021-05-05 14:10:46 -04:00 committed by Geoffrey McRae
parent 903cc9815f
commit c6d7fb8dd0
2 changed files with 44 additions and 19 deletions

View File

@ -32,6 +32,13 @@ enum OptionType
OPTION_TYPE_CUSTOM OPTION_TYPE_CUSTOM
}; };
enum doHelpMode
{
DOHELP_MODE_NO = 0,
DOHELP_MODE_YES,
DOHELP_MODE_RST
};
struct Option; struct Option;
struct Option struct Option

View File

@ -37,7 +37,7 @@ struct OptionGroup
struct State struct State
{ {
bool doHelp; enum doHelpMode doHelp;
struct Option ** options; struct Option ** options;
int oCount; int oCount;
struct OptionGroup * groups; struct OptionGroup * groups;
@ -46,7 +46,7 @@ struct State
static struct State state = static struct State state =
{ {
.doHelp = false, .doHelp = DOHELP_MODE_NO,
.options = NULL, .options = NULL,
.oCount = 0, .oCount = 0,
.groups = NULL, .groups = NULL,
@ -258,7 +258,13 @@ bool option_parse(int argc, char * argv[])
{ {
if (strcmp(argv[a], "-h") == 0 || strcmp(argv[a], "--help") == 0) if (strcmp(argv[a], "-h") == 0 || strcmp(argv[a], "--help") == 0)
{ {
state.doHelp = true; state.doHelp = DOHELP_MODE_YES;
continue;
}
if (strcmp(argv[a], "--rst-help") == 0)
{
state.doHelp = DOHELP_MODE_RST;
continue; continue;
} }
@ -528,7 +534,7 @@ exit:
bool option_validate(void) bool option_validate(void)
{ {
if (state.doHelp) if (state.doHelp != DOHELP_MODE_NO)
{ {
option_print(); option_print();
return false; return false;
@ -577,6 +583,24 @@ bool option_validate(void)
return ok; return ok;
} }
void option_print_hrule(char * headerLine, int maxLen, char ruleChar)
{
printf(" +%c", ruleChar);
for (int i = 0; i < maxLen; i++)
{
if(i < strlen(headerLine))
{
if (headerLine[i] == '|')
{
putc('+', stdout);
continue;
}
}
putc(ruleChar, stdout);
}
printf("%c+\n", ruleChar);
}
void option_print(void) void option_print(void)
{ {
printf( printf(
@ -591,6 +615,7 @@ void option_print(void)
int maxLen; int maxLen;
int valueLen = 5; int valueLen = 5;
char * line; char * line;
char * headerLine;
// ensure the pad length is atleast as wide as the heading // ensure the pad length is atleast as wide as the heading
if (state.groups[g].pad < 4) if (state.groups[g].pad < 4)
@ -626,6 +651,7 @@ void option_print(void)
); );
assert(maxLen > 0); assert(maxLen > 0);
headerLine = line;
stringlist_push(lines, line); stringlist_push(lines, line);
for(int i = 0; i < state.groups[g].count; ++i) for(int i = 0; i < state.groups[g].count; ++i)
@ -659,11 +685,7 @@ void option_print(void)
{ {
if (i == 0) if (i == 0)
{ {
// print a horizontal rule option_print_hrule(headerLine, maxLen, '-');
printf(" |");
for(int i = 0; i < maxLen + 2; ++i)
putc('-', stdout);
printf("|\n");
} }
char * line = stringlist_at(lines, i); char * line = stringlist_at(lines, i);
@ -671,19 +693,15 @@ void option_print(void)
if (i == 0) if (i == 0)
{ {
// print a horizontal rule option_print_hrule(headerLine, maxLen, state.doHelp == DOHELP_MODE_RST ? '=' : '-');
printf(" |"); }
for(int i = 0; i < maxLen + 2; ++i) else if (state.doHelp == DOHELP_MODE_RST && i < stringlist_count(lines) - 1)
putc('-', stdout); {
printf("|\n"); option_print_hrule(headerLine, maxLen, '-');
} }
} }
// print a horizontal rule option_print_hrule(headerLine, maxLen, '-');
printf(" |");
for(int i = 0; i < maxLen + 2; ++i)
putc('-', stdout);
printf("|\n");
stringlist_free(&lines); stringlist_free(&lines);