Argx 1.0.2-build
Simple argument parser made in C
 
Loading...
Searching...
No Matches
Argx.c File Reference
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "../inc/Argx.h"
#include "../inc/types.h"

Go to the source code of this file.

Data Structures

struct  Argxc
 

Functions

void argxcAddOption (Argxc *argxc, ArgxcOptions option)
 Add a new option to the Argxc instance.
 
void argxcAddSubOption (ArgxcOptions *parent, ArgxcOptions subOption)
 Add a sub-option to a parent option.
 
bool argxcCompareArgs (ArgxcOptions *options, size_t optionsCount, char **argv, size_t argvCount)
 Compare if the given argv matches a list of ArgxcOptions.
 
ArgxcargxcCreate (const char *id, int argc, char *argv[])
 Create a new Argxc instance with a specified ID and command-line arguments.
 
ArgxcargxcCreateDefault (void)
 Create a new Argxc instance with default values (empty ID and no arguments).
 
char * argxcCreateDocs (Argxc *argxc, ArgxcStyle style, const char *title, const char *mainInfo)
 Generate documentation for the defined options.
 
ArgxcError argxcCreateError (const char *type, const char *error, const char *help, int code)
 Create an ArgxcError object.
 
ArgxcOptions argxcCreateOption (const char *id, const char *param, const char *sparam, const char *info, bool hasSubParams, bool hasAnySubParams)
 Create a new option.
 
void argxcDestroy (Argxc *argxc)
 Destroy an Argxc instance and free all associated memory.
 
int argxcFindParam (Argxc *argxc, const char *id)
 Find the index of a parameter by ID.
 
void argxcFreeError (ArgxcError *error)
 Free resources associated with an ArgxcError struct.
 
void argxcFreeOption (ArgxcOptions *option)
 Free resources associated with an ArgxcOptions struct.
 
void argxcFreeParam (ArgxcParam *param)
 Free resources associated with an ArgxcParam struct.
 
void argxcFreeStringArray (char **array, size_t count)
 Free a string array returned from Argxc (e.g., argv array).
 
int argxcGetArgc (Argxc *argxc)
 Get the number of command-line arguments passed.
 
const char * argxcGetId (Argxc *argxc)
 Get the identifier of the Argxc instance.
 
char ** argxcGetMainArgs (Argxc *argxc, size_t *count)
 Get the raw command-line arguments passed to Argxc.
 
ArgxcOptionsargxcGetOptions (Argxc *argxc, size_t *count)
 Get all top-level options defined for the Argxc instance.
 
ArgxcParam argxcGetParam (Argxc *argxc, const char *id)
 Retrieve a parameter by ID.
 
bool argxcGetSubParam (Argxc *argxc, const ArgxcParam *param, const char *id)
 Retrieve a sub-parameter from a given parameter by ID.
 
bool argxcParamExists (Argxc *argxc, const char *id)
 Check if a parameter with the given ID exists.
 
static void freeOptionsArray (ArgxcOptions *options, size_t count)
 
static void freeStringArray (char **array, size_t count)
 
static char * stringDuplicate (const char *str)
 

Function Documentation

◆ argxcAddOption()

void argxcAddOption ( Argxc * argxc,
ArgxcOptions option )

Add a new option to the Argxc instance.

Parameters
argxcPointer to the Argxc instance.
optionThe option to add.

Definition at line 124 of file Argx.c.

125{
126 if (!argxc) return;
127
128 if (argxc->optionsCount >= argxc->optionsCapacity)
129 {
130 argxc->optionsCapacity *= 2;
131 argxc->options = realloc(argxc->options, argxc->optionsCapacity * sizeof(ArgxcOptions));
132 if (!argxc->options) return;
133 }
134
135 argxc->options[argxc->optionsCount++] = option;
136}
size_t optionsCapacity
Definition Argx.c:22
size_t optionsCount
Definition Argx.c:21
ArgxcOptions * options
Definition Argx.c:20

References Argxc::options, Argxc::optionsCapacity, and Argxc::optionsCount.

◆ argxcAddSubOption()

void argxcAddSubOption ( ArgxcOptions * parent,
ArgxcOptions subOption )

Add a sub-option to a parent option.

Parameters
parentPointer to the parent option.
subOptionSub-option to add under the parent.

Definition at line 138 of file Argx.c.

139{
140 if (!parent) return;
141
142 if (parent->subParamsCount >= parent->subParamsCapacity)
143 {
144 size_t newCapacity = parent->subParamsCapacity == 0 ? 2 : parent->subParamsCapacity * 2;
145 ArgxcOptions *newSubParams = realloc(parent->subParams, newCapacity * sizeof(ArgxcOptions));
146 if (!newSubParams) return;
147
148 parent->subParams = newSubParams;
149 parent->subParamsCapacity = newCapacity;
150 }
151
152 parent->subParams[parent->subParamsCount++] = subOption;
153}
size_t subParamsCount
Definition types.h:33
struct ArgxcOptions * subParams
Definition types.h:32
size_t subParamsCapacity
Definition types.h:34

References ArgxcOptions::subParams, ArgxcOptions::subParamsCapacity, and ArgxcOptions::subParamsCount.

◆ argxcCompareArgs()

bool argxcCompareArgs ( ArgxcOptions * options,
size_t optionsCount,
char ** argv,
size_t argvCount )

Compare if the given argv matches a list of ArgxcOptions.

Parameters
optionsArray of ArgxcOptions.
optionsCountNumber of options.
argvArray of argument strings.
argvCountNumber of argument strings.
Returns
true if argv matches the options, false otherwise.

Definition at line 499 of file Argx.c.

500{
501 if (!options || !argv) return false;
502
503 for (size_t i = 1; i < argvCount; i++)
504 {
505 const char *arg = argv[i];
506
507 bool found = false;
508 bool hasSubParams = false;
509 bool hasAnySubParams = false;
510 ArgxcOptions *matchedOption = NULL;
511
512 // Find the matching option
513 for (size_t j = 0; j < optionsCount; j++)
514 {
515 if ((options[j].sparam && strcmp(options[j].sparam, arg) == 0) ||
516 (options[j].param && strcmp(options[j].param, arg) == 0))
517 {
518 found = true;
519 hasSubParams = options[j].hasSubParams;
520 hasAnySubParams = options[j].hasAnySubParams;
521 matchedOption = &options[j];
522 break;
523 }
524 }
525
526 if (!found) return false;
527
528 if (hasSubParams || hasAnySubParams)
529 {
530 // Check if there's a next argument
531 if (i + 1 < argvCount)
532 {
533 const char *nextArg = argv[i + 1];
534
535 // Check if next argument is a sub-parameter
536 bool isSubParam = false;
537
538 if (nextArg && strlen(nextArg) > 0 && nextArg[0] != '-')
539 {
540 // Validate if it's a valid sub-parameter
541 for (size_t k = 0; k < matchedOption->subParamsCount; k++)
542 {
543 if ((matchedOption->subParams[k].param && strcmp(matchedOption->subParams[k].param, nextArg) == 0) ||
544 (matchedOption->subParams[k].sparam && strcmp(matchedOption->subParams[k].sparam, nextArg) == 0))
545 {
546 isSubParam = true;
547 break;
548 }
549 }
550
551 if (isSubParam)
552 {
553 i++; // Skip the sub-parameter
554 } else if (hasSubParams || hasAnySubParams)
555 {
556 return false;
557 }
558 } else if (hasSubParams || hasAnySubParams)
559 {
560 return false;
561 }
562 }
563 }
564 }
565
566 return true;
567}
char * param
Definition types.h:27
char * sparam
Definition types.h:28
bool hasAnySubParams
Definition types.h:31
bool hasSubParams
Definition types.h:30

References ArgxcOptions::hasAnySubParams, ArgxcOptions::hasSubParams, ArgxcOptions::param, ArgxcOptions::sparam, ArgxcOptions::subParams, and ArgxcOptions::subParamsCount.

◆ argxcCreate()

Argxc * argxcCreate ( const char * id,
int argc,
char * argv[] )

Create a new Argxc instance with a specified ID and command-line arguments.

Parameters
idIdentifier for the parser instance.
argcNumber of command-line arguments.
argvArray of command-line argument strings.
Returns
Argxc* Pointer to the created Argxc instance.

Definition at line 64 of file Argx.c.

65{
66 Argxc *argxc = malloc(sizeof(Argxc));
67 if (!argxc) return NULL;
68
69 argxc->id = stringDuplicate(id);
70 argxc->mainArgc = argc;
71 argxc->mainArgsCount = argc;
72 argxc->mainArgs = malloc(argc * sizeof(char*));
73 argxc->optionsCount = 0;
74 argxc->optionsCapacity = 10;
75 argxc->options = malloc(argxc->optionsCapacity * sizeof(ArgxcOptions));
76
77 if (!argxc->mainArgs || !argxc->options)
78 {
79 argxcDestroy(argxc);
80 return NULL;
81 }
82
83 for (int i = 0; i < argc; i++)
84 {
85 argxc->mainArgs[i] = stringDuplicate(argv[i]);
86 }
87
88 return argxc;
89}
static char * stringDuplicate(const char *str)
Definition Argx.c:26
void argxcDestroy(Argxc *argxc)
Destroy an Argxc instance and free all associated memory.
Definition Argx.c:113
Definition Argx.c:15
size_t mainArgsCount
Definition Argx.c:18
char ** mainArgs
Definition Argx.c:17
char * id
Definition Argx.c:16
unsigned int mainArgc
Definition Argx.c:19

References argxcDestroy(), Argxc::id, Argxc::mainArgc, Argxc::mainArgs, Argxc::mainArgsCount, Argxc::options, Argxc::optionsCapacity, Argxc::optionsCount, and stringDuplicate().

◆ argxcCreateDefault()

Argxc * argxcCreateDefault ( void )

Create a new Argxc instance with default values (empty ID and no arguments).

Returns
Argxc* Pointer to the default Argxc instance.

Definition at line 91 of file Argx.c.

92{
93 Argxc *argxc = malloc(sizeof(Argxc));
94 if (!argxc) return NULL;
95
96 argxc->id = NULL;
97 argxc->mainArgs = NULL;
98 argxc->mainArgsCount = 0;
99 argxc->mainArgc = 0;
100 argxc->optionsCount = 0;
101 argxc->optionsCapacity = 10;
102 argxc->options = malloc(argxc->optionsCapacity * sizeof(ArgxcOptions));
103
104 if (!argxc->options)
105 {
106 free(argxc); argxc = NULL;
107 return NULL;
108 }
109
110 return argxc;
111}

References Argxc::id, Argxc::mainArgc, Argxc::mainArgs, Argxc::mainArgsCount, Argxc::options, Argxc::optionsCapacity, and Argxc::optionsCount.

◆ argxcCreateDocs()

char * argxcCreateDocs ( Argxc * argxc,
ArgxcStyle style,
const char * title,
const char * mainInfo )

Generate documentation for the defined options.

Parameters
argxcPointer to the Argxc instance.
styleDocumentation output style.
titleTitle of the documentation.
mainInfoAdditional info to be displayed in the documentation.
Returns
char* Documentation string (must be freed by caller).

Definition at line 375 of file Argx.c.

376{
377 if (!argxc) return NULL;
378
379 size_t bufferSize = 4096;
380 char *contentStr = malloc(bufferSize);
381 if (!contentStr) return NULL;
382
383 contentStr[0] = '\0';
384
385 if (style == ARGX_STYLE_PROFESSIONAL)
386 {
387 for (size_t i = 0; i < argxc->optionsCount; i++)
388 {
389 ArgxcOptions *opt = &argxc->options[i];
390
391 // Main option header line
392 char temp[512];
393 snprintf(temp, sizeof(temp), "ID: %s\n", opt->id ? opt->id : "");
394 strcat(contentStr, temp);
395
396 snprintf(temp, sizeof(temp), "[ %s | %s",
397 opt->sparam ? opt->sparam : "",
398 opt->param ? opt->param : "");
399 strcat(contentStr, temp);
400
401 if (opt->hasSubParams && opt->subParamsCount > 0)
402 {
403 strcat(contentStr, " [ ");
404
405 for (size_t j = 0; j < opt->subParamsCount; j++)
406 {
407 strcat(contentStr, opt->subParams[j].param ? opt->subParams[j].param : "");
408
409 if (j < opt->subParamsCount - 1)
410 {
411 strcat(contentStr, " | ");
412 } else if (j <= opt->subParamsCount)
413 {
414 strcat(contentStr, " ");
415 }
416 }
417
418 strcat(contentStr, "] ] ");
419 } else {
420 strcat(contentStr, " ] ");
421 }
422
423 strcat(contentStr, opt->info ? opt->info : "");
424 strcat(contentStr, "\n");
425
426 // Print all sub-options
427 if (opt->hasSubParams && opt->subParamsCount > 0)
428 {
429 for (size_t j = 0; j < opt->subParamsCount; j++)
430 {
431 ArgxcOptions *sub = &opt->subParams[j];
432
433 // Create spacing for alignment
434 size_t paramLen = opt->param ? strlen(opt->param) : 0;
435 for (size_t k = 0; k < paramLen; k++)
436 {
437 strcat(contentStr, " ");
438 }
439
440 snprintf(temp, sizeof(temp), " [ %s | %s ] %s\n",
441 sub->sparam ? sub->sparam : "",
442 sub->param ? sub->param : "",
443 sub->info ? sub->info : "");
444 strcat(contentStr, temp);
445 }
446 }
447 }
448 } else if (style == ARGX_STYLE_SIMPLE)
449 {
450 for (size_t i = 0; i < argxc->optionsCount; i++)
451 {
452 ArgxcOptions *opt = &argxc->options[i];
453
454 char temp[256];
455 snprintf(temp, sizeof(temp), "%s, %s - %s\n",
456 opt->sparam ? opt->sparam : "",
457 opt->param ? opt->param : "",
458 opt->info ? opt->info : "");
459 strcat(contentStr, temp);
460
461 if (opt->hasSubParams && opt->subParamsCount > 0)
462 {
463 for (size_t j = 0; j < opt->subParamsCount; j++)
464 {
465 ArgxcOptions *sub = &opt->subParams[j];
466
467 snprintf(temp, sizeof(temp), " %s, %s - %s\n",
468 sub->sparam ? sub->sparam : "",
469 sub->param ? sub->param : "",
470 sub->info ? sub->info : "");
471 strcat(contentStr, temp);
472 }
473 }
474 }
475 }
476
477 // Combine title, mainInfo, and content
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; // Extra space for newlines
482
483 char *result = malloc(totalLen);
484 if (!result)
485 {
486 free(contentStr); contentStr = NULL;
487 return NULL;
488 }
489
490 snprintf(result, totalLen, "%s\n%s\n%s",
491 title ? title : "",
492 mainInfo ? mainInfo : "",
493 contentStr);
494
495 free(contentStr); contentStr = NULL;
496 return result;
497}
char * id
Definition types.h:26
char * info
Definition types.h:29
@ ARGX_STYLE_PROFESSIONAL
Definition types.h:11
@ ARGX_STYLE_SIMPLE
Definition types.h:12

References ARGX_STYLE_PROFESSIONAL, ARGX_STYLE_SIMPLE, ArgxcOptions::hasSubParams, ArgxcOptions::id, ArgxcOptions::info, Argxc::options, Argxc::optionsCount, ArgxcOptions::param, ArgxcOptions::sparam, ArgxcOptions::subParams, and ArgxcOptions::subParamsCount.

◆ argxcCreateError()

ArgxcError argxcCreateError ( const char * type,
const char * error,
const char * help,
int code )

Create an ArgxcError object.

Parameters
typeType/category of the error.
errorError message.
helpHelp message for resolving the error.
codeExit code or error code.
Returns
ArgxcError The created error struct.

Definition at line 667 of file Argx.c.

668{
669 ArgxcError err = {0};
670
671 err.type = stringDuplicate(type);
672 err.error = stringDuplicate(error);
673 err.help = stringDuplicate(help);
674 err.code = code;
675
676 return err;
677}
int code
Definition types.h:19
char * error
Definition types.h:17
char * help
Definition types.h:18
char * type
Definition types.h:16

References ArgxcError::code, ArgxcError::error, ArgxcError::help, stringDuplicate(), and ArgxcError::type.

◆ argxcCreateOption()

ArgxcOptions argxcCreateOption ( const char * id,
const char * param,
const char * sparam,
const char * info,
bool hasSubParams,
bool hasAnySubParams )

Create a new option.

Parameters
idOption identifier.
paramParameter name (e.g., –param).
sparamShort form (e.g., -p).
infoDescription of the option.
hasSubParamsWhether the option has sub-parameters.
hasAnySubParamsWhether the option accepts any sub-parameters.
Returns
ArgxcOptions The created option.

Definition at line 607 of file Argx.c.

609{
610 ArgxcOptions option = {0};
611
612 option.id = stringDuplicate(id);
613 option.param = stringDuplicate(param);
614 option.sparam = stringDuplicate(sparam);
615 option.info = stringDuplicate(info);
616 option.hasSubParams = hasSubParams;
617 option.hasAnySubParams = hasAnySubParams;
618 option.subParams = NULL;
619 option.subParamsCount = 0;
620
621 return option;
622}

References ArgxcOptions::hasAnySubParams, ArgxcOptions::hasSubParams, ArgxcOptions::id, ArgxcOptions::info, ArgxcOptions::param, ArgxcOptions::sparam, stringDuplicate(), ArgxcOptions::subParams, and ArgxcOptions::subParamsCount.

◆ argxcDestroy()

void argxcDestroy ( Argxc * argxc)

Destroy an Argxc instance and free all associated memory.

Parameters
argxcPointer to the Argxc instance to destroy.

Definition at line 113 of file Argx.c.

114{
115 if (!argxc) return;
116
117 free(argxc->id); argxc->id = NULL;
118 if (argxc->mainArgs) freeStringArray(argxc->mainArgs, argxc->mainArgsCount);
119 if (argxc->options) freeOptionsArray(argxc->options, argxc->optionsCount);
120 free(argxc); argxc = NULL;
121}
static void freeStringArray(char **array, size_t count)
Definition Argx.c:38
static void freeOptionsArray(ArgxcOptions *options, size_t count)
Definition Argx.c:50

References freeOptionsArray(), freeStringArray(), Argxc::id, Argxc::mainArgs, Argxc::mainArgsCount, Argxc::options, and Argxc::optionsCount.

Referenced by argxcCreate().

◆ argxcFindParam()

int argxcFindParam ( Argxc * argxc,
const char * id )

Find the index of a parameter by ID.

Parameters
argxcPointer to the Argxc instance.
idThe identifier of the parameter to find.
Returns
int Index of the parameter, or -1 if not found.

Definition at line 155 of file Argx.c.

156{
157 if (!argxc || !id) return -1;
158
159 // First check if it's a main parameter
160 for (size_t i = 0; i < argxc->optionsCount; i++)
161 {
162 if (argxc->options[i].id && strcmp(argxc->options[i].id, id) == 0)
163 {
164 // Check if this main parameter exists in arguments
165 for (size_t j = 0; j < argxc->mainArgsCount; j++)
166 {
167 if ((argxc->options[i].param && strcmp(argxc->mainArgs[j], argxc->options[i].param) == 0) ||
168 (argxc->options[i].sparam && strcmp(argxc->mainArgs[j], argxc->options[i].sparam) == 0))
169 {
170 return (int)i;
171 }
172 }
173 }
174 }
175
176 // Then look for sub-parameters
177 for (size_t i = 0; i < argxc->optionsCount; i++)
178 {
179 ArgxcOptions *opt = &argxc->options[i];
180
181 // Check if the parent option exists in the arguments
182 bool parentExists = false;
183 for (size_t j = 0; j < argxc->mainArgsCount; j++)
184 {
185 if ((opt->param && strcmp(argxc->mainArgs[j], opt->param) == 0) ||
186 (opt->sparam && strcmp(argxc->mainArgs[j], opt->sparam) == 0))
187 {
188 parentExists = true;
189 break;
190 }
191 }
192
193 if (parentExists)
194 {
195 // Find the index of the requested sub-parameter
196 for (size_t j = 0; j < opt->subParamsCount; j++)
197 {
198 if (opt->subParams[j].id && strcmp(opt->subParams[j].id, id) == 0)
199 {
200 return (int)j;
201 }
202 }
203 }
204 }
205
206 return -1; // Not found
207}

References ArgxcOptions::id, Argxc::mainArgs, Argxc::mainArgsCount, Argxc::options, Argxc::optionsCount, ArgxcOptions::param, ArgxcOptions::sparam, ArgxcOptions::subParams, and ArgxcOptions::subParamsCount.

Referenced by argxcGetSubParam(), and argxcParamExists().

◆ argxcFreeError()

void argxcFreeError ( ArgxcError * error)

Free resources associated with an ArgxcError struct.

Parameters
errorPointer to the error to free.

Definition at line 679 of file Argx.c.

680{
681 if (!error) return;
682
683 free(error->type); error->type = NULL;
684 free(error->error); error->error = NULL;
685 free(error->help); error->help = NULL;
686 memset(error, 0, sizeof(ArgxcError));
687}

References ArgxcError::error, ArgxcError::help, and ArgxcError::type.

◆ argxcFreeOption()

void argxcFreeOption ( ArgxcOptions * option)

Free resources associated with an ArgxcOptions struct.

Parameters
optionPointer to the option to free.

Definition at line 624 of file Argx.c.

625{
626 if (!option) return;
627
628 if (option->id)
629 { free(option->id); option->id = NULL; }
630 if (option->param)
631 { free(option->param); option->param = NULL; }
632 if (option->sparam)
633 { free(option->sparam); option->sparam = NULL; }
634 if (option->info)
635 { free(option->info); option->info = NULL; }
636
637 if (option->subParams)
638 {
639 for (size_t i = 0; i < option->subParamsCount; i++)
640 {
641 argxcFreeOption(&option->subParams[i]);
642 }
643
644 free(option->subParams);
645 option->subParams = NULL;
646 option->subParamsCapacity = 0;
647 option->subParamsCount = 0;
648 }
649
650 memset(option, 0, sizeof(ArgxcOptions));
651}
void argxcFreeOption(ArgxcOptions *option)
Free resources associated with an ArgxcOptions struct.
Definition Argx.c:624

References argxcFreeOption(), ArgxcOptions::id, ArgxcOptions::info, ArgxcOptions::param, ArgxcOptions::sparam, ArgxcOptions::subParams, ArgxcOptions::subParamsCapacity, and ArgxcOptions::subParamsCount.

Referenced by argxcFreeOption(), and freeOptionsArray().

◆ argxcFreeParam()

void argxcFreeParam ( ArgxcParam * param)

Free resources associated with an ArgxcParam struct.

Parameters
paramPointer to the parameter to free.

Definition at line 653 of file Argx.c.

654{
655 if (!param) return;
656
657 free(param->subExists); param->subExists = NULL;
658 memset(param, 0, sizeof(ArgxcParam));
659}
bool * subExists
Definition types.h:39

References ArgxcParam::subExists.

◆ argxcFreeStringArray()

void argxcFreeStringArray ( char ** array,
size_t count )

Free a string array returned from Argxc (e.g., argv array).

Parameters
arrayThe array to free.
countNumber of elements in the array.

Definition at line 661 of file Argx.c.

662{
663 freeStringArray(array, count);
664}

References freeStringArray().

◆ argxcGetArgc()

int argxcGetArgc ( Argxc * argxc)

Get the number of command-line arguments passed.

Parameters
argxcPointer to the Argxc instance.
Returns
int Number of arguments.

Definition at line 588 of file Argx.c.

589{
590 return argxc ? argxc->mainArgc : 0;
591}

References Argxc::mainArgc.

◆ argxcGetId()

const char * argxcGetId ( Argxc * argxc)

Get the identifier of the Argxc instance.

Parameters
argxcPointer to the Argxc instance.
Returns
const char* The ID string.

Definition at line 601 of file Argx.c.

602{
603 return argxc ? argxc->id : NULL;
604}

References Argxc::id.

◆ argxcGetMainArgs()

char ** argxcGetMainArgs ( Argxc * argxc,
size_t * count )

Get the raw command-line arguments passed to Argxc.

Parameters
argxcPointer to the Argxc instance.
countOutput: number of arguments returned.
Returns
char** Array of argument strings.

Definition at line 570 of file Argx.c.

571{
572 if (!argxc || !count) return NULL;
573
574 *count = argxc->mainArgsCount;
575
576 // Create a copy of the array
577 char **copy = malloc(argxc->mainArgsCount * sizeof(char*));
578 if (!copy) return NULL;
579
580 for (size_t i = 0; i < argxc->mainArgsCount; i++)
581 {
582 copy[i] = stringDuplicate(argxc->mainArgs[i]);
583 }
584
585 return copy;
586}

References Argxc::mainArgs, Argxc::mainArgsCount, and stringDuplicate().

◆ argxcGetOptions()

ArgxcOptions * argxcGetOptions ( Argxc * argxc,
size_t * count )

Get all top-level options defined for the Argxc instance.

Parameters
argxcPointer to the Argxc instance.
countOutput: number of options returned.
Returns
ArgxcOptions* Array of options.

Definition at line 593 of file Argx.c.

594{
595 if (!argxc || !count) return NULL;
596
597 *count = argxc->optionsCount;
598 return argxc->options; // Return direct reference (be careful with modification)
599}

References Argxc::options, and Argxc::optionsCount.

◆ argxcGetParam()

ArgxcParam argxcGetParam ( Argxc * argxc,
const char * id )

Retrieve a parameter by ID.

Parameters
argxcPointer to the Argxc instance.
idThe identifier of the parameter to retrieve.
Returns
ArgxcParam The parameter object.

Definition at line 214 of file Argx.c.

215{
216 ArgxcParam result = {false, NULL, 0};
217
218 if (!argxc || !id || argxc->mainArgc <= 1)
219 {
220 return result;
221 }
222
223 // First, check if this is a top-level option
224 for (size_t i = 0; i < argxc->optionsCount; i++)
225 {
226 ArgxcOptions *opt = &argxc->options[i];
227
228 if (opt->id && strcmp(opt->id, id) == 0)
229 {
230 // Find the position of the main option in arguments
231 int mainOptionPos = -1;
232
233 for (size_t j = 0; j < argxc->mainArgsCount; j++)
234 {
235 if ((opt->param && strcmp(argxc->mainArgs[j], opt->param) == 0) ||
236 (opt->sparam && strcmp(argxc->mainArgs[j], opt->sparam) == 0))
237 {
238 result.exists = true;
239 mainOptionPos = j;
240 break;
241 }
242 }
243
244 if (result.exists)
245 {
246 if (opt->hasSubParams || opt->hasAnySubParams)
247 {
248 // Allocate memory for sub-parameter existence array
249 result.subExistsCount = opt->subParamsCount;
250 result.subExists = malloc(result.subExistsCount * sizeof(bool));
251
252 if (result.subExists)
253 {
254 // Check each sub-parameter
255 for (size_t j = 0; j < opt->subParamsCount; j++)
256 {
257 bool subMatched = false;
258 ArgxcOptions *sub = &opt->subParams[j];
259
260 // Look for sub-parameters after the main option
261 for (size_t k = mainOptionPos + 1; k < argxc->mainArgsCount; k++)
262 {
263 if ((sub->param && strcmp(argxc->mainArgs[k], sub->param) == 0) ||
264 (sub->sparam && strcmp(argxc->mainArgs[k], sub->sparam) == 0))
265 {
266 subMatched = true;
267 break;
268 }
269 }
270
271 result.subExists[j] = subMatched;
272 }
273 }
274 }
275
276 return result;
277 }
278 }
279 }
280
281 // If not found as top-level, check if it's a sub-parameter
282 for (size_t i = 0; i < argxc->optionsCount; i++)
283 {
284 ArgxcOptions *opt = &argxc->options[i];
285
286 // Find if the parent option exists and get its position
287 int parentPos = -1;
288
289 for (size_t j = 0; j < argxc->mainArgsCount; j++)
290 {
291 if ((opt->param && strcmp(argxc->mainArgs[j], opt->param) == 0) ||
292 (opt->sparam && strcmp(argxc->mainArgs[j], opt->sparam) == 0))
293 {
294 parentPos = j;
295 break;
296 }
297 }
298
299 if (parentPos >= 0 && (opt->hasSubParams || opt->hasAnySubParams))
300 {
301 // Check if the requested sub-parameter exists after the parent
302 for (size_t j = 0; j < opt->subParamsCount; j++)
303 {
304 ArgxcOptions *sub = &opt->subParams[j];
305
306 if (sub->id && strcmp(sub->id, id) == 0)
307 {
308 for (size_t k = parentPos + 1; k < argxc->mainArgsCount; k++)
309 {
310 if ((sub->param && strcmp(argxc->mainArgs[k], sub->param) == 0) ||
311 (sub->sparam && strcmp(argxc->mainArgs[k], sub->sparam) == 0))
312 {
313 result.exists = true;
314 break;
315 }
316 }
317
318 if (!result.exists && (size_t)(parentPos + 1) < argxc->mainArgsCount)
319 {
320 char *nextArg = argxc->mainArgs[parentPos + 1];
321
322 if ((sub->param && strcmp(nextArg, sub->param) == 0) ||
323 (sub->sparam && strcmp(nextArg, sub->sparam) == 0))
324 {
325 result.exists = true;
326 }
327 }
328
329 // Handle any sub-sub-parameters if they exist
330 if (result.exists && (sub->hasSubParams || sub->hasAnySubParams))
331 {
332 result.subExistsCount = sub->subParamsCount;
333 result.subExists = malloc(result.subExistsCount * sizeof(bool));
334
335 if (result.subExists)
336 {
337 for (size_t k = 0; k < sub->subParamsCount; k++)
338 {
339 bool subsubMatched = false;
340 ArgxcOptions *subsub = &sub->subParams[k];
341
342 for (size_t l = 0; l < argxc->mainArgsCount; l++)
343 {
344 if ((subsub->param && strcmp(argxc->mainArgs[l], subsub->param) == 0) ||
345 (subsub->sparam && strcmp(argxc->mainArgs[l], subsub->sparam) == 0))
346 {
347 subsubMatched = true;
348 break;
349 }
350 }
351
352 result.subExists[k] = subsubMatched;
353 }
354 }
355 }
356
357 return result;
358 }
359 }
360 }
361 }
362
363 return result;
364}
size_t subExistsCount
Definition types.h:40
bool exists
Definition types.h:38

References ArgxcParam::exists, ArgxcOptions::hasAnySubParams, ArgxcOptions::hasSubParams, ArgxcOptions::id, Argxc::mainArgc, Argxc::mainArgs, Argxc::mainArgsCount, Argxc::options, Argxc::optionsCount, ArgxcOptions::param, ArgxcOptions::sparam, ArgxcParam::subExists, ArgxcParam::subExistsCount, ArgxcOptions::subParams, and ArgxcOptions::subParamsCount.

◆ argxcGetSubParam()

bool argxcGetSubParam ( Argxc * argxc,
const ArgxcParam * param,
const char * id )

Retrieve a sub-parameter from a given parameter by ID.

Parameters
argxcPointer to the Argxc instance.
paramPointer to the parent parameter.
idThe identifier of the sub-parameter.
Returns
true if the sub-parameter exists, false otherwise.

Definition at line 366 of file Argx.c.

367{
368 if (!argxc || !param || !id) return false;
369
370 int index = argxcFindParam(argxc, id);
371 return argxcParamExists(argxc, id) && param->subExists && index >= 0 &&
372 (size_t)index < param->subExistsCount && param->subExists[index];
373}
bool argxcParamExists(Argxc *argxc, const char *id)
Check if a parameter with the given ID exists.
Definition Argx.c:209
int argxcFindParam(Argxc *argxc, const char *id)
Find the index of a parameter by ID.
Definition Argx.c:155

References argxcFindParam(), argxcParamExists(), and ArgxcParam::subExists.

◆ argxcParamExists()

bool argxcParamExists ( Argxc * argxc,
const char * id )

Check if a parameter with the given ID exists.

Parameters
argxcPointer to the Argxc instance.
idThe identifier of the parameter.
Returns
true if the parameter exists, false otherwise.

Definition at line 209 of file Argx.c.

210{
211 return argxcFindParam(argxc, id) >= 0;
212}

References argxcFindParam().

Referenced by argxcGetSubParam().

◆ freeOptionsArray()

static void freeOptionsArray ( ArgxcOptions * options,
size_t count )
static

Definition at line 50 of file Argx.c.

51{
52 if (!options) return;
53
54 for (size_t i = 0; i < count; i++)
55 {
56 if (&options[i]) argxcFreeOption(&options[i]);
57 }
58
59 if (options)
60 { free(options); options = NULL; }
61}

References argxcFreeOption().

Referenced by argxcDestroy().

◆ freeStringArray()

static void freeStringArray ( char ** array,
size_t count )
static

Definition at line 38 of file Argx.c.

39{
40 if (!array) return;
41 for (size_t i = 0; i < count; i++)
42 {
43 if (array[i]) free(array[i]);
44 }
45
46 if (array)
47 { free(array); array = NULL; }
48}

Referenced by argxcDestroy(), and argxcFreeStringArray().

◆ stringDuplicate()

static char * stringDuplicate ( const char * str)
static

Definition at line 26 of file Argx.c.

27{
28 if (!str) return NULL;
29 size_t len = strlen(str) + 1;
30 char *dup = malloc(len);
31 if (dup)
32 {
33 memcpy(dup, str, len);
34 }
35 return dup;
36}

Referenced by argxcCreate(), argxcCreateError(), argxcCreateOption(), and argxcGetMainArgs().