summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEtienne Samson <samson.etienne@gmail.com>2018-06-23 15:45:04 +0200
committerEtienne Samson <samson.etienne@gmail.com>2018-07-07 14:52:24 +0200
commit5b7ba78630d935a8222db11d5bccffe85ddbf1eb (patch)
treeb03eb4dbf470a3f464f48643cbb2d852e6ae8b0f
parent8e063eb66b7a2a45854460a55797d7e05e03352d (diff)
downloadlibgit2-5b7ba78630d935a8222db11d5bccffe85ddbf1eb.tar.gz
examples: add a helper for boolean-style options
-rw-r--r--examples/common.c19
-rw-r--r--examples/common.h9
2 files changed, 28 insertions, 0 deletions
diff --git a/examples/common.c b/examples/common.c
index 772c20d33..8c1042471 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 503c8fbbf..1c7b2035e 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`)
*/