From: Ayabusa Date: Sun, 7 Jun 2026 19:40:54 +0000 (+0200) Subject: good enough arg parser X-Git-Url: https://git.ayabusa.dev/?a=commitdiff_plain;h=78378a42a7746e51e7a7e4160b4560b506287b5e;p=thoryum.git good enough arg parser --- diff --git a/Makefile b/Makefile index 262a9eb..9faa0d0 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS = -Wall -Wextra -g LIBS = -lcurl SRC = src/main.c src/cpu.c src/downloader.c src/cli.c -OBJ = build/main.o build/cpu.o build/downloader.o src/cli.o +OBJ = build/main.o build/cpu.o build/downloader.o build/cli.o RES = build/Thoryum all: setup $(OBJ) build diff --git a/src/cli.c b/src/cli.c index 083bf2c..5643c76 100644 --- a/src/cli.c +++ b/src/cli.c @@ -5,8 +5,19 @@ #include "cli.h" extern char *optarg; // What the hell is that glibc ??? +extern int optind, opterr, optopt; + +void check_help(int argc, char **argv) { + for (int i = 1; i < argc; i++) + if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + print_help(); + exit(EXIT_SUCCESS); + } +} struct ctx *init_ctx(int argc, char **argv) { + check_help(argc, argv); + struct ctx *ctx = calloc(1, sizeof(struct ctx)); int opt; @@ -39,6 +50,18 @@ void *free_ctx(struct ctx *ctx) { return NULL; } +void print_ctx(struct ctx *ctx) { + printf( + "Thoryum context:\n" + "Mode = %s\n" + "Install location = '%s'\n" + "Silent = %s\n", + ctx->mode ? "UPDATE" : "NORMAL", + ctx->install_loc, + ctx->silent ? "YES" : "NOPE" + ); +} + void print_help() { printf( "Usage: thoryum [options]\n" diff --git a/src/cli.h b/src/cli.h index 9647c10..8a36ff2 100644 --- a/src/cli.h +++ b/src/cli.h @@ -12,6 +12,9 @@ struct ctx { int silent; }; +struct ctx *init_ctx(int argc, char **argv); +void *free_ctx(struct ctx *ctx); +void print_ctx(struct ctx *ctx); void print_help(); #endif diff --git a/src/cli.o b/src/cli.o deleted file mode 100644 index 48003b7..0000000 Binary files a/src/cli.o and /dev/null differ diff --git a/src/main.c b/src/main.c index 62bafe9..42bf73c 100644 --- a/src/main.c +++ b/src/main.c @@ -34,7 +34,7 @@ void patch_desktop_file(char *install_loc) { wait(NULL); } -int main() { +int main(int argc, char **argv) { printf("Hello this is Thoryum ;)\n"); enum CPU_opti opti = CPU_fetch_opti(); char opti_name[5] = {0}; @@ -47,5 +47,7 @@ int main() { */ //printf("Latest version url : '%s,'\n",DL_get_latest_version_url("AVX.zip")); //patch_desktop_file("."); - print_help(); + struct ctx *ctx = init_ctx(argc, argv); + print_ctx(ctx); + ctx = free_ctx(ctx); }