From 4c626f657ed61a8e2df5d104ece196384f628f86 Mon Sep 17 00:00:00 2001 From: kevinnls <57634663+kevinnls@users.noreply.github.com> Date: Sat, 3 Apr 2021 23:26:31 +0530 Subject: [PATCH] feat: basic argugment parsing + placeholders - added placeholders for items to be implemented - basic parsing of _argv using `parse()` - copies `_argv` to global `argv` - alternative pref - need hashing function to match with switch-case - refactored print statements into main - and wrapped in conditions - fixed global default conditions to 1 (true) - vim auto indent applied --- src/main.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index d91a6e6..b1cd39e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,24 +1,87 @@ #include +#include //remove if alt to `malloc` + clone found +unsigned long int hash(){ + //implement a hashing function +} + +// define hashes of legal cli flags +/* +#define OUTPUT hash("-o"); +#define ERROR hash("-e"); +#define NOOUT hash("-t"); +#definte NOERR hash("-r"); +*/ + +// default values char *output = "This is output", *error = "This is an error message"; -int printOut = 0, printErr = 0; +int printOut = 1, printErr = 1; -void write_stdout(){ - fprintf(stdout, "%s\n", output); +// to make the cli args globally available +char **argv; + +// ! use minimal # of functions to reduce stacks ! // + +void print_usage(){ + printf("HELP MENU PRINTED"); + // JK. make a help menu } -void write_stderr(){ - fprintf(stderr, "%s\n", error); + +int parse(const int index){ + + int _return_value = 1; + char* param = argv[index]; + + printf("currently parsing index %d of argv: %s\n", index, param); + + // hash `param` + /* + hash(param) + * */ + // run switch-case matching on hash + /* + switch (hashed_param) { + case OUTPUT: + output = argv[index+1]; + ++_return_value; + case ERROR: + error = argv[index+1]; + ++_return_value; + case NOOUT: + printOut = 0; + case NOERR: + printErr = 0; + default: + fprintf(stderr, "ERROR: unrecognised flags\n"); + print_usage(); + exit(1); + } + */ + + return _return_value; } -int main(int argc, char const *argv[]) { +int main(const int argc, char **_argv){ + + //look for alt method to make _argv globally accessible + argv = malloc( sizeof *argv * argc); + for(int i=0; i 1){ //parse the arguments + int counter = 1; + while ( counter < argc ){ + counter += parse(counter); + } } - write_stdout(); - write_stderr(); + if(printOut) + fprintf(stdout, "%s\n", output); + if(printErr) + fprintf(stderr, "%s\n", error); return 0; }