From 4d54bafdc40601dde921ab16ef8a2564b58b5093 Mon Sep 17 00:00:00 2001 From: Nganga02 Date: Sat, 28 Mar 2026 12:19:08 +0300 Subject: [PATCH] chore: refactored the set function --- src/cli/cmd_line.c | 177 +++++++++++++++++++++++---------------------- src/cli/cmd_line.h | 2 + 2 files changed, 93 insertions(+), 86 deletions(-) diff --git a/src/cli/cmd_line.c b/src/cli/cmd_line.c index 4c7f451..47812f4 100644 --- a/src/cli/cmd_line.c +++ b/src/cli/cmd_line.c @@ -1,64 +1,61 @@ #include "cmd_line.h" - -bool cmd_line_argument_new(struct cmd_line_argument *self,enum cmd_line_argument_type type,char *short_name,char *long_name,char *description) +bool cmd_line_argument_new(struct cmd_line_argument *self, enum cmd_line_argument_type type, char *short_name, char *long_name, char *description) { - if(ACHIOR_LABS_NULL(self)) + if (ACHIOR_LABS_NULL(self)) { return false; } - self->type = type; - self->short_name = short_name; - self->long_name = long_name; - self->description = description; - self->is_active = false; + self->type = type; + self->short_name = short_name; + self->long_name = long_name; + self->description = description; + self->is_active = false; return true; } - -bool cmd_line_argument_parser_new(struct cmd_line_argument_parser *self,char *name,char *description,char *version,struct bump_allocator *bump) +bool cmd_line_argument_parser_new(struct cmd_line_argument_parser *self, char *name, char *description, char *version, struct bump_allocator *bump) { - if(ACHIOR_LABS_NULL(self)) + if (ACHIOR_LABS_NULL(self)) { return false; } - self->name = name; + self->name = name; self->description = description; - self->version = version; - self->bump = bump; + self->version = version; + self->bump = bump; - cmd_line_argument_list_new(&self->arguments,10,self->bump); + cmd_line_argument_list_new(&self->arguments, 10, self->bump); - return true;; + return true; + ; } -bool cmd_line_argument_parser_add_argument(struct cmd_line_argument_parser *self,struct cmd_line_argument argument) +bool cmd_line_argument_parser_add_argument(struct cmd_line_argument_parser *self, struct cmd_line_argument argument) { - if(ACHIOR_LABS_NULL(self)) + if (ACHIOR_LABS_NULL(self)) { return false; } - cmd_line_argument_list_push_back(&self->arguments,argument); + cmd_line_argument_list_push_back(&self->arguments, argument); return true; } - - -struct cmd_line_argument cmd_line_argument_parser_get(struct cmd_line_argument_parser *self,char *long_name) +struct cmd_line_argument cmd_line_argument_parser_get(struct cmd_line_argument_parser *self, char *long_name) { - if(ACHIOR_LABS_NULL(self)) + if (ACHIOR_LABS_NULL(self)) { ACHIOR_LABS_RETURN_DEFER(failure); } - for(u64 i = 0; i < self->arguments.size; i++) + for (u64 i = 0; i < self->arguments.size; i++) { - if(ACHIOR_LABS_STRCMP(self->arguments.data[i].long_name,long_name) == 0) + if (ACHIOR_LABS_STRCMP(self->arguments.data[i].long_name, long_name) == 0) { return self->arguments.data[i]; } @@ -68,47 +65,62 @@ struct cmd_line_argument cmd_line_argument_parser_get(struct cmd_line_argument_p return (struct cmd_line_argument){}; } -bool cmd_line_argument_parser_parse(struct cmd_line_argument_parser *self,int argc,char **argv) +void cmd_line_argument_parser_set(struct cmd_line_argument_parser *self, char *long_name, bool value) { - for(int i = 1; i < argc; i++) + if (ACHIOR_LABS_NULL(self)) + return; + + for (u64 i = 0; i < self->arguments.size; i++) + { + if (ACHIOR_LABS_STRCMP(self->arguments.data[i].long_name, long_name) == 0) + { + self->arguments.data[i].is_active = value; + self->arguments.data[i].value.flag = value; + } + } +} + +bool cmd_line_argument_parser_parse(struct cmd_line_argument_parser *self, int argc, char **argv) +{ + for (int i = 1; i < argc; i++) { char *arg = argv[i]; - if(arg[0] == '-') + if (arg[0] == '-') { - if(ACHIOR_LABS_STRLEN(arg) > 1 && arg[1] == '-') + if (ACHIOR_LABS_STRLEN(arg) > 1 && arg[1] == '-') { arg += 2; - for(u64 j = 0; j < self->arguments.size; j++) + for (u64 j = 0; j < self->arguments.size; j++) { struct cmd_line_argument argument = self->arguments.data[j]; - if(ACHIOR_LABS_STRCMP(argument.long_name,arg) == 0) + if (ACHIOR_LABS_STRCMP(argument.long_name, arg) == 0) { - switch(argument.type) + switch (argument.type) { - case CMD_LINE_ARGUMENT_FLAG: - { - self->arguments.data[j].is_active = true; - self->arguments.data[j].value.flag = true; - break; - } - case CMD_LINE_ARGUMENT_STRING: - { - if(i + 1 >= argc || argv[i+1][0] == '-') - { - // expected an argument after a value option - cmd_line_argument_parser_fatal(self,"expected an argument after a value option --%s", arg); - } - - self->arguments.data[j].is_active = true; - self->arguments.data[j].value.string = argv[++i]; - break; - } - default: + case CMD_LINE_ARGUMENT_FLAG: + { + self->arguments.data[j].is_active = true; + self->arguments.data[j].value.flag = true; + break; + } + case CMD_LINE_ARGUMENT_STRING: + { + if (i + 1 >= argc || argv[i + 1][0] == '-') { - cmd_line_argument_parser_fatal(self,"unsupported argument type", NULL); + // expected an argument after a value option + cmd_line_argument_parser_fatal(self, "expected an argument after a value option --%s", arg); } + + self->arguments.data[j].is_active = true; + self->arguments.data[j].value.string = argv[++i]; + break; + } + default: + { + cmd_line_argument_parser_fatal(self, "unsupported argument type", NULL); + } } } } @@ -116,37 +128,37 @@ bool cmd_line_argument_parser_parse(struct cmd_line_argument_parser *self,int ar else { arg += 1; - for(u64 j = 0; j < self->arguments.size; j++) + for (u64 j = 0; j < self->arguments.size; j++) { struct cmd_line_argument argument = self->arguments.data[j]; - if(ACHIOR_LABS_NOT_NULL(argument.short_name) && ACHIOR_LABS_STRCMP(argument.short_name,arg) == 0) + if (ACHIOR_LABS_NOT_NULL(argument.short_name) && ACHIOR_LABS_STRCMP(argument.short_name, arg) == 0) { - switch(argument.type) + switch (argument.type) { - case CMD_LINE_ARGUMENT_FLAG: - { - self->arguments.data[j].is_active = true; - self->arguments.data[j].value.flag = true; - break; - } - case CMD_LINE_ARGUMENT_STRING: - { - if(i + 1 >= argc || argv[i+1][0] == '-')// Catches missing arguments even when in between arguments - { - // expected an argument after a value option - //Prints the string with missing arguments - cmd_line_argument_parser_fatal(self,"expected an argument after a value option -%s \n", arg); - } - - self->arguments.data[j].is_active = true; - self->arguments.data[j].value.string = argv[++i]; - break; - } - default: + case CMD_LINE_ARGUMENT_FLAG: + { + self->arguments.data[j].is_active = true; + self->arguments.data[j].value.flag = true; + break; + } + case CMD_LINE_ARGUMENT_STRING: + { + if (i + 1 >= argc || argv[i + 1][0] == '-') // Catches missing arguments even when in between arguments { - cmd_line_argument_parser_fatal(self,"unsupported argument type %s\n", NULL); + // expected an argument after a value option + // Prints the string with missing arguments + cmd_line_argument_parser_fatal(self, "expected an argument after a value option -%s \n", arg); } + + self->arguments.data[j].is_active = true; + self->arguments.data[j].value.string = argv[++i]; + break; + } + default: + { + cmd_line_argument_parser_fatal(self, "unsupported argument type %s\n", NULL); + } } } } @@ -154,20 +166,13 @@ bool cmd_line_argument_parser_parse(struct cmd_line_argument_parser *self,int ar } else { - //We are sussposed to set the help flag so as to show usage - self->arguments.data[0].is_active = true; - self->arguments.data[0].value.flag = true; + // We are sussposed to set the help flag so as to show usage + cmd_line_argument_parser_set(self, "help", true); } } } - - - - - - -void cmd_line_argument_parser_fatal(struct cmd_line_argument_parser *self,char *str, char *argument_option) +void cmd_line_argument_parser_fatal(struct cmd_line_argument_parser *self, char *str, char *argument_option) { ACHIOR_LABS_PRINTF(str, argument_option); exit(1); diff --git a/src/cli/cmd_line.h b/src/cli/cmd_line.h index 4a9675e..78cfe49 100644 --- a/src/cli/cmd_line.h +++ b/src/cli/cmd_line.h @@ -54,4 +54,6 @@ void cmd_line_argument_parser_fatal(struct cmd_line_argument_parser *self,char * struct cmd_line_argument cmd_line_argument_parser_get(struct cmd_line_argument_parser *self,char *long_name); +void cmd_line_argument_parser_set(struct cmd_line_argument_parser *self, char *long_name, bool value); + #endif \ No newline at end of file