From db0b922e1117123f620121e0501070048062dfeb Mon Sep 17 00:00:00 2001 From: natjms Date: Fri, 20 Jan 2023 17:23:48 -0800 Subject: [PATCH] Implement actual arg parsing --- calathea.c | 64 +++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/calathea.c b/calathea.c index 8a45645..077426d 100644 --- a/calathea.c +++ b/calathea.c @@ -5,6 +5,7 @@ #include #include #include +#include #include // Structure defining the content and metadata of a single page @@ -217,40 +218,43 @@ char * substitute_string(char dest[], char sub[], char *start, char *end) { return compiled; } -int main(int argc, char *argv[]) { - char pagesLocation[256] = "./pages"; - int initialInboundCapacity = 2; - char templateFileName[256] = "./template.html"; - char outputDirectoryName[256] = "./build"; +static struct argp_option options[] = { + { "src", 's', "dir", 0, "Source directory of pages", 0 }, + { "template", 't', "file", 0, "Template file path", 0 }, + { "output", 'o', "dir", 0, "Output directory", 0 }, + { 0 } +}; - /*** Argument Parsing ***/ - for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "--pages") == 0) { - // We only want to do this if a directory was actually supplied - if (i + 1 < argc) { - i++; - strcpy(pagesLocation, argv[i]); - } - } else if (strcmp(argv[i], "--template") == 0) { - if (i + 1 < argc) { - i++; - strcpy(templateFileName, argv[i]); - } - } else if (strcmp(argv[i], "--output-dir") == 0) { - if (i + 1 < argc) { - i++; - strcpy(outputDirectoryName, argv[i]); - } - } else if (strcmp(argv[i], "--incoming-cap") == 0) { - if (i + 1 < argc) { - i++; - initialInboundCapacity = atoi(argv[i]); - } - } else { - printf("Unknown argument: %s\n", argv[i]); +static char *pagesLocation = "./pages"; +static char *templateFileName = "./template.html"; +static char *outputDirectoryName = "./build"; + +static int parse_opt(int key, char *arg, struct argp_state *state) { + switch (key) { + case 's': { + pagesLocation = arg; + break; + } + case 't': { + templateFileName = arg; + break; + } + case 'o': { + outputDirectoryName = arg; + break; } } + return 0; +} + +int main(int argc, char *argv[]) { + int initialInboundCapacity = 2; + + struct argp argp = {options, parse_opt, 0, 0 }; + + argp_parse(&argp, argc, argv, 0, 0, 0); + char *templateContent = read_file(templateFileName); if (templateContent == NULL) {