]> git.ipfire.org Git - collecty.git/commitdiff
daemon: Parse command line options
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 11:13:25 +0000 (11:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 11:13:25 +0000 (11:13 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/main.c

index 01e1a78883ee72bcbc39776ea19a2c31024b25f8..bf8246ce31bab02630891f4ecc4dc7bae8d93fa8 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <argp.h>
 #include <stddef.h>
 #include <stdlib.h>
+#include <syslog.h>
 
 #include "ctx.h"
 
+const char* argp_program_version = PACKAGE_VERSION;
+
+static const char* doc = "The collecty Daemon";
+
+enum {
+       OPT_DEBUG  = 1,
+};
+
+static struct argp_option options[] = {
+       { "debug", OPT_DEBUG, NULL, 0, "Run in debug mode", 0 },
+       { NULL },
+};
+
+static error_t parse(int key, char* arg, struct argp_state* state) {
+       collecty_ctx* ctx = state->input;
+
+       switch (key) {
+               case OPT_DEBUG:
+                       collecty_ctx_set_log_level(ctx, LOG_DEBUG);
+                       break;
+
+               default:
+                       return ARGP_ERR_UNKNOWN;
+       }
+
+       return 0;
+}
+
 int main(int argc, char* argv[]) {
        collecty_ctx* ctx = NULL;
+       struct argp parser = {
+               .options = options,
+               .parser  = parse,
+               .doc     = doc,
+       };
+       int arg_index = 0;
        int r;
 
        // Allocate a new context
@@ -32,7 +68,10 @@ int main(int argc, char* argv[]) {
        if (r < 0)
                goto ERROR;
 
-       // TODO
+       // Parse command line arguments
+       r = argp_parse(&parser, argc, argv, ARGP_NO_ARGS, &arg_index, &ctx);
+       if (r)
+               goto ERROR;
 
 ERROR:
        if (ctx)