From 4fe942dd95a8b068985a380e24be3482087c5284 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Thu, 10 Oct 2019 22:31:48 +0200 Subject: Make leasetime argument be an option We don't want it to be mandatory but we would like to be able to add more mandatory positional arguments in the future. An option it is. --- wg-dynamic-server.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/wg-dynamic-server.c b/wg-dynamic-server.c index e9df888..474678f 100644 --- a/wg-dynamic-server.c +++ b/wg-dynamic-server.c @@ -29,7 +29,7 @@ #include "netlink.h" static const char *progname; -static const char *wg_interface; +static const char *wg_interface = NULL; static struct in6_addr well_known; static wg_device *device = NULL; @@ -50,7 +50,7 @@ struct mnl_cb_data { static void usage() { - die("usage: %s \n", progname); + die("usage: %s [--leasetime ] \n", progname); } static int data_cb(const struct nlmsghdr *nlh, void *data) @@ -649,12 +649,24 @@ int main(int argc, char *argv[]) char *endptr = NULL; progname = argv[0]; - if (argc != 3) - usage(); - - wg_interface = argv[1]; - leasetime = (uint32_t) strtoul(argv[2], &endptr, 10); - if (*endptr) + ++argv; + --argc; + + while (argc > 0) { + if (!strcmp(argv[0], "--leasetime") && argc >= 2) { + leasetime = (uint32_t) strtoul(argv[1], &endptr, 10); + if (*endptr) + usage(); + argv += 2; + argc -= 2; + } else { + wg_interface = argv[0]; + argv += 1; + argc -= 1; + break; + } + } + if (!wg_interface || argc > 0) usage(); setup(); -- cgit v1.2.3-59-g8ed1b