aboutsummaryrefslogtreecommitdiff
path: root/git-compat-util.h
diff options
context:
space:
mode:
authorChristian Couder <christian.couder@gmail.com>2017-12-09 21:40:07 +0100
committerJunio C Hamano <gitster@pobox.com>2017-12-11 16:10:12 -0800
commitafaef55e230c6fcfb4e8a27c1b970f7e6400a7f6 (patch)
tree90c784259dffd3f2b0a7fc75997485021bbdfdac /git-compat-util.h
parent1a4e40aa5dc16564af879142ba9dfbbb88d1e5ff (diff)
downloadgit-afaef55e230c6fcfb4e8a27c1b970f7e6400a7f6.tar.gz
git-afaef55e230c6fcfb4e8a27c1b970f7e6400a7f6.tar.xz
git-compat-util: introduce skip_to_optional_arg()
We often accept both a "--key" option and a "--key=<val>" option. These options currently are parsed using something like: if (!strcmp(arg, "--key")) { /* do something */ } else if (skip_prefix(arg, "--key=", &arg)) { /* do something with arg */ } which is a bit cumbersome compared to just: if (skip_to_optional_arg(arg, "--key", &arg)) { /* do something with arg */ } This also introduces skip_to_optional_arg_default() for the few cases where something different should be done when the first argument is exactly "--key" than when it is exactly "--key=". In general it is better for UI consistency and simplicity if "--key" and "--key=" do the same thing though, so that using skip_to_optional_arg() should be encouraged compared to skip_to_optional_arg_default(). Note that these functions can be used to parse any "key=value" string where "key" is also considered as valid, not just command line options. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'git-compat-util.h')
-rw-r--r--git-compat-util.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/git-compat-util.h b/git-compat-util.h
index cedad4d58..68b2ad531 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -485,6 +485,29 @@ static inline int skip_prefix(const char *str, const char *prefix,
}
/*
+ * If the string "str" is the same as the string in "prefix", then the "arg"
+ * parameter is set to the "def" parameter and 1 is returned.
+ * If the string "str" begins with the string found in "prefix" and then a
+ * "=" sign, then the "arg" parameter is set to "str + strlen(prefix) + 1"
+ * (i.e., to the point in the string right after the prefix and the "=" sign),
+ * and 1 is returned.
+ *
+ * Otherwise, return 0 and leave "arg" untouched.
+ *
+ * When we accept both a "--key" and a "--key=<val>" option, this function
+ * can be used instead of !strcmp(arg, "--key") and then
+ * skip_prefix(arg, "--key=", &arg) to parse such an option.
+ */
+int skip_to_optional_arg_default(const char *str, const char *prefix,
+ const char **arg, const char *def);
+
+static inline int skip_to_optional_arg(const char *str, const char *prefix,
+ const char **arg)
+{
+ return skip_to_optional_arg_default(str, prefix, arg, "");
+}
+
+/*
* Like skip_prefix, but promises never to read past "len" bytes of the input
* buffer, and returns the remaining number of bytes in "out" via "outlen".
*/