Skip to content

Commit afaef55

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1a4e40a commit afaef55

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

git-compat-util.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,29 @@ static inline int skip_prefix(const char *str, const char *prefix,
484484
return 0;
485485
}
486486

487+
/*
488+
* If the string "str" is the same as the string in "prefix", then the "arg"
489+
* parameter is set to the "def" parameter and 1 is returned.
490+
* If the string "str" begins with the string found in "prefix" and then a
491+
* "=" sign, then the "arg" parameter is set to "str + strlen(prefix) + 1"
492+
* (i.e., to the point in the string right after the prefix and the "=" sign),
493+
* and 1 is returned.
494+
*
495+
* Otherwise, return 0 and leave "arg" untouched.
496+
*
497+
* When we accept both a "--key" and a "--key=<val>" option, this function
498+
* can be used instead of !strcmp(arg, "--key") and then
499+
* skip_prefix(arg, "--key=", &arg) to parse such an option.
500+
*/
501+
int skip_to_optional_arg_default(const char *str, const char *prefix,
502+
const char **arg, const char *def);
503+
504+
static inline int skip_to_optional_arg(const char *str, const char *prefix,
505+
const char **arg)
506+
{
507+
return skip_to_optional_arg_default(str, prefix, arg, "");
508+
}
509+
487510
/*
488511
* Like skip_prefix, but promises never to read past "len" bytes of the input
489512
* buffer, and returns the remaining number of bytes in "out" via "outlen".

strbuf.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ int starts_with(const char *str, const char *prefix)
1111
return 0;
1212
}
1313

14+
int skip_to_optional_arg_default(const char *str, const char *prefix,
15+
const char **arg, const char *def)
16+
{
17+
const char *p;
18+
19+
if (!skip_prefix(str, prefix, &p))
20+
return 0;
21+
22+
if (!*p) {
23+
if (arg)
24+
*arg = def;
25+
return 1;
26+
}
27+
28+
if (*p != '=')
29+
return 0;
30+
31+
if (arg)
32+
*arg = p + 1;
33+
return 1;
34+
}
35+
1436
/*
1537
* Used as the default ->buf value, so that people can always assume
1638
* buf is non NULL and ->buf is NUL terminated even for a freshly

0 commit comments

Comments
 (0)