Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 91 additions & 86 deletions src/cli/cmd_line.c
Original file line number Diff line number Diff line change
@@ -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];
}
Expand All @@ -68,106 +65,114 @@ 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);
}
}
}
}
}
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);
}
}
}
}
}
}
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);
Expand Down
2 changes: 2 additions & 0 deletions src/cli/cmd_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -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