Commit 5b7ba78630d935a8222db11d5bccffe85ddbf1eb

Etienne Samson 2018-06-23T15:45:04

examples: add a helper for boolean-style options

diff --git a/examples/common.c b/examples/common.c
index 772c20d..8c10424 100644
--- a/examples/common.c
+++ b/examples/common.c
@@ -184,6 +184,25 @@ static int match_int_internal(
 	return 1;
 }
 
+int match_bool_arg(int *out, struct args_info *args, const char *opt)
+{
+	const char *found = args->argv[args->pos];
+
+	if (!strcmp(found, opt)) {
+		*out = 1;
+		return 1;
+	}
+
+	if (!strncmp(found, "--no-", strlen("--no-")) &&
+	    !strcmp(found + strlen("--no-"), opt + 2)) {
+		*out = 0;
+		return 1;
+	}
+
+	*out = -1;
+	return 0;
+}
+
 int is_integer(int *out, const char *str, int allow_negative)
 {
 	return match_int_internal(out, str, allow_negative, NULL);
diff --git a/examples/common.h b/examples/common.h
index 503c8fb..1c7b203 100644
--- a/examples/common.h
+++ b/examples/common.h
@@ -91,6 +91,15 @@ extern int match_int_arg(
 	int *out, struct args_info *args, const char *opt, int allow_negative);
 
 /**
+ * Check current `args` entry against a "bool" `opt` (ie. --[no-]progress).
+ * If `opt` matches positively, out will be set to 1, or if `opt` matches
+ * negatively, out will be set to 0, and in both cases 1 will be returned.
+ * If neither the positive or the negative form of opt matched, out will be -1,
+ * and 0 will be returned.
+ */
+extern int match_bool_arg(int *out, struct args_info *args, const char *opt);
+
+/**
  * Basic output function for plain text diff output
  * Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`)
  */