summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2018-08-03 17:11:15 +0800
committerJeremy Kerr <jk@ozlabs.org>2018-08-09 13:46:19 +0800
commit6f9c4330df5fb85504d21e1aa6973b302d80a5b9 (patch)
tree0eab07b984dd4115dfc1c56687ba8d829e6aca69
parent0f1a49c8e84730b8925c6e8f64c6e55dcf84d3ae (diff)
downloadjsnbd-6f9c4330df5fb85504d21e1aa6973b302d80a5b9.tar.gz
jsnbd-6f9c4330df5fb85504d21e1aa6973b302d80a5b9.zip
nbd-proxy: Add getopt-based option parsing
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
-rw-r--r--nbd-proxy.c28
1 files changed, 26 insertions, 2 deletions
diff --git a/nbd-proxy.c b/nbd-proxy.c
index e251a1e..3ce8879 100644
--- a/nbd-proxy.c
+++ b/nbd-proxy.c
@@ -20,6 +20,7 @@
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <getopt.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
@@ -546,6 +547,16 @@ static int config_select(struct ctx *ctx, const char *name)
return 0;
}
+static const struct option options[] = {
+ { .name = "help", .val = 'h' },
+ { 0 },
+};
+
+static void print_usage(const char *progname)
+{
+ fprintf(stderr, "usage: %s [configuration]\n", progname);
+}
+
int main(int argc, char **argv)
{
const char *config_name;
@@ -554,8 +565,21 @@ int main(int argc, char **argv)
config_name = NULL;
- if (argc > 1)
- config_name = argv[1];
+ for (;;) {
+ int c = getopt_long(argc, argv, "h", options, NULL);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'h':
+ case '?':
+ print_usage(argv[0]);
+ return c == 'h' ? EXIT_SUCCESS : EXIT_FAILURE;
+ }
+ }
+
+ if (optind < argc)
+ config_name = argv[optind];
ctx = &_ctx;
memset(ctx, 0, sizeof(*ctx));
OpenPOWER on IntegriCloud