pretty.c: rework describe options parsing for better extensibility

It contains option arguments only, not options. We would like to add option support here too, but to do that we need to distinguish between different types of options. Lay out the groundwork for distinguishing between bools, strings, etc. and move the central logic (validating values and pushing new arguments to *args) into the successful match, because that will be fairly conditional on what type of argument is being parsed. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eli Schwartz committed Oct 31, 2021 at 13:15 UTC 3c6eb4ec50d9a4a9017fb8c1f220d12dceb206b1
1 file changed +21 -10
pretty.c
+21 -10
@@ -1216,28 +1216,39 @@ int format_set_trailers_options(struct process_trailer_options *opts,
1216
1217 static size_t parse_describe_args(const char *start, struct strvec *args)
1218 {
1219 - const char *options[] = { "match", "exclude" };
1219 + struct {
1220 + char *name;
1221 + enum {
1222 + DESCRIBE_ARG_STRING,
1223 + } type;
1224 + } option[] = {
1225 + { "exclude", DESCRIBE_ARG_STRING },
1226 + { "match", DESCRIBE_ARG_STRING },
1227 + };
1228 const char *arg = start;
1229
1230 for (;;) {
1223 - const char *matched = NULL;
1231 + int found = 0;
1232 const char *argval;
1233 size_t arglen = 0;
1234 int i;
1235
1228 - for (i = 0; i < ARRAY_SIZE(options); i++) {
1229 - if (match_placeholder_arg_value(arg, options[i], &arg,
1230 - &argval, &arglen)) {
1231 - matched = options[i];
1236 + for (i = 0; !found && i < ARRAY_SIZE(option); i++) {
1237 + switch (option[i].type) {
1238 + case DESCRIBE_ARG_STRING:
1239 + if (match_placeholder_arg_value(arg, option[i].name, &arg,
1240 + &argval, &arglen)) {
1241 + if (!arglen)
1242 + return 0;
1243 + strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
1244 + found = 1;
1245 + }
1246 break;
1247 }
1248 }
1235 - if (!matched)
1249 + if (!found)
1250 break;
1251
1238 - if (!arglen)
1239 - return 0;
1240 - strvec_pushf(args, "--%s=%.*s", matched, (int)arglen, argval);
1252 }
1253 return arg - start;
1254 }