added support for --flags

This commit is contained in:
AUnicornWithNoLife 2023-07-29 13:23:01 +01:00
parent 7266746727
commit cb953cc3f2
3 changed files with 51 additions and 14 deletions

View File

@ -14,11 +14,22 @@ carg_parse_data* carg_parse(int argc, char** argv)
char **lv_values = (char**)malloc(sizeof(char*) * argc); char **lv_values = (char**)malloc(sizeof(char*) * argc);
uint32_t lv_cp = 0; uint32_t lv_cp = 0;
char **flags = (char**)malloc(sizeof(char*) * argc);
uint32_t flags_cp = 0;
data->exec_name = argv[0]; data->exec_name = argv[0];
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
if (argv[i][0] == '-') // label if (argv[i][0] == '-') // label / flag
{
if (argv[i][1] != '\0' && argv[i][1] == '-') // flag
{
flags[flags_cp] = argv[i] + 2;
flags_cp++;
}
else // label
{ {
lv_labels[lv_cp] = argv[i] + 1; lv_labels[lv_cp] = argv[i] + 1;
@ -35,6 +46,7 @@ carg_parse_data* carg_parse(int argc, char** argv)
lv_cp++; lv_cp++;
} }
}
else // not labelled else // not labelled
{ {
values[values_cp] = argv[i]; values[values_cp] = argv[i];
@ -65,6 +77,16 @@ carg_parse_data* carg_parse(int argc, char** argv)
data->lv_values = NULL; data->lv_values = NULL;
} }
data->flags_len = flags_cp;
if (flags_cp > 0)
{
data->flags = realloc(flags, sizeof(char*) * flags_cp);
}
else
{
data->flags = NULL;
}
return data; return data;
} }
@ -84,5 +106,10 @@ void carg_parse_free(carg_parse_data* data)
free(data->lv_values); free(data->lv_values);
} }
if (data->flags != NULL)
{
free(data->flags);
}
free(data); free(data);
} }

View File

@ -15,6 +15,9 @@ typedef struct
uint32_t lv_len; uint32_t lv_len;
char** lv_labels; char** lv_labels;
char** lv_values; char** lv_values;
uint32_t flags_len;
char** flags;
} carg_parse_data; } carg_parse_data;
carg_parse_data* carg_parse(int argc, char** argv); carg_parse_data* carg_parse(int argc, char** argv);

7
test.c
View File

@ -30,6 +30,13 @@ int main(int argc, char** argv)
} }
} }
printf("\nFLAGS:\n");
for (uint32_t i = 0; i < data->flags_len; i++)
{
printf("%d :-: %s\n", i, data->flags[i]);
}
carg_parse_free(data); carg_parse_free(data);
return 0; return 0;