Implement actual arg parsing

This commit is contained in:
Nat 2023-01-20 17:23:48 -08:00
parent dd9a450042
commit db0b922e11
Signed by: nat
GPG Key ID: B53AB05285D710D6
1 changed files with 34 additions and 30 deletions

View File

@ -5,6 +5,7 @@
#include <dirent.h> #include <dirent.h>
#include <math.h> #include <math.h>
#include <ctype.h> #include <ctype.h>
#include <argp.h>
#include <cmark.h> #include <cmark.h>
// Structure defining the content and metadata of a single page // 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; return compiled;
} }
int main(int argc, char *argv[]) { static struct argp_option options[] = {
char pagesLocation[256] = "./pages"; { "src", 's', "dir", 0, "Source directory of pages", 0 },
int initialInboundCapacity = 2; { "template", 't', "file", 0, "Template file path", 0 },
char templateFileName[256] = "./template.html"; { "output", 'o', "dir", 0, "Output directory", 0 },
char outputDirectoryName[256] = "./build"; { 0 }
};
/*** Argument Parsing ***/ static char *pagesLocation = "./pages";
for (int i = 1; i < argc; i++) { static char *templateFileName = "./template.html";
if (strcmp(argv[i], "--pages") == 0) { static char *outputDirectoryName = "./build";
// We only want to do this if a directory was actually supplied
if (i + 1 < argc) { static int parse_opt(int key, char *arg, struct argp_state *state) {
i++; switch (key) {
strcpy(pagesLocation, argv[i]); case 's': {
pagesLocation = arg;
break;
} }
} else if (strcmp(argv[i], "--template") == 0) { case 't': {
if (i + 1 < argc) { templateFileName = arg;
i++; break;
strcpy(templateFileName, argv[i]);
} }
} else if (strcmp(argv[i], "--output-dir") == 0) { case 'o': {
if (i + 1 < argc) { outputDirectoryName = arg;
i++; break;
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]);
} }
} }
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); char *templateContent = read_file(templateFileName);
if (templateContent == NULL) { if (templateContent == NULL) {