diff options
author | Linus Nordberg <linus@nordberg.se> | 2019-10-10 22:31:48 +0200 |
---|---|---|
committer | Linus Nordberg <linus@nordberg.se> | 2019-10-10 22:31:48 +0200 |
commit | 4fe942dd95a8b068985a380e24be3482087c5284 (patch) | |
tree | ea8a03e5544f8d44d8343396ce2979cfd121ef22 | |
parent | Make leasetime command line argument mandatory (diff) | |
download | wg-dynamic-4fe942dd95a8b068985a380e24be3482087c5284.tar.xz wg-dynamic-4fe942dd95a8b068985a380e24be3482087c5284.zip |
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.
-rw-r--r-- | wg-dynamic-server.c | 28 |
1 files 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 <wg-interface> <leasetime>\n", progname); + die("usage: %s [--leasetime <leasetime>] <wg-interface>\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(); |