diff --git a/src/Main.c b/src/Main.c index 1b78a24..770a2a6 100644 --- a/src/Main.c +++ b/src/Main.c @@ -27,6 +27,27 @@ ParseeUptime(void) return UtilTsMillis() - start; } +static const Argument arguments[] = +{ +#define Argument(c, req, vdesc, desc) \ + { \ + .end = false, \ + .argument = c, .value_req = req, \ + .value_descr = vdesc, \ + .description = desc \ + }, + + Argument('H', true, "number", "Sets the number of HTTP threads") + Argument('J', true, "number", "Sets the number of XMPP threads") + + Argument('g',false,NULL,"Generates a parsee.yaml AS file before exiting") + Argument('v',false,NULL,"Forces Parsee to print in a more verbose fashion") + Argument('h',false,NULL,"Generates an help screen(this one!)") + + { .end = true } +#undef Argument +}; + int Main(Array *args, HashMap *env) { @@ -51,6 +72,7 @@ Main(Array *args, HashMap *env) { ArgParseState state; + char *opts = ParseeGenerateGetopt(arguments); int flag; int xmpp = 8; int http = 8; @@ -58,10 +80,14 @@ Main(Array *args, HashMap *env) ArgParseStateInit(&state); /* TODO: Have a smarter way of generating the arg table * (with a list of structs, with a description and everything) */ - while ((flag = ArgParse(&state, args, "vgH:J:")) != -1) + while ((flag = ArgParse(&state, args, opts)) != -1) { switch (flag) { + case 'h': + ParseeGenerateHelp(arguments); + Free(opts); + goto end; case 'H': http = strtol(state.optArg, NULL, 10); break; @@ -74,15 +100,18 @@ Main(Array *args, HashMap *env) yaml = StreamOpen("parsee.yaml", "w"); ParseeExportConfigYAML(yaml); StreamClose(yaml); + Free(opts); goto end; case 'v': LogConfigLevelSet(LogConfigGlobal(), LOG_DEBUG); break; case '?': Log(LOG_ERR, "INVALID ARGUMENT GIVEN"); + Free(opts); goto end; } } + Free(opts); ParseeSetThreads(xmpp, http); } diff --git a/src/Parsee/Data.c b/src/Parsee/Data.c index d13499d..9b891ee 100644 --- a/src/Parsee/Data.c +++ b/src/Parsee/Data.c @@ -875,3 +875,52 @@ ParseeGenerateMTO(char *common_id) return matrix_to; } + + +void +ParseeGenerateHelp(const Argument *list) +{ + if (!list) + { + return; + } + + while (!list->end) + { + char *str = list->value_req ? + StrConcat(3, " [", list->value_descr, "]") : + StrDuplicate(""); + Log(LOG_INFO, "-%c%s", list->argument, str); + LogConfigIndent(LogConfigGlobal()); + Log(LOG_INFO, "%s", list->description); + LogConfigUnindent(LogConfigGlobal()); + list++; + + Free(str); + } + return; +} +char * +ParseeGenerateGetopt(const Argument *list) +{ + char *ret = NULL, *tmp = NULL; + if (!list) + { + return NULL; + } + + while (!list->end) + { + char buffer[3] = { + list->argument, list->value_req ? ':' : '\0', + '\0' + }; + + tmp = ret; + ret = StrConcat(2, ret, buffer); + Free(tmp); + + list++; + } + return ret; +} diff --git a/src/include/Parsee.h b/src/include/Parsee.h index 4a0903e..030e748 100644 --- a/src/include/Parsee.h +++ b/src/include/Parsee.h @@ -58,6 +58,16 @@ typedef struct ParseeData { char *id; } ParseeData; +typedef struct Argument { + bool end; + + char argument; + bool value_req; + + const char *value_descr; + const char *description; +} Argument; + #define GrabString(obj, ...) JsonValueAsString(JsonGet(obj, __VA_ARGS__)) #define GrabInteger(obj, ...) JsonValueAsInteger(JsonGet(obj, __VA_ARGS__)) #define GrabBoolean(obj, ...) JsonValueAsBoolean(JsonGet(obj, __VA_ARGS__)) @@ -76,6 +86,19 @@ typedef struct ParseeData { #define MB * 1024 KB #define GB * 1024 MB +/** Generates a valid, getopt-style argument list from a end-terminated + * argument list. + * ------------ + * Returns: a valid string to be used in getopt and the likes[HEAP] + * Modifies: NOTHING */ +extern char * ParseeGenerateGetopt(const Argument *list); + +/** Generates a help screen to the logger. + * ------------ + * Returns: NOTHING + * Modifies: NOTHING */ +extern void ParseeGenerateHelp(const Argument *list); + /* Initialises a Parsee config from scratch, and writes to it * as JSON in the CWD. */ extern void ParseeConfigInit(void);