28 if (!str)
return NULL;
29 size_t len = strlen(str) + 1;
30 char *dup = malloc(len);
33 memcpy(dup, str, len);
41 for (
size_t i = 0; i < count; i++)
43 if (array[i]) free(array[i]);
47 { free(array); array = NULL; }
54 for (
size_t i = 0; i < count; i++)
60 { free(options); options = NULL; }
67 if (!argxc)
return NULL;
72 argxc->
mainArgs = malloc(argc *
sizeof(
char*));
83 for (
int i = 0; i < argc; i++)
94 if (!argxc)
return NULL;
106 free(argxc); argxc = NULL;
117 free(argxc->
id); argxc->
id = NULL;
120 free(argxc); argxc = NULL;
146 if (!newSubParams)
return;
157 if (!argxc || !
id)
return -1;
182 bool parentExists =
false;
218 if (!argxc || !
id || argxc->
mainArgc <= 1)
228 if (opt->
id && strcmp(opt->
id,
id) == 0)
231 int mainOptionPos = -1;
257 bool subMatched =
false;
261 for (
size_t k = mainOptionPos + 1; k < argxc->
mainArgsCount; k++)
306 if (sub->
id && strcmp(sub->
id,
id) == 0)
308 for (
size_t k = parentPos + 1; k < argxc->
mainArgsCount; k++)
320 char *nextArg = argxc->
mainArgs[parentPos + 1];
322 if ((sub->
param && strcmp(nextArg, sub->
param) == 0) ||
339 bool subsubMatched =
false;
347 subsubMatched =
true;
368 if (!argxc || !param || !
id)
return false;
372 (size_t)index < param->subExistsCount && param->
subExists[index];
377 if (!argxc)
return NULL;
379 size_t bufferSize = 4096;
380 char *contentStr = malloc(bufferSize);
381 if (!contentStr)
return NULL;
383 contentStr[0] =
'\0';
393 snprintf(temp,
sizeof(temp),
"ID: %s\n", opt->
id ? opt->
id :
"");
394 strcat(contentStr, temp);
396 snprintf(temp,
sizeof(temp),
"[ %s | %s",
399 strcat(contentStr, temp);
403 strcat(contentStr,
" [ ");
409 if (j < opt->subParamsCount - 1)
411 strcat(contentStr,
" | ");
412 }
else if (j <= opt->subParamsCount)
414 strcat(contentStr,
" ");
418 strcat(contentStr,
"] ] ");
420 strcat(contentStr,
" ] ");
423 strcat(contentStr, opt->
info ? opt->
info :
"");
424 strcat(contentStr,
"\n");
434 size_t paramLen = opt->
param ? strlen(opt->
param) : 0;
435 for (
size_t k = 0; k < paramLen; k++)
437 strcat(contentStr,
" ");
440 snprintf(temp,
sizeof(temp),
" [ %s | %s ] %s\n",
444 strcat(contentStr, temp);
455 snprintf(temp,
sizeof(temp),
"%s, %s - %s\n",
459 strcat(contentStr, temp);
467 snprintf(temp,
sizeof(temp),
" %s, %s - %s\n",
471 strcat(contentStr, temp);
478 size_t titleLen = title ? strlen(title) : 0;
479 size_t mainInfoLen = mainInfo ? strlen(mainInfo) : 0;
480 size_t contentLen = strlen(contentStr);
481 size_t totalLen = titleLen + mainInfoLen + contentLen + 10;
483 char *result = malloc(totalLen);
486 free(contentStr); contentStr = NULL;
490 snprintf(result, totalLen,
"%s\n%s\n%s",
492 mainInfo ? mainInfo :
"",
495 free(contentStr); contentStr = NULL;
501 if (!options || !argv)
return false;
503 for (
size_t i = 1; i < argvCount; i++)
505 const char *arg = argv[i];
508 bool hasSubParams =
false;
509 bool hasAnySubParams =
false;
513 for (
size_t j = 0; j < optionsCount; j++)
515 if ((options[j].sparam && strcmp(options[j].sparam, arg) == 0) ||
516 (options[j].param && strcmp(options[j].param, arg) == 0))
521 matchedOption = &options[j];
526 if (!found)
return false;
528 if (hasSubParams || hasAnySubParams)
531 if (i + 1 < argvCount)
533 const char *nextArg = argv[i + 1];
536 bool isSubParam =
false;
538 if (nextArg && strlen(nextArg) > 0 && nextArg[0] !=
'-')
554 }
else if (hasSubParams || hasAnySubParams)
558 }
else if (hasSubParams || hasAnySubParams)
572 if (!argxc || !count)
return NULL;
578 if (!copy)
return NULL;
595 if (!argxc || !count)
return NULL;
603 return argxc ? argxc->
id : NULL;
608 const char *info,
bool hasSubParams,
bool hasAnySubParams)
629 { free(option->
id); option->
id = NULL; }
631 { free(option->
param); option->
param = NULL; }
635 { free(option->
info); option->
info = NULL; }
683 free(error->
type); error->
type = NULL;
685 free(error->
help); error->
help = NULL;
bool argxcParamExists(Argxc *argxc, const char *id)
Check if a parameter with the given ID exists.
Argxc * argxcCreate(const char *id, int argc, char *argv[])
Create a new Argxc instance with a specified ID and command-line arguments.
bool argxcGetSubParam(Argxc *argxc, const ArgxcParam *param, const char *id)
Retrieve a sub-parameter from a given parameter by ID.
int argxcGetArgc(Argxc *argxc)
Get the number of command-line arguments passed.
const char * argxcGetId(Argxc *argxc)
Get the identifier of the Argxc instance.
bool argxcCompareArgs(ArgxcOptions *options, size_t optionsCount, char **argv, size_t argvCount)
Compare if the given argv matches a list of ArgxcOptions.
static void freeStringArray(char **array, size_t count)
void argxcFreeStringArray(char **array, size_t count)
Free a string array returned from Argxc (e.g., argv array).
int argxcFindParam(Argxc *argxc, const char *id)
Find the index of a parameter by ID.
void argxcAddOption(Argxc *argxc, ArgxcOptions option)
Add a new option to the Argxc instance.
ArgxcOptions argxcCreateOption(const char *id, const char *param, const char *sparam, const char *info, bool hasSubParams, bool hasAnySubParams)
Create a new option.
char ** argxcGetMainArgs(Argxc *argxc, size_t *count)
Get the raw command-line arguments passed to Argxc.
void argxcFreeParam(ArgxcParam *param)
Free resources associated with an ArgxcParam struct.
void argxcAddSubOption(ArgxcOptions *parent, ArgxcOptions subOption)
Add a sub-option to a parent option.
ArgxcParam argxcGetParam(Argxc *argxc, const char *id)
Retrieve a parameter by ID.
Argxc * argxcCreateDefault(void)
Create a new Argxc instance with default values (empty ID and no arguments).
static char * stringDuplicate(const char *str)
void argxcDestroy(Argxc *argxc)
Destroy an Argxc instance and free all associated memory.
static void freeOptionsArray(ArgxcOptions *options, size_t count)
char * argxcCreateDocs(Argxc *argxc, ArgxcStyle style, const char *title, const char *mainInfo)
Generate documentation for the defined options.
void argxcFreeOption(ArgxcOptions *option)
Free resources associated with an ArgxcOptions struct.
void argxcFreeError(ArgxcError *error)
Free resources associated with an ArgxcError struct.
ArgxcOptions * argxcGetOptions(Argxc *argxc, size_t *count)
Get all top-level options defined for the Argxc instance.
ArgxcError argxcCreateError(const char *type, const char *error, const char *help, int code)
Create an ArgxcError object.
struct ArgxcOptions * subParams
@ ARGX_STYLE_PROFESSIONAL