From 45ca5c416d3ffb8343aa4de8f2fa18dbf55ec192 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Tue, 29 Sep 2020 20:21:34 +1000 Subject: [PATCH] Update cliparser.c Adjusted to meet RRG format style and color. --- client/deps/cliparser/cliparser.c | 63 ++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/client/deps/cliparser/cliparser.c b/client/deps/cliparser/cliparser.c index 6a4c36faa..10055a420 100644 --- a/client/deps/cliparser/cliparser.c +++ b/client/deps/cliparser/cliparser.c @@ -11,10 +11,25 @@ #include "cliparser.h" #include #include +#include // Get color constants +#include // get PrintAndLogEx + #ifndef ARRAYLEN # define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0])) #endif +// Custom Colors +// To default the color return s +#define _SectionTagColor_(s) _GREEN_(s) +#define _ExampleColor_(s) _YELLOW_(s) +#define _CommandColor_(s) _RED_(s) +#define _DescriptionColor_(s) _CYAN_(s) +#define _ArgColor_(s) _WHITE_(s) +#define _ArgHelpColor_(s) _WHITE_(s) +// End Custom Colors +// Option width set to 30 to allow option descriptions to align. approx line 74 +// Example width set to 50 to allow help descriptions to align. approx line 93 + int CLIParserInit(CLIParserContext **ctx, const char *vprogramName, const char *vprogramHint, const char *vprogramHelp) { *ctx = malloc(sizeof(CLIParserContext)); if (!*ctx) { @@ -40,7 +55,7 @@ int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargta /* verify the argtable[] entries were allocated sucessfully */ if (arg_nullcheck(ctx->argtable) != 0) { /* NULL entries were detected, some allocations must have failed */ - printf("ERROR: Insufficient memory\n"); + PrintAndLogEx(ERR,"ERROR: Insufficient memory\n"); fflush(stdout); return 2; } @@ -49,14 +64,43 @@ int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargta /* special case: '--help' takes precedence over error reporting */ if ((argc < 2 && !allowEmptyExec) || ((struct arg_lit *)(ctx->argtable)[0])->count > 0) { // help must be the first record - printf("Usage: %s", ctx->programName); - arg_print_syntaxv(stdout, ctx->argtable, "\n"); if (ctx->programHint) - printf("%s\n\n", ctx->programHint); - arg_print_glossary(stdout, ctx->argtable, " %-20s %s\n"); - printf("\n"); - if (ctx->programHelp) - printf("%s \n", ctx->programHelp); + PrintAndLogEx(NORMAL,"\n"_DescriptionColor_("%s"), ctx->programHint); + + PrintAndLogEx(NORMAL,"\n"_SectionTagColor_("usage:")); + PrintAndLogEx (NORMAL," "_CommandColor_("%s")NOLF, ctx->programName); + arg_print_syntaxv(stdout, ctx->argtable, "\n\n"); + + PrintAndLogEx(NORMAL,_SectionTagColor_("options:")); + + arg_print_glossary(stdout, ctx->argtable, " "_ArgColor_("%-30s")" "_ArgHelpColor_("%s")"\n"); + + PrintAndLogEx(NORMAL,""); + if (ctx->programHelp) { + PrintAndLogEx(NORMAL,_SectionTagColor_("examples:")); + char *buf = NULL; + int idx = 0; + buf = realloc (buf,strlen (ctx->programHelp)+1); // more then enough as we are splitting + + char *p2; // pointer to split example from comment. + for (int i = 0; i < strlen (ctx->programHelp); i++) { + buf[idx++] = ctx->programHelp[i]; + if ((ctx->programHelp[i] == '\n') || (ctx->programHelp[i] == 0x00)) { + buf[idx-1] = 0x00; + p2 = strstr(buf,"->"); // See if the example has a comment. + if (p2 != NULL) { + *(p2-1) = 0x00; + PrintAndLogEx(NORMAL," "_ExampleColor_("%-50s")" %s",buf,p2); + } else { + PrintAndLogEx(NORMAL," "_ExampleColor_("%-50s"),buf); + } + idx = 0; + } + } + + PrintAndLogEx(NORMAL,""); + free (buf); + } fflush(stdout); return 1; @@ -66,7 +110,7 @@ int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargta if (nerrors > 0) { /* Display the error details contained in the arg_end struct.*/ arg_print_errors(stdout, ((struct arg_end *)(ctx->argtable)[vargtableLen - 1]), ctx->programName); - printf("Try '%s --help' for more information.\n", ctx->programName); + PrintAndLogEx(WARNING,"Try '%s --help' for more information.\n", ctx->programName); fflush(stdout); return 3; } @@ -74,6 +118,7 @@ int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargta return 0; } + enum ParserState { PS_FIRST, PS_ARGUMENT,