From: Michael Tremer Date: Sun, 10 May 2026 11:34:57 +0000 (+0000) Subject: main: Add a very basic command line parser X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9229a95bf0be18981de22123727f21d703f82492;p=zone-sync.git main: Add a very basic command line parser Signed-off-by: Michael Tremer --- diff --git a/configure.ac b/configure.ac index 653dc6b..1e55ca6 100644 --- a/configure.ac +++ b/configure.ac @@ -122,7 +122,10 @@ AC_TYPE_SSIZE_T AC_TYPE_UID_T AC_TYPE_UINT64_T -AC_CHECK_HEADERS([]) +AC_CHECK_HEADERS([ \ + argp.h \ + syslog.h \ +]) AC_CHECK_FUNCS([]) diff --git a/main.c b/main.c index f3c7629..c8c900f 100644 --- a/main.c +++ b/main.c @@ -18,6 +18,66 @@ # # #############################################################################*/ +#include +#include + +typedef struct ctx { + int log_level; +} ctx_t; + +const char* argp_program_version = PACKAGE_VERSION; + +static const char* args_doc = "TODO"; + +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) { + ctx_t* ctx = state->input; + + switch (key) { + case OPT_DEBUG: + ctx->log_level = LOG_DEBUG; + break; + + // Ignore these + case ARGP_KEY_END: + case ARGP_KEY_ERROR: + case ARGP_KEY_INIT: + case ARGP_KEY_FINI: + break; + + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; +} + int main(int argc, char* argv[]) { + int r; + + // Create the context + ctx_t ctx = {}; + + // Setup the command line parser + struct argp parser = { + .options = options, + .parser = parse, + .args_doc = args_doc, + }; + int arg_index = 0; + + // Parse the command line + r = argp_parse(&parser, argc, argv, ARGP_IN_ORDER, &arg_index, &ctx); + if (r) + return r; + return 0; }