git-compat-util: introduce skip_to_optional_arg()
commitafaef55e230c6fcfb4e8a27c1b970f7e6400a7f6
authorChristian Couder <christian.couder@gmail.com>
Sat, 9 Dec 2017 20:40:07 +0000 (9 21:40 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 Dec 2017 00:10:12 +0000 (11 16:10 -0800)
tree90c784259dffd3f2b0a7fc75997485021bbdfdac
parent1a4e40aa5dc16564af879142ba9dfbbb88d1e5ff
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>
git-compat-util.h
strbuf.c