examples: add a helper for boolean-style options
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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`)
*/